Asp.net Quick Generation clear thumbnail Solution

Source: Internet
Author: User

You may often encounter the function of generating thumbnails when creating a website, because thumbnails of different sizes may be used in different situations.

The image generated in this article is square. Only the thumbnail of the square ensures that the image is clear enough.

When the square I mentioned here is first compressed proportionally, then a fixed white background is added and then displayed in the center.

Now let's talk about the Code:

Create outputimg. ashx

    Size NewSize( maxWidth,  maxHeight,  Width,                w =               h =               sw =              sh =              mw =              mh =               (sw < mw && sh < mh)                  w =                 h =                ((sw / sh) > (mw /                  w =                 h = (w * sh) /                                h =                 w = (h * sw) /                        }
     SendSmallImage( filename,  newfile,  maxHeight,  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 =              Graphics g =                          g.CompositingQuality =             g.SmoothingMode =             g.InterpolationMode =              g.DrawImage(img,  Rectangle(((maxWidth - newSize.Width) / ), ((maxHeight - newSize.Height) / ), newSize.Width, newSize.Height), ,                            EncoderParameters encoderParams =              [] quality =  [             quality[] =              EncoderParameter encoderParam =              encoderParams.Param[] =                          ImageCodecInfo[] arrayICI =             ImageCodecInfo jpegICI =               ( x = ; x < arrayICI.Length; x++                   (arrayICI[x].FormatDescription.Equals(                      jpegICI = arrayICI[x];                                     (jpegICI !=                               }

Output image:

// Output image 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. status Code = 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 );}}

  

                      imgUrl = context.Request.QueryString[ trueFilePath = imgUrl.Split()[             width = Convert.ToInt32(imgUrl.Split()[].Replace(, ).Split()[ height = Convert.ToInt32(imgUrl.Split()[].Replace(, ).Split()[             (File.Exists(context.Server.MapPath( ++ (!.IsNullOrEmpty(imgUrl) && File.Exists(context.Server.MapPath( += System.Drawing.Image.FromFile(context.Server.MapPath( + newBitmap =                     SendSmallImage(context.Server.MapPath( + trueFilePath),context.Server.MapPath( + imgUrl), width, height,                     OutPutImg( +

The above is all the code. For the effect, see:

Http://meiyouyisi.com/Right-click the 100x100 next to the image in the new tab and you can change it to the appropriate image size.

I didn't judge whether to generate too many thumbnails. Thank you!

Related Article

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.