Use the System. Drawing. Image class to perform Image-related operations,

Source: Internet
Author: User
Tags bmp image

Use the System. Drawing. Image class to perform Image-related operations,

C # The Image operations are mainly performed through the System. Drawing. Image class.

1. convert an image to a byte stream
/// <Summary> /// image processing Help class /// </summary> public static class PicProcessHelper {/// <summary> // convert the image to a specified byte stream /// </summary> /// <param name = "filePath"> image path </param> /// <returns> the specified byte stream </returns> public static byte [] ConvertToByte (String filePath) {var m = new System. IO. memoryStream (); var bp = new System. drawing. bitmap (filePath); bp. save (m, System. drawing. imaging. imageFormat. jpeg); // Save the image in the specified format To the specified stream. Byte [] imgByte = m. GetBuffer (); // read return imgByte from the memory buffer ;}}
2. Transfer byte back to image
/// <Summary> /// return the Image type based on the byte stream /// </summary> /// <param name = "streamByte"> </param> /// <returns> </returns> public static Image ReturnImage (byte [] streamByte) {System. IO. memoryStream MS = new System. IO. memoryStream (streamByte); Image img = Image. fromStream (MS); return img ;}
3. convert an Image object to a byte stream
// Convert the Image into streaming data and save it as byte [] public static byte [] PhotoImageInsert (System. drawing. image imgPhoto) {MemoryStream mstream = new MemoryStream (); imgPhoto. save (mstream, System. drawing. imaging. imageFormat. bmp); byte [] byData = new Byte [mstream. length]; mstream. position = 0; mstream. read (byData, 0, byData. length); mstream. close (); return byData ;}
4. Save Images
Var oldFilename = @ "E: \ environment deployment \ image set \ panda 1.jpg"; var oldImage = System. drawing. image. fromFile (oldFilename); var newFilename = @ "E: \ my new panda .jpg"; oldImage. save (newFilename, ImageFormat. jpeg );
5. Generate a thumbnail 1 // <summary> 2 // generate an image thumbnail file 3 /// </summary> 4 /// <param name = "originalImage"> Image source File </param> 5 /// <param name = "width"> thumbnail width </param> 6 /// <param name = "height"> thumbnail height </param> 7 // <param name = "mode"> how to generate a thumbnail </param> 8 // <returns> image file after compression </returns> 9 public static image MakeThumbnail (Image originalImage, int width, int height, ThumbnailModel mode) 10 {11 int towidth = width; 12 int toheight = height; 13 14 int x = 0; 15 int y = 0; 16 int ow = originalImage. width; 17 int oh = originalImage. height; 18 19 switch (mode) 20 {21 case ThumbnailModel. highWidth: // specify high-width Scaling (may be deformed) 22 break; 23 case ThumbnailModel. width: // specify the Width. The height is proportional to 24 toheight = originalImage. height * width/originalImage. width; 25 break; 26 case ThumbnailModel. hight: // specify the height. The width is proportional to 27 towidth = originalImage. width * height/originalImage. height; 28 break; 29 case ThumbnailModel. default: // specify the height, width by proportion 30 if (ow <= towidth & oh <= toheight) 31 {32 x =-(towidth-ow)/2; 33 y =-(toheight-oh)/2; 34 ow = towidth; 35 oh = toheight; 36} 37 else 38 {39 if (ow> oh) // larger than 40 {41 x = 0; 42 y =-(ow-oh)/2; 43 oh = ow; 44} 45 else // height at width 46 {47 y = 0; 48 x =-(oh-ow)/2; 49 ow = oh; 50} 51} 52 break; 53 case ThumbnailModel. auto: 54 if (originalImage. width/originalImage. height> = width/height) 55 {56 if (originalImage. width> width) 57 {58 towidth = width; 59 toheight = (originalImage. height * width)/originalImage. width; 60} 61 else 62 {63 towidth = originalImage. width; 64 toheight = originalImage. height; 65} 66} 67 else 68 {69 if (originalImage. height> height) 70 {71 toheight = height; 72 towidth = (originalImage. width * height)/originalImage. height; 73} 74 else 75 {76 towidth = originalImage. width; 77 toheight = originalImage. height; 78} 79} 80 break; 81 case ThumbnailModel. cut: // specify high width Cut (not deformed) 82 if (double) originalImage. width/(double) originalImage. height> (double) towidth/(double) toheight) 83 {84 oh = originalImage. height; 85 ow = originalImage. height * towidth/toheight; 86 y = 0; 87 x = (originalImage. width-ow)/2; 88} 89 else 90 {91 ow = originalImage. width; 92 oh = originalImage. width * height/towidth; 93 x = 0; 94 y = (originalImage. height-oh)/2; 95} 96 break; 97 default: 98 99 break; 100} 101 102 // create a bmp image 103 System. drawing. image bitmap = new System. drawing. bitmap (towidth, toheight); 104 105 // create a canvas 106 System. drawing. graphics g = System. drawing. graphics. fromImage (bitmap); 107 108 // set a high quality interpolation method of 109 GB. interpolationMode = System. drawing. drawing2D. interpolationMode. high; 110 111 // set High quality, low speed rendering smoothness 112g. smoothingMode = System. drawing. drawing2D. smoothingMode. highQuality; 113 114 // clear the canvas and fill 115g with a transparent background color. clear (System. drawing. color. white); 116 117 // at the specified position and draw the specified part of the original image in the specified size 118 GB. drawImage (originalImage, new System. drawing. rectangle (0, 0, towidth, toheight), 119 new System. drawing. rectangle (x, y, ow, oh), 120 System. drawing. graphicsUnit. pixel); 121 122 return bitmap; 123}View Code

The thumbnail mode is defined as follows:

/// <Summary> /// scaling chart processing mode /// </summary> public enum ThumbnailModel {// <summary> /// specify high-width Scaling (may be deformed)) /// </summary> HighWidth, /// <summary> // specify the Width, and the height is proportional. /// </summary> Width, /// <summary> /// the Default full graph is not deformed /// </summary> Default, /// <summary> /// specify the height, width by proportion /// </summary> Hight, /// <summary> /// specifies the height width to be cut (not deformed )?? Specify the cropping area /// </summary> Cut, /// <summary> /// scale the original image proportionally. /// </summary> Auto}View Code

 

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.