Sunday, August 23, 2009

Get MemoryStream object from an image file, save MemoryStream Object to an image file

I see a lot of people who want to get MemoryStream object from an image file and to save a MemoryStream object to a file, so I decided to write a little post about it.

A little explanation about MemoryStream Class:
The MemoryStream class creates streams that have memory as a backing store instead of a disk or a network connection. MemoryStream encapsulates data stored as an unsigned byte array. The encapsulated data is directly accessible in memory. Memory streams can reduce the need for temporary buffers and files in an application.


Let's start with saving an image file to a MemoryStream object.
First i will show a code snippet and then I will explain further.

Code snippet(c#):


//Read the file into a MemoryStream.
FileStream fileStream = File.OpenRead(filePath);
MemoryStream imageStream = new MemoryStream();

//Copy all data from the file to MemoryStream Object.
imageStream.SetLength(fileStream.Length);
fileStream.Read(imageStream.GetBuffer(), 0, (int)fileStream.Length);

//Clean up
fileStream.Close();

So what is going on in here…

First open the chosen file for reading and save the stream instance inside the fileStream object.


FileStream fileStream = File.OpenRead(filePath);

Set imageStream length.


imageStream.SetLength(fileStream.Length);

Read all the file data and writes it to the imageStream buffer.


fileStream.Read(imageStream.GetBuffer(), 0, (int)fileStream.Length);

That's all, now we have an image represented by a MemoryStream:)


After we finished explaining how to save image to a MemoryStream object, we will save MemoryStream object into an image file.

Code snippet(c#):


using (FileStream imageFile = File.OpenWrite("\\Liquid.jpg")
{
imageFile.Write(ms.ToArray(), 0, (int)ms.Length);
ms.Close();
}

First we will open the stream to a file, the method creates a new file if it is not already exists. imageFile object hold the stream instance of the file.


using (FileStream imageFile = File.OpenWrite("Liquid.jpg")

Write() method writes all the bytes to an imagefile stream instance object, using data from buffer.


imageFile.Write(ms.ToArray(), 0, (int)ms.Length);


See you in the next post,
The-Liquid.

Source Code:
Download

Wednesday, August 19, 2009

ASP.net Ajax - Connect DropDownList select to ajax event.

Today we will demonstrate a client-server communication using Ajax under ASP.Net.

We will create client-side event, which communicates the server side method that has the WebMethod attribute and should return us the appropriate data by id.

First of all, explanation about the example that we are going to write:

When the user select an item of the DropDownList control, a client-side event is triggered, this event will send an Ajax Request to the server-side with the selected data. The Web Service will parse the Ajax request and will execute the target method.

In our case the target method should return a serialized object that holds all relative user information.

If the client-side received a valid response from the server, a method which named 'OnSuccess' will be executed, this method will plant all user information into the correct fields.

Now let's start coding, First of all lets add a ScriptManager control to our page ( manages client script for Microsoft ASP.NET AJAX pages).

- DropDownList control.

- 6 labels (3 will show the fields names and the other 3 will display dynamic information).

Code example:




Design example:


In addition we'll implement a new handler for the 'onchange' event:


<script type ="text/javascript">

function GetNameInformation() {

//Get the selected name in the DropDownList.

var selectedValue = document.getElementById("usersDropDown").value;

//Call WebMethod.

PageMethods.GetIdInformation(selectedValue, OnSuccess, OnFail);

}

Now let's set the 'onchange' event of our DropDownList control to the new handler we just wrote:

<asp:DropDownList ID="usersDropDown" runat="server" Width="200px" onchange="GetNameInformation()">

A little explanation about GetIdInformation() method:
The first parameter 'selectedValue' is sent to the server.
The second parameter 'OnSuccess' has the name of the method that run if the request success.
The third parameter 'OnFail' has the name of the method that runs if the request failed.


//The method planting the response data in the labels fields. The method is executed only if GetNameInformation() Succeded.
//response - The variable actually represents the object that returned from the server.

function OnSuccess(response) {
document.getElementById(""nameDisplayLabel").innerHTML = response.Name;
document.getElementById("ageDisplayLabel").innerHTML = response.Age;
document.getElementById("countryDisplayLabel").innerHTML = response.Country;
}
///The function is performed in case the request to the server fails.
function OnFail() {
alert('Failed to get Name information.');


Now we will change 'EnablePageMethods' property that indicates whether public static page methods in an ASP.NET page can be called from client script. Of course, in our case we select "true".


<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods=true/>


Let's go to Server-side:

What's left is to execute the program.

See you on the next post,
The Liquid.

Source Code
Download