C # examples of Image File compression and cropping,

Source: Internet
Author: User

C # examples of Image File compression and cropping,

This example describes how C # compresses and crops image files, which is of great practical value in C # project development. Share it with you for your reference. The details are as follows:

Generally, image processing is performed on projects. Previously, when uploading images, the size of images is limited. This causes 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.

The procedure is as follows:

Image compression:

Step 1: read an image file, Read method:

// <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 content to the target image in the specified size..

/// <Summary> /// method 1 for generating thumbnail Overloading, returns the thumbnail's Image object /// </summary> /// <param name = "Width"> Width </param> /// <param name = "Height"> height of the thumbnail </param> /// <returns> the Image object of the thumbnail </returns> public Image GetReducedImage (int Width, int Height) {try {// use the specified size and format to initialize the Bitmap class's new instance Bitmap bitmap = new Bitmap (Width, Height, PixelFormat. format32bppArgb); // create a Graphics graphics = Graphics from the specified Image object. fromImage (bitmap); // clear the entire drawing surface and fill graphics with a transparent background color. clear (Color. transparent); // draw the original image graphics at the specified position and by the specified size. 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.

For more information about image encapsulation objects, see the official document http://msdn.microsoft.com/zh-cn/library/system.drawing.bitmap.aspx.

2. Graphics class

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

For more information about the Graphics class, click here to view the official Tutorial: http://msdn.microsoft.com/zh-cn/library/system.drawing.graphics.aspx.

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> /// method of capturing an image /// </summary> /// <param name = "url"> image address </param> /// <param name = "beginX"> Start position-X </param> // <param name = "beginY"> Start position-Y </param> // <param name = "getX"> truncation 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 (beginX, 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 is out of the image range ";}} else {return" Please confirm (beginX <getX) & (beginY <getY )";}}

Note:

Rectangle class: Rectangle, see the official documentation for details: http://msdn.microsoft.com/zh-cn/library/system.windows.shapes.rectangle (v = vs.85). aspx

The above is the sample code for cropping an image file.

The above motion graphs are provided by "Graph dado ".

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.