On the compression and cropping of picture files in C #

Source: Internet
Author: User
Tags gety

In the project, the processing of the picture, used in the upload, limit the size of the way, this brings many inconvenience. After all, the site operators do not necessarily do the image processing, often beyond the size limit, even if the use of image processing software, but also due to personal level reasons, the processing effect is not satisfactory.

So the use of C # for us to provide image editing capabilities, to achieve a one-stop upload, through the program to generate the desired size, size of the target image.

Go to the Chase--

First, picture compression.

The first step: need to read a picture file, read the method:

<param name= "Imagefilepathandname" > Picture file full path name </param>
Public Image resourceimage =image.fromfile (imagefilepathandname);

Description

Image class: An abstract base class that references self-system.drawing and provides functionality for classes originating from Bitmap and Metafile.

The main property:size-> gets the width and height of this image in pixels.

   physicaldimension-> Get the width and height of this image ( If the image is a bitmap, returns the width and height in pixels. If the image is a metafile, the width and height are returned in 0.01 millimeters. )。

Pixelformat-> gets The pixel format of this Image.

Height, width-> gets the height, width, in pixels, of this Image.

Main method: FromFile (String), creates an Image from the specified file .

FromStream (Stream) creates an Image from the specified data stream .

Save (String FileName), saves the Image to the specified file or stream.

Save (Stream, ImageFormat) saves this image to the specified stream in the specified format.

Save (String, ImageFormat) saves this Image to the specified file in the specified format.

For more property and method descriptions, click.

In the second step, the thumbnail is generated and the original content is drawn to the target image at the specified size.

//<summary>//Generate thumbnail Reload Method 1, return the thumbnail image object///</summary>//<param name= "Width" > Thumbnail width </param>//<param name= "height" > Thumbnail height </param>///<returns> thumbnail Image Object &                Lt;/returns> public Image getreducedimage (int Width, int Height) {try {                Initializes a new instance of the Bitmap class with the specified size and format Bitmap Bitmap = Bitmap (Width, Height, Pixelformat.format32bppargb);                Creates a new graphics object from the specified image object graphics graphics = graphics.fromimage (bitmap); Clears the entire drawing surface and fills the graphics with a transparent background color.                Clear (color.transparent); Draws the original picture object graphics at the specified position and at the specified size.                DrawImage (Resourceimage, New Rectangle (0, 0, Width, Height));            return bitmap;                } catch (Exception e) {errmessage = E.message;            return null; }        }

Description: 1, Bitmap class

Referenced from System.Drawing, encapsulates a GDI + bitmap that consists of pixel data for a graphical image and its properties. Bitmap is an object that is used to process images defined by pixel data.

(The object that encapsulates the image), please click here for more information.

2. Graphics class

Reference from System.Drawing,(the object that processes the image), encapsulates a GDI + drawing surface.

Please click here for details.

The third step, save

The image object returned in the second step is temporarily named: IImage:

Iimage.save (Pathandname, System.Drawing.Imaging.ImageFormat.Jpeg);

The above is the compression operation, did the next experiment, 101k picture, after compression is 57k. This should be a matter of size.

The following is the picture clipping, in fact the principle and the above similar, nothing more than to redraw the picture operation.

<summary>///Capture Image 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" > Intercept width </param>//<param name= "GetY" > Intercept length </param>//<par Am Name= "fileName" > File name </param>//<param name= "Savepath" > Save path </param>//<param N Ame= "Fileext" > Suffix name </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);//Original
if ((BeginX + GetX) <= bitmap. Width) && ((BeginY + GetY) <= bitmap. Height) {Bitmap destbitmap = new Bitmap (GetX, GetY);//target graph
Rectangle destrect = new Rectangle (0, 0, GetX, GetY);//Rectangle container Rectangle srcrect = new Rectangle (BeginX, b Eginy, GetX, GetY); Graphics.fromimage (DESTBITMAP);
            Graphics.DrawImage (Bitmap, Destrect, Srcrect, GraphicsUnit.Pixel); imageformat format = imageformat.png; Switch (Fileext.tolower ()) {case "png": Format = Ima Geformat.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 ' intercept range beyond image range '; }} else {return "please confirm (BeginX < GetX) && (BeginY < GetY)"; } }

Description
Rectangle class: Rectangle, please click here for details.
The above is a sample code to crop a picture file.

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

Hope to communicate with you to learn together.

On the compression and cropping of picture files in C #

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.