C # A Preliminary Study on Image File compression and cropping,

Source: Internet
Author: User
Tags gety

C # A Preliminary Study on Image File compression and cropping,

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


& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

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.