asp.net generates high-quality thumbnail code

Source: Internet
Author: User
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 ();
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.