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!