Wednesday, 18 September 2013

Display high resolution image using C#

Display high resolution image using C#

I'm saving both high resolution and compressed version of high resolution
image in the database. When the user requests a high resolution image, i
need to display that else the compressed one. here is my code. The issue
is : when i set that image byte array into a stream and bitmap, file size
has compressed 2.27MB to 339kB. What i'm doing wrong here.
private void DisplayImageFromBytes(byte[] byteArray, int resizeWidth, bool
isHiResImage) {
if (isHiResImage)
{
Stream stream = new MemoryStream(byteArray);
System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
Bitmap bitmap = null;
if (resizeWidth > 0 && img.Width > resizeWidth)
{
int newHeight = (int)((float)img.Height * ((float)resizeWidth /
(float)img.Width));
bitmap = new Bitmap(img, resizeWidth, newHeight);
}
else
{
bitmap = new Bitmap(img.Width, img.Height,
System.Drawing.Imaging.PixelFormat.Max);
}
Response.ContentType = "image/Jpeg";
bitmap.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Dispose();
img.Dispose();
bitmap.Dispose();
}
else
{
DisplayImageFromBytes(byteArray, resizeWidth);
}
}

No comments:

Post a Comment