Friday, April 1, 2011

Create Thumb Image In Asp.net

here is the steps:

1) create the simple web application (Asp.net using C#).

Add Name Space using System.IO


2) put the image file in to the application (here i used Sunset.jpg it’s resolution is (800 ,600)).

3) put the one button (id =btnGenerateThumbImage)

4) put the below code in to the click event of the button.

//here is the file name
string ImageFileName = “sunset.jpg”;
//create the image object here and gice the filename
//retrive the physical path of the file
System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(ImageFileName));

//here is create the object for the newly image
//give the width and height for that image
System.Drawing.Image newImage = image.GetThumbnailImage

(75, 75, new System.Drawing.Image.GetThumbnailImageAbort(Callback), IntPtr.Zero);

// create the object for memory stream
MemoryStream ObjMemoryStream = new MemoryStream();

// save the image in to memory stream and here i used the format jpeg
newImage.Save(ObjMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);

// create byte array the same size as the new image
byte[] imageContent = new Byte[ObjMemoryStream.Length];

// asign the position to the memory stream
ObjMemoryStream.Position = 0;
ObjMemoryStream.Read(imageContent, 0, (int)ObjMemoryStream.Length);
Response.ContentType = “image/jpeg”;

Response.BinaryWrite(imageContent);

5) put the below function in to the file which is not important but use it./// these is not Required but simple use
public bool Callback()
{
return true;
}
--
Happy coding..

No comments: