asp.net| thumbnail private static 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));
}
public static void Sendsmallimage (String fileName, string newFile, int maxheight, int maxwidth)
{
System.Drawing.Image img = System.Drawing.Image.FromFile (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 ();
The following code sets the compression quality when saving a picture
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;
Gets the ImageCodecInfo object that contains information about the built-in image codec.
imagecodecinfo[] Arrayici = Imagecodecinfo.getimageencoders ();
ImageCodecInfo jpegici = null;
for (int x = 0; x < Arrayici.length + +)
{
if (arrayici[x). Formatdescription.equals ("JPEG"))
{
Jpegici = arrayici[x];//Set JPEG encoding
Break
}
}
if (Jpegici!= null)
{
Outbmp.save (NewFile, Jpegici, encoderparams);
}
Else
{
Outbmp.save (NewFile, Thisformat);
}
Img. Dispose ();
Outbmp.dispose ();
}