C # Initial Exploration of Image File compression and cropping

Source: Internet
Author: User

In the past, image processing was performed during project uploading and its size was limited, which caused a lot of inconvenience. After all, website O & M personnel may not necessarily process images, which often exceed the size limit. Even if image processing software is used, the processing effect may be unsatisfactory for personal reasons.

Therefore, we use the image editing function provided by C # To achieve one-stop upload and generate the desired size and size of the target image through the program.

Go to the topic -->

Image Compression

Step 1: read an image file. The read method is as follows:

  1. // <Param name = "imagefilepathandname"> full path name of the image file </param>
  2. Public Image resourceimage = image. fromfile (imagefilepathandname );

Note:

Image class: referenced from system. Drawing. It is an abstract base class that provides functions for Classes originating from Bitmap and Metafile.

Main attribute: Size-> obtains the width and height of the image in pixels.

Physicaldimension-> get the width and height of the image (if the image is a bitmap, return the width and height in pixels. If the image is a Metafile, the width and height are returned in 0.01mm units .).

Pixelformat-> get the pixel format of this image.

Height, width-> get the height and width (in pixels) of the image ).

Main method: fromfile (string)-> create an image from the specified file.

Fromstream (Stream)-> creates an image from the specified data stream.

Save (string filename)-> Save the image to the specified file or stream.

Save (stream, imageformat)-> Save the image to the specified stream in the specified format.

Save (string, imageformat)-> Save the image to the specified file in the specified format.

For more information about attributes and methods, click.

Step 2: generate a thumbnail and draw the source image in the specified size to the target image.

  1. /// <Summary>
  2. /// Method 1 for generating thumbnail overloading. The image object of the thumbnail is returned.
  3. /// </Summary>
  4. /// <Param name = "width"> width of the thumbnail </param>
  5. /// <Param name = "height"> height of the thumbnail </param>
  6. /// <Returns> thumbnail image object </returns>
  7. Public Image getreducedimage (INT width, int height)
  8. {
  9. Try
  10. {
  11. // Initialize a new bitmap instance with the specified size and format
  12. Bitmap bitmap = new Bitmap (width, height, pixelformat. format32bppargb );
  13. // Create a new graphics object from the specified image object
  14. Graphics graphics = graphics. fromimage (Bitmap );
  15. // Clear the entire drawing surface and fill it with a transparent background color
  16. Graphics. Clear (color. Transparent );
  17. // Draw the original image object at the specified position and by the specified size
  18. Graphics. drawimage (resourceimage, new rectangle (0, 0, width, height ));
  19. Return bitmap;
  20. }
  21. Catch (exception E)
  22. {
  23. Errmessage = E. message;
  24. Return NULL;
  25. }
  26. }

 

Note: 1. Bitmap class

The image is referenced from system. Drawing and encapsulates the GDI + bitmap. This bitmap consists of the pixel data of the image and its features. Bitmap is an object used to process images defined by pixel data.

(Encapsulate the image object). Click here for details.

2. graphics class

Referenced from system. Drawing (the object that processes the image), encapsulates a GDI + drawing.

Click here for details.

Step 3: Save

The image object returned in step 2 is temporarily named as: iimage:

  1. Iimage. Save (pathandname, system. Drawing. imaging. imageformat. JPEG );

 

The above is the compression operation. In the following experiment, the image size of KB is 57 KB after compression. This should be related to the size.

The following is the image Cropping Mechanism, which is similar to the above principle. It is nothing more than re-painting the image.

  1. /// <Summary>
  2. /// Screenshot Method
  3. /// </Summary>
  4. /// <Param name = "url"> image address </param>
  5. /// <Param name = "beginx"> Start position-x </param>
  6. /// <Param name = "beginy"> Start position-Y </param>
  7. /// <Param name = "getx"> width </param>
  8. /// <Param name = "Gety"> truncation length </param>
  9. /// <Param name = "FILENAME"> file name </param>
  10. /// <Param name = "savepath"> Save path </param>
  11. /// <Param name = "fileext"> suffix </param>
  12. Public static string cutimage (string URL, int beginx, int beginy, int getx, int Gety, string filename, string savepath, string fileext)
  13. {
  14. If (beginx <getx) & (beginy <Gety ))
  15. {
  16. Bitmap bitmap = new Bitmap (URL); // source Image
  17. If (beginx + getx) <= bitmap. width) & (beginy + Gety) <= bitmap. Height ))
  18. {
  19. Bitmap destbitmap = new Bitmap (getx, Gety); // target MAP
  20. Rectangle destrect = new rectangle (0, 0, getx, Gety); // Rectangular Container
  21. Rectangle srcrect = new rectangle (begangle, beginy, getx, Gety );
  22. Graphics. fromimage (destbitmap );
  23. Graphics. drawimage (bitmap, destrect, srcrect, graphicsunit. pixel );
  24. Imageformat format = imageformat. PNG;
  25. Switch (fileext. tolower ())
  26. {
  27. Case "PNG ":
  28. Format = imageformat. PNG;
  29. Break;
  30. Case "BMP ":
  31. Format = imageformat. BMP;
  32. Break;
  33. Case "GIF ":
  34. Format = imageformat. gif;
  35. Break;
  36. }
  37. Destbitmap. Save (savepath + "//" + filename, format );
  38. Return savepath + "\" + "*" + filename. Split ('.') [0] + "." + fileext;
  39. }
  40. Else
  41. {
  42. Return "The screenshot range exceeds the image range ";
  43. }
  44. }
  45. Else
  46. {
  47. Return "Please confirm (beginx <getx) & (beginy <Gety )";
  48. }
  49. }

 

Note:
Rectangle class: rectangle. Click here for details.
The above is the sample code for cropping an image file.

The code used in this article is the real code in the project and has been tested.

I hope to share my learning experience with you.

Original address http://www.cnblogs.com/xyang/archive/2013/02/25/2932145.html

C # Initial Exploration of Image File compression and cropping

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.