This is not "compressing". Probably you store already compressed image, if you store JPEG or something else with lossy or lostless compression. What you need is called "re-sampling". First thing you need to know: you can effectively reduce picture size with acceptable quality. If you want to re-sample with enlargement, don't hope for good quality. I think this is obvious.
You can do one of two things: re-sample the image in C# or use it on the page as is using the attribute
width
:
<img src="myfile.jpg" width="required width in pixels" />
In this case, the image will be re-sampled by the Web browser. The problem of this approach is that the image will be downloaded in its full size, which needs more time and uses more traffic compared to the smaller image size. Still, I think this simplest method the best in most practical cases. You might need re-sampling on the server side if the original image is really much bigger, or you show different fragment of huge picture with different resolutions (zoom feature).
For re-sampling in on the server side in C# is pretty easy with
System.Drawing
. See, for example, this code sample:
http://stackoverflow.com/questions/87753/resizing-an-image-without-losing-any-quality[
^].
[EDIT: answering a follow-up question]
Use the class
Bitmap
and its constructor
Bitmap(Stream)
. Create memory stream from your bytes using constructor
MemoryStream(Byte[])
and read bitmap from this memory stream:
Bitmap myBitmap = new Bitmap(new MemoryStream(myBytes));
See
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx[
^].
—SA