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:
- // <Param name = "ImageFilePathAndName"> full path name of the image file </param>
- 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.
- /// <Summary>
- /// Method 1 for generating thumbnail overloading. The Image object of the thumbnail is returned.
- /// </Summary>
- /// <Param name = "Width"> Width of the thumbnail </param>
- /// <Param name = "Height"> Height of the thumbnail </param>
- /// <Returns> thumbnail Image object </returns>
- Public Image GetReducedImage (int Width, int Height)
- {
- Try
- {
- // Initialize a new Bitmap instance with the specified size and format
- Bitmap bitmap = new Bitmap (Width, Height, PixelFormat. Format32bppArgb );
- // Create a new Graphics object from the specified Image object
- Graphics graphics = Graphics. FromImage (bitmap );
- // Clear the entire drawing surface and fill it with a transparent background color
- Graphics. Clear (Color. Transparent );
- // Draw the original image object at the specified position and by the specified size
- Graphics. DrawImage (ResourceImage, new Rectangle (0, 0, Width, Height ));
- Return bitmap;
- }
- Catch (Exception e)
- {
- ErrMessage = e. Message;
- Return null;
- }
- }
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:
- 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.
- /// <Summary>
- /// Screenshot Method
- /// </Summary>
- /// <Param name = "url"> image address </param>
- /// <Param name = "beginX"> Start position-X </param>
- /// <Param name = "beginY"> Start position-Y </param>
- /// <Param name = "getX"> width </param>
- /// <Param name = "getY"> truncation length </param>
- /// <Param name = "fileName"> file name </param>
- /// <Param name = "savePath"> Save path </param>
- /// <Param name = "fileExt"> suffix </param>
- Public static string CutImage (string url, int beginX, int beginY, int getX, int getY, string fileName, string savePath, string fileExt)
- {
- If (beginX <getX) & (beginY <getY ))
- {
- Bitmap bitmap = new Bitmap (url); // source Image
- If (beginX + getX) <= bitmap. Width) & (beginY + getY) <= bitmap. Height ))
- {
- Bitmap destBitmap = new Bitmap (getX, getY); // target MAP
- Rectangle destRect = new Rectangle (0, 0, getX, getY); // Rectangular Container
- Rectangle srcRect = new Rectangle (begangle, beginY, getX, getY );
- Graphics. FromImage (destBitmap );
- Graphics. DrawImage (bitmap, destRect, srcRect, GraphicsUnit. Pixel );
- ImageFormat format = ImageFormat. Png;
- Switch (fileExt. ToLower ())
- {
- Case "png ":
- Format = ImageFormat. Png;
- Break;
- Case "bmp ":
- Format = ImageFormat. Bmp;
- Break;
- Case "gif ":
- Format = ImageFormat. Gif;
- Break;
- }
- DestBitmap. Save (savePath + "//" + fileName, format );
- Return savePath + "\" + "*" + fileName. Split ('.') [0] + "." + fileExt;
- }
- Else
- {
- Return "The screenshot range exceeds the image range ";
- }
- }
- Else
- {
- Return "Please confirm (beginX <getX) & (beginY <getY )";
- }
- }
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.