Create ThumbNail from image byte[]

private

byte[] GenerateThumbNail(byte[] foto){

System.IO.MemoryStream memStream = new System.IO.MemoryStream(foto);

int thumbWidth = 88;
int thumbHeight = 100;
if (QueryStringHelper.ItemType == “detail”){
thumbWidth = 250;
thumbHeight = 250;
}

System.Drawing.Image myImage = System.Drawing.Image.FromStream(memStream);

HtmlImage myHtmlImage = new HtmlImage();

if (myImage.Width > myImage.Height) {

if (myImage.Width > thumbWidth) {

myHtmlImage.Width = thumbWidth;

myHtmlImage.Height = Convert.ToInt32(myImage.Height*thumbWidth/myImage.Width);

}else {

myHtmlImage.Width = myImage.Width;

myHtmlImage.Height = myImage.Height;

}

// portrait image

}else {

if (myImage.Height > thumbHeight) {

myHtmlImage.Height = thumbHeight;

myHtmlImage.Width = Convert.ToInt32(myImage.Width*thumbHeight/myImage.Height);

}else {

myHtmlImage.Width = myImage.Width;

myHtmlImage.Height = myImage.Height;

}}

 

System.Drawing.Image myThumbnail;

System.Drawing.Image.GetThumbnailImageAbort myAbort = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);

myThumbnail = myImage.GetThumbnailImage(myHtmlImage.Width,myHtmlImage.Height, myAbort, IntPtr.Zero);

memStream = new System.IO.MemoryStream();

myThumbnail.Save(memStream, System.Drawing.Imaging.ImageFormat.Jpeg);

byte[] imageContent = new Byte[memStream.Length];

// rewind the memory stream

memStream.Position = 0;

// load the byte array with the image

memStream.Read(imageContent, 0, (int)memStream.Length);

return imageContent;

}