Profile

Click to view full profile
Hi, I'm Veerapat Sriarunrungrueang, an expert in technology field, especially full stack web development and performance testing.This is my coding diary. I usually develop and keep code snippets or some tricks, and update to this diary when I have time. Nowadays, I've been giving counsel to many well-known firms in Thailand.
view more...

Thursday, September 13, 2012

Get Dynamic Image in string format from Post Ajax Services

Instead of loading images via provided links, we can send images directly, and request it from ajax post channel. In order to do that, first covert image into array of bytes, then transform them into string base 64.
For example:
public JsonResult GetImage(string chartname)
{
    var chart = new Chart();
    chart.ChartAreas.Add(new ChartArea());

    var series = new Series(chartname);
    series.ChartType = SeriesChartType.Line;
    series.Points.AddXY(1, 1);
    series.Points.AddXY(2, 2);
    series.Points.AddXY(3, 3);

    chart.Series.Add(series);

    string result = null;
    byte[] bytes = null;
    using (MemoryStream stream = new MemoryStream())
    {
        chart.SaveImage(stream, ChartImageFormat.Png);
        bytes = stream.ToArray();
        result = Convert.ToBase64String(bytes);
    }
    return Json(result);
}
This example will convert chart image into PNG binary file format then convert to string base 64 before return.

For the client side, just write ajax post method, after get data insert image element into somewhere on a page.
For example:
<!DOCTYPE html>
<html>
 <head>
  <title>Image Service Index</title>
  <script type="text/javascript">
      function load() {
    $.ajax({
        type: "POST",
        data: { "chartname": "ImageServiceTest" },
     url: "@Url.Action("GetImage", "ImageService")",
     dataType: "json",
     success: function (response) {
         var chartDivId = $('#imageContent');
      // Response message will be json format if we use json as a data type
      $(chartDivId).html('<img src="data:image/png;base64,' + response + '" />');
     },
     error: function (ex) {
      debugger;
      alert(ex.status);
     }
    });
   }
   
   window.onload = load;
  </script>
 </head>
<body>
 <div id="imageContent">
 </div>
</body>
</html>
You can download full example here.
P.S. This example use ASP.NET MVC 3 for the server side, and use jQuery in the client side.

Setting Router (DD-WRT) as a client in unbridged mode

This a a Linksys router (DD-WRT), I just borrowed to configure it as a client hahaha. I mean config this router to connect main router ( has WAN) via wireless network, then make computers and client router connect together via LAN.

To set it up do as follows:
  • Change router mode (in wireless tab) to client mode, and set SSID to be the same as main router's SSID.
  • In wireless security tab (sub tab), set security mode and password to be the same as main router's also.
  • In security tab, disable firewall because it will make your setting become harder to be set. You can configure later if you want to.
  • In setup tab, change connection type to Automatic Configuration - DHCP, and disable STP.
  • Change router IP, which is different from main router IP. e.g. if your main router IP is 192.168.1.*, you can change a client router IP to 192.168.2.*. Next, use the same subnet mask.
  • Check at Use DNSMasq for DHCP.
  • Check at use DNSMasq for DNS.
  • Check at DHCP-Authoritative.
  • Set other stuffs if necessary.

Tuesday, September 11, 2012

Auto Mouse Move in C#

In .NET platform, it allows us to access a cursor of the mouse, and intercept accessibility from external environment (user input). It's quite easy to do so, just import "System.Windows.Forms", then play with Cursor class using static property "Cursor.Position". The property Position is from "System.Drawing" so you need to import this too in order to play with cursor position. The following code is an example program:
public static void Main()
{
     // Move cursor to (100, 100)
     Cursor.Position = new Point(100, 100);
}
Download full example program here.

Friday, September 7, 2012

Let's create own Evolutionary Optimization Algorithms (C#)

This post is just the same as the previous post. It's about basic algorithms in AI to solve optimization problems. In this post, I read from the same writer as previous post. He described how do we implement evolutionary algorithms and a program structure in the following link: http://msdn.microsoft.com/en-us/magazine/jj133825.aspx.

Let's create own Neuron Networks (C#)

It's a kind of fun to create own Neuron Networks. To review a school age lol (in AI class), take a look at http://msdn.microsoft.com/en-us/magazine/hh975375.aspx. He described very clear and detailed. You can follow him to create your own :D. 

The following page is a preview page that I just referred above:

Wednesday, September 5, 2012

How to Implement IDisposable correctly

Before getting to know how to implement IDisposable, must has some basic about .NET system first.

Unmanaged Code, which means the code developed outside .NET Framework is known as unmanaged code. It's not run under CLR (Common Language Runtime) such as C++ can be used to write such applications , for examples, access low level functions of the operating system. It offers the powerful features that managed code does not, and might damaging as well. Unmanaged code can be unmanged source code and unmanaged compile code, and  unmanged  code is execute with the help of wrapper classes, and caution is required (doesn't be executed by CLR). It will be compiled into machine code and executed by the OS directly.

Managed Code is the code that targets to common language runtime which is under foundation of .NET Framework. Managed Code will supply necessary things for CLR to provides services such as memory management, cross-language integration, handling security, lifetime control of objects, in short, it will look after your applications. All code based on IL (Intermediate Language) executes as managed code.

Garbage Collector will clean unused resources automatically, but can still call it explicitly. It will do finalizer in destructor of each object. The process of cleaning resources are done in a finalizer thread not a main thread.

Unmanaged Resources falls into two categories:
  1. Resources that are wrapped in a managed class (ie Bitmap, Font etc) but still need Dispose to be called to clean them up property.
  2. Resources that you allocated that are representations of native resources (ie device contexts that need to be released)
The propose of using dispose is we want to free both kinds of unmanged resources. Note that for the first kind of unmanged resources, the garbage collector will take care of them  when it have to clean them up. We only need to focus about true native resources that we have allocated (in case we have).

Monday, September 3, 2012

Quick Prototyper for Web and Mobile Apps

Take a loook at: http://www.justinmind.com/prototyper/free-edition. It's free and easy to use for prototype Mobile and Web Applications. It will be useful in case you want to show idea of your apps to others, want to make them understand look and feel of your apps in the same time, and want to avoid misunderstand and communication when deliver messages to others.

The following demo is the example of this program.

Thursday, July 26, 2012

How to temporary fix mouse cursor disappear in Google Chrome

Some of you may face this problem in Google Chrome that the mouse cursor will be dis appear when it stay idle just for seconds, and it will be appear again when it is moved. Moreover, it will be gone during drag a web content. So, this problem is very annoying. I, personally , don't know how this problem come from, but to fix this problem (may be just temporary fix) you need to kill process of plug-in "Shockwave Flash" task using Chrome Task Manager then your cursor will be like the old one you miss T^T.

Saturday, July 7, 2012

Silverlight Binding and StringFormat in XAML

This link is help a lot to know string format syntax in Silverlight Binding.

Tilt Effect in Windows Phone

Take a look at a following link, he clearly explained about the problem of tilt effect in Windows Phone Toolkit, and then how to use his effect in Windows Phone development in a pace.