Processing of image scaling and Image Scaling

Source: Internet
Author: User

Processing of image scaling and Image Scaling

Processing of Image Scaling

Recently I used Image Processing for some of my work. However, I did not find a suitable image processing algorithm, but I found several algorithms for obtaining thumbnails. The code is not long, and the principle of simple viewing is also relatively simple (in fact, it is not simple because there are too many C # packages, so it is relatively simple ). Since there is no relevant Unified Call code, simply put it out, I will sort out a set of relevant processing code.

Image Processing Analysis

In fact, the image processing I want to use is very simple, that is, simply compressing a piece. Then, set the image size to the corresponding size. However, for the setting method, the Rect range settings used in the C # basic function. This range setting may be a headache. So are you sure you want to make it easier for the caller to call the interface while ignoring the Rect parameter? Therefore, I have defined several image scaling types (Full, Zoom, Overflow, and Original ). By default, the center of the original image is aligned with the center of the output image. Then, the image is scaled in different modes based on the scaling type.

Scaling Mode

  1. Full: Give up the original proportion of the image and stretch the image directly according to the target data.
  2. Zoom: keep the original proportion of the image, and ensure that the original image is in the center of the target image, and that the original image is white.
  3. Original: keep the Original image size. The output image is similar to the image in a certain range in the middle of the cropping.
  4. Overflow: Maintain the original proportion of the image, and ensure that the object is filled with images without any blank space.

Call example

ImageHelper. GetInstance (). ImageCompress (InputFile, Path. Combine (OutPutPath, item_file), new System. Drawing. Size (500,300), ImageZoomType. Full, 50 );

Sample display

Original Image

Full 300*500

 

Full 500*300

 

Original 300*500

 

Original 500*300

 

Overflow 300*500

 

Overflow 500*300

 

Zoom 300*500

 

Zoom 500*300

Limitations

  1. There is no unique technology for compression
  2. Currently, only JPEG images are supported.
  3. After some images are scaled, a 1-pixel shallow side is left

Subsequent content

Image processing not only scales, but also many other things are more practical (compression, high scaling, wide scaling, adding watermarks, and adding hidden watermarks ), I am going to process commonly used images. It is organized into a help class to facilitate migration to various projects.

So I established a dedicated image processing open source project to facilitate subsequent functions to join, address: http://git.oschina.net/anxin1225/ImageCompress

 

Compress related Code

Using System; using System. collections. generic; using System. drawing; using System. drawing. drawing2D; using System. drawing. imaging; using System. IO; using System. linq; using System. text; using System. threading. tasks; namespace ImageCompress {public struct AnchorPoint {// public AnchorPoint (): this (0, 0) {} public AnchorPoint (double x, double y) {X = x; Y = y;} public double X, Y;} public enum ImageZoom Type {Full, Zoom, Overflow, Original,} public delegate void ImageHandle (Image image, Bitmap bitmap); public class ImageHelper {private ImageHelper () {} private static ImageHelper _ image_helper = null; private Dictionary <ImageZoomType, ImageHandle> _ name_to_handle = new Dictionary <ImageZoomType, ImageHandle> (); public static ImageHelper GetInstance () {if (_ image_helper = null) {_ image_helper = New ImageHelper (); // fill _ image_helper. _ name_to_handle [ImageZoomType. full] = (image, bitmap) => {_ image_helper.DrawImage (image, bitmap, new Rectangle (new Point (0, 0), bitmap. size) ;}; // original _ image_helper. _ name_to_handle [ImageZoomType. original] = (image, bitmap) =>{_ image_helper.DrawImage (image, bitmap, 1, new AnchorPoint (0.5, 0.5), new AnchorPoint (0.5, 0.5 ));}; // overflow _ image_helper. _ name_to_ha Ndle [ImageZoomType. overflow] = (image, bitmap) => {float proportion_x = (float) bitmap. width/image. width; float proportion_y = (float) bitmap. height/image. height; _ image_helper.DrawImage (image, bitmap, proportion_x> proportion_y? Proportion_x: proportion_y, new AnchorPoint (0.5, 0.5), new AnchorPoint (0.5, 0.5) ;}; // zoom _ image_helper. _ name_to_handle [ImageZoomType. zoom] = (image, bitmap) => {float proportion_x = (float) bitmap. width/image. width; float proportion_y = (float) bitmap. height/image. height; _ image_helper.DrawImage (image, bitmap, proportion_x <proportion_y? Proportion_x: proportion_y, new AnchorPoint (0.5, 0.5), new AnchorPoint (0.5, 0.5) ;}}return _ image_helper ;} /// <summary> /// compress the image /// </summary> /// <param name = "source_path"> source data location </param> /// <param name = "save_path"> Save Data Location </param> /// <param name = "save_size"> Save image size </param> /// <param name =" ztype "> scaling mode </param> /// <param name =" flag "> Image Storage Quality </param> /// <returns> whether compression is completed </returns> public bool Im AgeCompress (string source_path, string save_path, Size save_size, ImageZoomType ztype, int flag) {bool success = false; Image source = null; while (true) {source = LoadImage (source_path ); if (source = null) break; Bitmap bitmap = new Bitmap (save_size.Width, save_size.Height); if (_ name_to_handle.ContainsKey (ztype) {_ name_to_handle [ztype] (source, bitmap);} else {break;} success = SaveImage (Bitmap, save_path, source. RawFormat, flag); break;} if (source! = Null) source. Dispose (); return success;} public Image LoadImage (string source_path) {Image image = null; while (true) {if (! File. exists (source_path) break; try {image = Image. fromFile (source_path);} catch {break;} return image ;} /// <summary> /// Save the BitMap to the disk /// </summary> /// <param name = "image"> the image to be saved </param> /// <param name = "save_path"> saved path </param> /// <param name = "format"> saved format </param> /// <param name = "flag"> Storage Quality </param> // <returns> </returns> public bool SaveImage (Bitmap image, string save_path, ImageFormat format, int flag) {// when saving the image, set the compression quality EncoderParameters ep = new EncoderParameters (); long [] qy = new long [1]; qy [0] = flag; // set the compression ratio to 1-100 EncoderParameter eParam = new EncoderParameter (System. drawing. imaging. encoder. quality, qy); ep. param [0] = eParam; try {ImageCodecInfo [] arrayICI = ImageCodecInfo. getImageEncoders (); ImageCodecInfo effeciciinfo = null; for (int x = 0; x <arrayICI. Length; x ++) {if (arrayICI [x]. FormatDescription. Equals ("JPEG") {policiciinfo = arrayICI [x]; break ;}} if (policiciinfo! = Null) {image. save (save_path, effeciciinfo, ep); // dFile is the new path after compression} else {image. save (save_path, format);} return true;} catch {return false;} finally {image. dispose ();}} /// <summary> /// drawing /// </summary> /// <param name = "source"> original image </param> /// <param name = "output"> output image </param> /// <param name = "souce_scale"> scale the original image </param> /// <param name = "souce_anchor "> </param> /// <param name =" graphics_anchor "> </param> public void DrawImage (Image source, bitmap output, float souce_scale, AnchorPoint souce_anchor, AnchorPoint graphics_anchor) {DrawImage (source, output, souce_scale, souce_anchor, new Point (int) (output. width * graphics_anchor.X), (int) (output. height * graphics_anchor.Y )));} /// <summary> /// drawing /// </summary> /// <param name = "source"> original image </param> /// <param name = "output"> output highlight </param> /// <param name = "source_scale"> the placed value of the image </param> /// <param name = "source_anchor"> source Image anchor </param> // <param name = "souce_point"> Image Position </param> public void DrawImage (Image source, bitmap output, float source_scale, AnchorPoint source_anchor, Point souce_point) {var pic_po = new Point (int) (souce_point.X-source. size. width * source_scale * source_anchor.X), (int) (souce_point.Y-source. size. height * source_scale * source_anchor.Y); DrawImage (source, output, new Rectangle (pic_po, new Size (int) (source. width * source_scale), (int) (source. height * source_scale ))));} /// <summary> /// drawing /// </summary> /// <param name = "source"> </param> /// <param name =" output "> </param> /// <param name =" rect "> </param> public void DrawImage (Image source, bitmap output, Rectangle rect) {Graphics g = Graphics. fromImage (output); g. clear (Color. whiteSmoke); g. compositingQuality = CompositingQuality. highQuality; g. smoothingMode = SmoothingMode. highQuality; g. interpolationMode = InterpolationMode. highQualityBicubic; g. drawImage (source, rect, 0, 0, source. width, source. height, GraphicsUnit. pixel); g. dispose ();}}}

  

 

PS: for the sake of beauty, the original image in this article is not the original image, because the original image is too large. The page may slide, So I uploaded a small image. The Git image is the original image.

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.