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;
        }

thnx

http://amitpatriwala.blogspot.com/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s