Tuesday, March 2, 2010

Create thumbnails for images in c#,Image size reduction, Decrease the size of images in c#

public byte[] imagereduce(System.Drawing.Image imgInput, int maxImageSize)
{

int tWidth;
int tHeight;
double widthHeightRatio = (double)imgInput.Width / (double)imgInput.Height;
// If width greater than height, then width should be max image size, otherwise height should be.
// Image should keep the same proportions.
//tWidth = maxImageSize;
//tHeight = (int)(maxImageSize / widthHeightRatio);
if (widthHeightRatio > 1.0)
{
tWidth = maxImageSize;
tHeight = (int)(maxImageSize / widthHeightRatio);
}
else
{
tWidth = (int)(maxImageSize * widthHeightRatio);
tHeight = maxImageSize;
}

//Session["imgheight"] = tHeight;
//Session["imgwidth"] = tWidth;

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

System.Drawing.Image myThumbnail = imgInput.GetThumbnailImage(tWidth, tHeight, myCallBack, IntPtr.Zero);
//myThumbnail.Save(Server.MapPath(sSavePath + sThumbFile));


System.IO.MemoryStream ms = new System.IO.MemoryStream();
myThumbnail.Save(ms, ImageFormat.Jpeg);
byte[] bitmapData = ms.ToArray();
return bitmapData;
}
public bool ThumbnailCallback()
{
return false;
}

Customized Message pop up Box in c# for websites

It gives you a pop up msgbox to show error messages or any alerts.

public Label msgbox(string text)
{
Label lbl = new Label();
lbl.Text = "";
return (lbl);
}