Friday, October 3, 2008

Thumbnail Creation

Hi,

The following code is used to create a thumbnail image from the original image using C# code.

string file = "D:\\dcraw\\exif.jpg"; //Request.QueryString["file"];

// create an image object, using the filename we just retrieved
System.Drawing.Image image = System.Drawing.Image.FromFile(file); //Server.MapPath(file));

// create the actual thumbnail image
System.Drawing.Image thumbnailImage = image.GetThumbnailImage(64, 64, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);

// make a memory stream to work with the image bytes
MemoryStream imageStream = new MemoryStream();

// put the image into the memory stream
thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);

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

// rewind the memory stream
imageStream.Position = 0;

// load the byte array with the image
imageStream.Read(imageContent, 0, (int)imageStream.Length);

// return byte array to caller with image type
Response.ContentType = "image/jpeg";
Response.BinaryWrite(imageContent);

No comments: