C # Generating image thumbnails (two ideas ),

Source: Internet
Author: User
Tags bmp image

C # Generating image thumbnails (two ideas ),

In daily Image Browsing, if there are too many images, only one picture can be opened to know the image content. Obviously, browsing is inconvenient. Windows provides the thumbnail function when browsing images, which greatly facilitates the viewer to understand the content of each image. This example uses two methods to achieve the same features as Windows thumbnails.

First: generate the MakeThumbnail Method

1 //// <summary> 2 // generate a thumbnail 3 /// </summary> 4 /// <param name = "originalImagePath"> source image path (physical path) </param> 5 // <param name = "thumbnailPath"> thumbnail path (physical path) </param> 6 /// <param name = "width"> thumbnail width </param> 7 /// <param name = "height"> thumbnail height </param> 8 // <param name = "mode"> how to generate a thumbnail </param> 9 public static void MakeThumbnail (string originalImagePath, string thumbnailPath, int width, int height, string mode) 10 {11 Image originalImage = Image. fromFile (originalImagePath); 12 13 int towidth = width; 14 int toheight = height; 15 16 int x = 0; 17 int y = 0; 18 int ow = originalImage. width; 19 int oh = originalImage. height; 20 21 switch (mode) 22 {23 case "HW": // specify the Height and width for Scaling (may be deformed) 24 break; 25 case "W": // specify the width, high proportion 26 toheight = originalImage. height * width/originalImage. width; 27 break; 28 case "H": // specify the height. The Width is proportional to 29 towidth = originalImage. width * height/originalImage. height; 30 break; 31 case "Cut": // specify the Height and width (not deformed) 32 if (double) originalImage. width/(double) originalImage. height> (double) towidth/(double) toheight) 33 {34 oh = originalImage. height; 35 ow = originalImage. height * towidth/toheight; 36 y = 0; 37 x = (originalImage. width-ow)/2; 38} 39 else 40 {41 ow = originalImage. width; 42 oh = originalImage. width * height/towidth; 43 x = 0; 44 y = (originalImage. height-oh)/2; 45} 46 break; 47 default: 48 break; 49} 50 51 // create a bmp Image 52 Image bitmap = new System. drawing. bitmap (towidth, toheight); 53 54 // create a drawing board 55 Graphics g = System. drawing. graphics. fromImage (bitmap); 56 57 // set high quality Interpolation Method 58g. interpolationMode = System. drawing. drawing2D. interpolationMode. high; 59 60 // set High quality, low speed rendering smoothness 61 GB. smoothingMode = System. drawing. drawing2D. smoothingMode. highQuality; 62 63 // clear the canvas and fill 64 GB with a transparent background color. clear (Color. transparent); 65 66 // at the specified position and draw the specified part of the original image in the specified size 67 GB. drawImage (originalImage, new Rectangle (0, 0, towidth, toheight), new Rectangle (x, y, ow, oh), GraphicsUnit. pixel); 68 try 69 {70 // Save the thumbnail 71 bitmap in jpg format. save (thumbnailPath, System. drawing. imaging. imageFormat. jpeg); 72} 73 catch (System. exception e) 74 {75 throw e; 76} 77 finally 78 {79 originalImage. dispose (); 80 bitmap. dispose (); 81g. dispose (); 82} 83} 84 85 ThumbnailClassMakeThumbnail

Type 2: Write ThumbnailClass class, write four overload methods, and directly return Image objects, generate thumbnails, and save them to the specified directory.

1 using System. IO; 2 using System. drawing; 3 using System. drawing. imaging; 4 5 /// <summary> 6 /// image processing Class 7 /// 1. Create a thumbnail or change the image size and image quality proportionally. 8 // 2. 10 public class ImageClass 11 {12 public Image ResourceImage; 13 private int ImageWidth; 14 private int ImageHeight; 15 16 public string ErrMessage; 17 18 /// <summary> 19 // class constructor 20 /// </summary> 21 /// <param name = "ImageFileName"> full image file path Name </param> 22 public ImageClass (string ImageFileName) 23 {24 ResourceImage = Image. fromFile (ImageFileName); 25 ErrMessage = ""; 26} 27 28 public bool ThumbnailCallback () 29 {30 return false; 31} 32 33 // <summary> 34 // method 1 for generating thumbnail Overloading, returns the thumbnail's Image object 35 /// </summary> 36 /// <param name = "Width"> Width of the thumbnail </param> 37 /// <param name = "Height"> thumbnail Height </param> 38 // <returns> the thumbnail's Image object </returns> 39 public Image GetReducedImage (int Width, int Height) 40 {41 try 42 {43 Image ReducedImage; 44 45 Image. getThumbnailImageAbort callb = new Image. getThumbnailImageAbort (ThumbnailCallback); 46 47 performancedimage = ResourceImage. getThumbnailImage (Width, Height, callb, IntPtr. zero); 48 49 return ReducedImage; 50} 51 catch (Exception e) 52 {53 ErrMessage = e. message; 54 return null; 55} 56} 57 58 // <summary> 59 // method 2 for generating thumbnail Overloading, save the thumbnail file to the specified path 60 // </summary> 61 // <param name = "Width"> Width of the thumbnail </param> 62 // <param name = "Height"> Height of the thumbnail </param> 63 // <param name = "targetFilePath"> the full file name saved by the thumbnail, (With path), parameter format: D: \ Images \ filename.jpg </param> 64 // <returns> success returns true, otherwise, false is returned. </returns> 65 public bool GetReducedImage (int Width, int Height, string targetFilePath) 66 {67 try 68 {69 Image ReducedImage; 70 71 Image. getThumbnailImageAbort callb = new Image. getThumbnailImageAbort (ThumbnailCallback); 72 73 performancedimage = ResourceImage. getThumbnailImage (Width, Height, callb, IntPtr. zero); 74 cecedimage. save (@ targetFilePath, ImageFormat. jpeg); 75 76 ReducedImage. dispose (); 77 78 return true; 79} 80 catch (Exception e) 81 {82 ErrMessage = e. message; 83 return false; 84} 85} 86 87 // <summary> 88 // method 3 for generating thumbnail Overloading, return the thumbnail's Image object 89 /// </summary> 90 /// <param name = "Percent"> the width percentage of the thumbnail, for example, 80%, enter the 0.8 </param> 91 // <returns> thumbnail Image object </returns> 92 public Image GetReducedImage (double Percent) 93 {94 try 95 {96 Image ReducedImage; 97 98 Image. getThumbnailImageAbort callb = new Image. getThumbnailImageAbort (ThumbnailCallback); 99 100 ImageWidth = Convert. toInt32 (ResourceImage. width * Percent); 101 ImageHeight = Convert. toInt32 (ResourceImage. width * Percent); 102 103 ReducedImage = ResourceImage. getThumbnailImage (ImageWidth, ImageHeight, callb, IntPtr. zero); 104 105 return ReducedImage; 106} 107 catch (Exception e) 108 {109 ErrMessage = e. message; 110 return null; 111} 112} 113 114 // <summary> 115 // Method 4 for generating thumbnail Overloading, returns the thumbnail's Image object 116 /// </summary> 117 /// <param name = "Percent"> the width percentage of the thumbnail, for example, 80%, enter 0.8 </param> 118 // <param name = "targetFilePath"> the full file name saved by the thumbnail (with Path). Parameter format: D: \ Images \ filename.jpg </param> 119 // <returns> success returns true; otherwise, false is returned. </returns> 120 public bool GetReducedImage (double Percent, string targetFilePath) 121 {122 try123 {124 Image performancedimage; 125 126 Image. getThumbnailImageAbort callb = new Image. getThumbnailImageAbort (ThumbnailCallback); 127 128 ImageWidth = Convert. toInt32 (ResourceImage. width * Percent); 129 ImageHeight = Convert. toInt32 (ResourceImage. width * Percent); 130 131 ReducedImage = ResourceImage. getThumbnailImage (ImageWidth, ImageHeight, callb, IntPtr. zero); 132 133 ReducedImage. save (@ targetFilePath, ImageFormat. jpeg); 134 135 ReducedImage. dispose (); 136 137 return true; 138} 139 catch (Exception e) 140 {141 ErrMessage = e. message; 142 return false; 143} 144} 145}ThumbnailClass

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.