Private size newsize (INT maxwidth, int maxheight, int width, int height)
{
Double W = 0.0;
Double H = 0.0;
Double Sw = convert. todouble (width );
Double SH = convert. todouble (height );
Double Mw = convert. todouble (maxwidth );
Double MH = convert. todouble (maxheight );
If (SW <MW & SH <MH)
{
W = Sw;
H = sh;
}
Else if (SW/SH)> (Mw/MH ))
{
W = maxwidth;
H = (w * Sh)/SW;
}
Else
{
H = maxheight;
W = (H * SW)/SH;
}
Return new size (convert. toint32 (W), convert. toint32 (h ));
}
Private void sendsmallimage (string filename, int maxwidth, int maxheight)
{
System. Drawing. Image IMG = system. Drawing. image. fromfile (server. mappath (filename ));
System. Drawing. imaging. imageformat thisformat = IMG. rawformat;
Size newsize = newsize (maxwidth, maxheight, IMG. Width, IMG. Height );
Bitmap outbmp = new Bitmap (newsize. Width, newsize. Height );
Graphics G = graphics. fromimage (outbmp );
// Set the painting quality of the canvas
G. compositingquality = compositingquality. highquality;
G. smoothingmode = smoothingmode. highquality;
G. interpolationmode = interpolationmode. highqualitybicubic;
G. drawimage (IMG, new rectangle (0, 0, newsize. Width, newsize. Height ),
0, 0, IMG. Width, IMG. Height, graphicsunit. pixel );
G. Dispose ();
If (thisformat. Equals (imageformat. GIF ))
{
Response. contenttype = "image/GIF ";
}
Else
{
Response. contenttype = "image/JPEG ";
}
// The followingCodeSet the compression quality when saving the image
Encoderparameters encoderparams = new encoderparameters ();
Long [] Quality = new long [1];
Quality [0] = 100;
Encoderparameter encoderparam = new encoderparameter (system. Drawing. imaging. encoder. quality, quality );
Encoderparams. Param [0] = encoderparam;
// Obtain the imagecodecinfo object that contains information about the built-in image decoder.
Imagecodecinfo [] arrayici = imagecodecinfo. getimageencoders ();
Imagecodecinfo policici = NULL;
For (INT x = 0; x <arrayici. length; X ++)
{
If (arrayici [X]. formatdescription. Equals ("Jpeg "))
{
Jpegici = arrayici [X]; // sets the JPEG encoding.
Break;
}
}
If (policici! = NULL)
{
Outbmp. Save (response. outputstream, jpegici, encoderparams );
}
Else
{
Outbmp. Save (response. outputstream, thisformat );
}
IMG. Dispose ();
Outbmp. Dispose ();
}