When doing Web projects, it is found that a large number of high quality picture loads severely affect performance. Then consider the image compression preprocessing, that is, high-definition pictures, such as compression into a low quality picture, when the user clicks on a picture and then load display the picture HD original image. Just beginning to use C # Graphics DrawImage Redraw method batch picture compression. However, the test found that more than 15M of the size of the picture to redraw when there is an Out-of-memory exception (of course, this exception may be different computers, perhaps some of the computer 10M pictures graphics redraw When the exception will occur). Through Google, MSDN search for a variety of ways, can not resolve graphics to large picture redraw the memory of the exception. So give up treatment, to githup, CodePlex and so on open source community to find open source image operation Library, inadvertently found open Source Library magick.net. Magick.net from the name can make people vaguely feel that this class library is not written in. Net. In fact it's a. NET package for ImageMagick, and ImageMagick is a powerful image manipulation library that supports more than 100 file formats. Like the image compression this operation, the ImageMagick handles lightly loose. Using the ImageMagick library. NET encapsulated library magick.net to generate thumbnail operations (that is, image compression) for 1GB TIF images, there is no problem. Of course, the size of 1GB TIF processing will certainly take some time, the 1GB tif map by 1% ratio, such as compression, generate a new picture probably need 2~3 clock (the length of time due to the computer configuration will be different).
Another point to use magick.net to note that the Magick.net has 64/32 bits, of course, there are 64/32-bit systems to adapt to the library. That means Magick.net has three versions, 32-bit, 64-bit, and ANDYCPU editions. You also need to pay attention to the release of resources when using. NET programmers are accustomed to handing resource recycling tasks to the. NET garbage collection mechanism for processing. In fact, for the operation of resource consumption, it is necessary to manually release resources, after all. NET garbage collection is not omnipotent, the bulk operation is likely to cause the resources can not be released in time for abnormal, serious point may burst memory. Option one: Compress pictures using magick.net (recommended)
<summary>///generate thumbnails///</summary>///<param name= "Srcfilename" > Source images File </param>///<param name= "destFileName" > Target image file </param>///<param name= "width" > Reference Fixed compression width </param>///<param name= "height" > Specify compression height </param> private void Makethumbnail (string Srcfilename, string destfilename, int width, int height) {using (magickimage image = New Magickimage ( Srcfilename)) {if (width <= 0) width = (int) math.ceiling (image. Width * Getscale (image.
Height, height)); if (height <= 0) height = (int) math.ceiling (image. Height * Getscale (image.
width, width)); Image.
Thumbnail (width, height); Image.
Write (destFileName); }///<summary>///generate thumbnails///</summary>///<param name= "Srcfil EName > Source image file </param>///<param name= "destFileName" > Target image file </param>///<param name=
"percent" > proportional compression ratio </param> private void Makethumbnail (String srcfilename, string destfilename, int percent) {using (magickimage image = new Magickimage (srcfilename)) {image.
Thumbnail (new percentage (percent)); Image.
Write (destFileName); }
}
C # magick.net Picture Compression Complete code Scenario two: Compress a picture using. NET graphics Redraw (Compress large pictures is not recommended)
#region Generate thumbnails///<summary>//////</summary>///<para M name= "Originalimagepath" > Source map Path (physical path) </param>///<param name= "Thumbnailpath" > Thumbnail path (physical path) </para m>///<param name= "width" > Thumbnail width </param>///<param name= "height" > thumbnail height </PARAM&G
T <param name= "mode" > How to generate thumbnails </param> public void Makethumbnail (string originalimagepath, Strin g Thumbnailpath, int width, int height, Thumbnailmode mode, out string outthumbnailpath) {System.draw Ing.
Image originalimage = System.Drawing.Image.FromFile (Originalimagepath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = Originalimage.width;
int oh = originalimage.height; Switch (mode) {case thumbnailmode.heightandwidth://specifies width scaling (can becan deform) break; Case thumbnailmode.width://specified wide, high proportionally toheight = Originalimage.height * Width/ori
Ginalimage.width;
Break
Case thumbnailmode.height://specified high, wide proportional towidth = Originalimage.width * height/originalimage.height;
Break Case thumbnailmode.cut://Specify width cut (not deformed) if (double) originalimage.width/(double) origin Alimage.height > (double) towidth/(double) toheight) {Oh = OriginalImage.H
eight;
ow = Originalimage.height * towidth/toheight;
y = 0;
x = (Originalimage.width-ow)/2;
else {ow = Originalimage.width; Oh = originalimage.width * HeigHt/towidth;
x = 0;
y = (Originalimage.height-oh)/2;
} break;
Default:break;
//Create a new bmp picture System.Drawing.Image bitmap = new System.Drawing.Bitmap (towidth, toheight);
Create a new artboard Graphics g = System.Drawing.Graphics.FromImage (bitmap);
Setting high quality interpolation method g.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.High;
Set high quality, low speed rendering smooth degree g.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Empty the canvas and fill the g.clear with a transparent background color (color.transparent); Draws the specified portion of the original picture at the specified location and at the specified size g.drawimage (originalimage, New Rectangle (0, 0, Towidth, toheight), new
Rectangle (x, Y, Ow, OH), graphicsunit.pixel); try {//save thumbnail bitmap in jpg format. SavE (Thumbnailpath, System.Drawing.Imaging.ImageFormat.Jpeg);
Outthumbnailpath = Thumbnailpath;
catch (System.Exception e) {throw e;
finally {originalimage.dispose (); Bitmap.
Dispose ();
G.dispose ();
} public enum Thumbnailmode {///<summary>///Specify width scaling (possibly distorted)
</summary> heightandwidth = 0,///<summary>///specified wide, high proportionally
</summary> height = 1,///<summary>///specified wide, high proportionally </summary> Width = 2,///<summary>///specifies width cut (not deformed)/
</summary> cut = 3} #endregion