asp.net generate Thumbnail Example method sharing _ Practical Tips

Source: Internet
Author: User

When you do a station, you often encounter the ability to generate thumbnails, because you may need to use thumbnails of different sizes for different situations.

The images generated in this article are squares, and only a square thumbnail is enough to ensure that the picture is clear.

When I say here the square is first scaled, then a fixed white bottom and then centered.

Code:

New Outputimg.ashx

Copy Code code as follows:

Resize a picture
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)//If MaxWidth and maxheight are larger than the source image, the length and height of the thumbnail are unchanged
{
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));
}

Copy Code code as follows:

Generate thumbnails
public static void Sendsmallimage (string filename, string newfile, int maxheight, int maxwidth, string mode)
{
System.Drawing.Image img = System.Drawing.Image.FromFile (filename);//source image information
System.Drawing.Imaging.ImageFormat Thisformat = img. Rawformat; The format of the source image
Size newsize = newsize (maxwidth, MaxHeight, IMG. Width, IMG. Height); Returns the adjusted image width and height
Bitmap outbmp = new Bitmap (maxwidth, maxheight);
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.clear (Color.White);
G.drawimage (IMG, New Rectangle ((maxwidth-newsize.width)/2), ((Maxheight-newsize.height)/2), Newsize.width, Newsiz E.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 ();
}

Output Picture:

Copy Code code as follows:

Output picture
public static void Outputimg (String imgfilepath)
{
FileStream fs = new FileStream (HttpContext.Current.Server.MapPath (Imgfilepath), FileMode.Open, FileAccess.Read);
DateTime contentmodified = System.IO.File.GetLastWriteTime (HttpContext.Current.Server.MapPath (Imgfilepath));
if (isclientcached (contentmodified))
{
HttpContext.Current.Response.StatusCode = 304;
HttpContext.Current.Response.SuppressContent = true;
}
Else
{
byte[] MyData = new Byte[fs. Length];
int Length = Convert.ToInt32 (fs. Length);
Fs. Read (MyData, 0, Length);
Fs. Close ();
HttpContext.Current.Response.OutputStream.Write (MyData, 0, Length);
HttpContext.Current.Response.ContentType = "Image/jpeg";
HttpContext.Current.Response.End ();
HttpContext.Current.Response.Cache.SetETagFromFileDependencies ();
HttpContext.Current.Response.Cache.SetAllowResponseInBrowserHistory (TRUE);
HttpContext.Current.Response.Cache.SetLastModified (contentmodified);
}
}

Copy Code code as follows:

Outpuimg.ashx?src=/images/weimeidesc/8af30049-797e-4eb4-8a54-cc4de47c1694.jpg!100x100.jpg
public void ProcessRequest (HttpContext context)
{
Get Pictures
String Imgurl = context. request.querystring["src"];
String truefilepath = Imgurl.split ('! ') [0];
Get picture size
int width = Convert.ToInt32 (imgurl.split ('! ') [1]. Replace (". jpg", ""). Split (' x ') [0]);
int height = Convert.ToInt32 (imgurl.split ('! ') [1]. Replace (". jpg", ""). Split (' x ') [1]);

Picture already exists direct output
if (file.exists context. Server.MapPath ("~/" + Imgurl))
{
Outputimg ("~/" +imgurl);
}
Else
{
if (!string. IsNullOrEmpty (Imgurl) && file.exists (context. Server.MapPath ("~/" + Truefilepath))
{
Image Originalimage = System.Drawing.Image.FromFile (context. Server.MapPath ("~/" + Truefilepath));
var newbitmap = new Bitmap (originalimage);
Generate the corresponding small figure and save
Sendsmallimage (context. Server.MapPath ("~/" + Truefilepath), context. Server.MapPath ("~/" + imgurl), width, height, "Meiyouyisi");
Output
Outputimg ("~/" + Imgurl);
}
else//picture If there is no output default picture
{
Outputimg (Imgurl);
}
}
}

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.