C # Generating image thumbnails

Source: Internet
Author: User

C # Generating image thumbnails

Recently, some projects require image operations. The general function is to save and display images. However, it is too slow to directly operate the source image. In addition, if the amount of data transmitted is too large, it may directly cause the failure to call the WCF Service.

To solve this problem, we decided to adopt the thumbnail method. Save the source image and its thumbnail when saving the data. However, when the main interface is displayed, only the thumbnail is loaded. Click the thumbnail to display the original image. This avoids loading a large amount of data as soon as you enter the main interface, improving the efficiency.

Then the problem arises. How can we convert the source image into a thumbnail?

The following provides an image processing class for generating thumbnails. The GetReducedImage function is reloaded four times to meet different requirements.

Using System. IO; using System. Drawing; using System. Drawing. Imaging ;////// Image processing class // 1. Generate a thumbnail or change the image size and image quality proportionally. // 2. Place the generated thumbnail in the specified directory ///Public class ImageClass {public Image ResourceImage; private int ImageWidth; private int ImageHeight; public string ErrMessage ;////// Constructor of the class //////
 
  
Full path name of the image filePublic ImageClass (string ImageFileName) {ResourceImage = Image. FromFile (ImageFileName); ErrMessage = "" ;}public bool ThumbnailCallback () {return false ;}///
  /// Method 1 for generating thumbnail overloading. The Image object of the thumbnail is returned //////
  
   
Width of the thumbnail///
   
    
Height of the thumbnail///
    
     
Thumbnail Image object
    Public Image GetReducedImage (int Width, int Height) {try {Image ReducedImage; Image. getThumbnailImageAbort callb = new Image. getThumbnailImageAbort (ThumbnailCallback); performancedimage = ResourceImage. getThumbnailImage (Width, Height, callb, IntPtr. zero); return ReducedImage;} catch (Exception e) {ErrMessage = e. message; return null ;}}///
    /// Method 2 for generating thumbnail overloading. Save the thumbnail file to the specified path //////
    
     
Width of the thumbnail///
     
      
Height of the thumbnail///
      
        The full file name (with Path) Saved by the thumbnail. Parameter format: D: Images ilename.jpg///
       
         True is returned. Otherwise, false is returned.
       Public bool GetReducedImage (int Width, int Height, string targetFilePath) {try {Image ReducedImage; Image. getThumbnailImageAbort callb = new Image. getThumbnailImageAbort (ThumbnailCallback); performancedimage = ResourceImage. getThumbnailImage (Width, Height, callb, IntPtr. zero); ReducedImage. save (@ targetFilePath, ImageFormat. jpeg); ReducedImage. dispose (); return true;} catch (Exception e) {ErrMessage = e. message; return false ;}}///
       /// Method 3 for generating thumbnail overloading. The Image object of the thumbnail is returned //////
       
         For example, if the width percentage of a thumbnail is 80, enter 0.8.///
        
          Thumbnail Image object
        Public Image GetReducedImage (double Percent) {try {Image cecedimage; Image. getThumbnailImageAbort callb = new Image. getThumbnailImageAbort (ThumbnailCallback); ImageWidth = Convert. toInt32 (ResourceImage. width * Percent); ImageHeight = Convert. toInt32 (ResourceImage. width * Percent); ReducedImage = ResourceImage. getThumbnailImage (ImageWidth, ImageHeight, callb, IntPtr. zero); return ReducedImage;} catch (Exception e) {ErrMessage = e. message; return null ;}}///
        /// Method 4 for generating thumbnail overloading. The Image object of the thumbnail is returned //////
        
          For example, if the width percentage of a thumbnail is 80, enter 0.8.///
         
           The full file name (with Path) Saved by the thumbnail. Parameter format: D: Images ilename.jpg///
          
            True is returned. Otherwise, false is returned.
          Public bool GetReducedImage (double Percent, string targetFilePath) {try {Image ReducedImage; Image. getThumbnailImageAbort callb = new Image. getThumbnailImageAbort (ThumbnailCallback); ImageWidth = Convert. toInt32 (ResourceImage. width * Percent); ImageHeight = Convert. toInt32 (ResourceImage. width * Percent); ReducedImage = ResourceImage. getThumbnailImage (ImageWidth, ImageHeight, callb, IntPtr. zero); ReducedImage. save (@ targetFilePath, ImageFormat. jpeg); ReducedImage. dispose (); return true;} catch (Exception e) {ErrMessage = e. message; return false ;}}}
         
        
       
      
     
    
   
  
 

:

The GetReducedImage (double Percent) method is used to test the input scale-in percentage: 0.05. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHAgbGFuZz0 = "en-US">

The size of the source image is 1 MB, and the size of the processed image is only dozens of KB. The program running efficiency has indeed improved a lot.

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.