This article has shared the specific code of C# image processing for your reference. The specific content is as follows
using System; using ; using ; using ; using ; using .Drawing2D; namespace { public class ImageClass { public ImageClass() { } #region Thumbnail /// <summary> /// Generate thumbnails /// </summary> /// <param name="originalImagePath">Source map path (physical path)</param> /// <param name="thumbnailPath">ThumbnailPath (physical path)</param> /// <param name="width">Thumbnail width</param> /// <param name="height">Thumbnail height</param> /// <param name="mode">How to generate thumbnails</param> public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode) { originalImage = (originalImagePath); int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = ; int oh = ; switch (mode) { case "HW": //Specify the height and width scaling (may be deformed) break; case "W": //Specify width, height according to proportion toheight = * width / ; break; case "H": //Specify height, width according to proportion towidth = * height / ; break; case "Cut": //Specify the height and width cut (no deformation) if ((double) / (double) > (double)towidth / (double)toheight) { oh = ; ow = * towidth / toheight; y = 0; x = ( - ow) / 2; } else { ow = ; oh = * height / towidth; x = 0; y = ( - oh) / 2; } break; default: break; } // Create a new bmp image bitmap = new (towidth, toheight); // Create a new drawing board g = (bitmap); //Set high-quality interpolation method = .; //Set high quality and low speed to show smoothness = .; //Clear the canvas and fill it with transparent background color (); //Draw the specified part of the original picture at the specified position and according to the specified size (originalImage, new (0, 0, towidth, toheight), new (x, y, ow, oh), ); try { //Save thumbnails in jpg format (thumbnailPath, ); } catch ( e) { throw e; } finally { (); (); (); } } #endregion #region Picture watermark /// <summary> /// Image watermark processing method /// </summary> /// <param name="path">The image path (absolute path) that requires a watermark to be loaded</param> /// <param name="waterpath">Watermark picture (absolute path)</param> /// <param name="location">Watermark location (transmit the correct code)</param> public static string ImageWatermark(string path, string waterpath, string location) { string kz_name = (path); if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg") { DateTime time = ; string filename = "" + () + () + () + () + () + () + (); Image img = (path); Image waterimg = (waterpath); Graphics g = (img); ArrayList loca = GetLocation(location, img, waterimg); (waterimg, new Rectangle((loca[0].ToString()), (loca[1].ToString()), , )); (); (); string newpath = (path) + filename + kz_name; (newpath); (); (newpath, path, true); if ((newpath)) { (newpath); } } return path; } /// <summary> /// How to handle image watermark position /// </summary> /// <param name="location">Watermark location</param> /// <param name="img">Images that require watermarking</param> /// <param name="waterimg">Watermark picture</param> private static ArrayList GetLocation(string location, Image img, Image waterimg) { ArrayList loca = new ArrayList(); int x = 0; int y = 0; if (location == "LT") { x = 10; y = 10; } else if (location == "T") { x = / 2 - / 2; y = - ; } else if (location == "RT") { x = - ; y = 10; } else if (location == "LC") { x = 10; y = / 2 - / 2; } else if (location == "C") { x = / 2 - / 2; y = / 2 - / 2; } else if (location == "RC") { x = - ; y = / 2 - / 2; } else if (location == "LB") { x = 10; y = - ; } else if (location == "B") { x = / 2 - / 2; y = - ; } else { x = - ; y = - ; } (x); (y); return loca; } #endregion #region Text watermark /// <summary> /// Text watermark processing method /// </summary> /// <param name="path">Image path (absolute path)</param> /// <param name="size">Font size</param> /// <param name="letter">Watermark text</param> /// <param name="color">Color</param> /// <param name="location">Watermark location</param> public static string LetterWatermark(string path, int size, string letter, Color color, string location) { #region string kz_name = (path); if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg") { DateTime time = ; string filename = "" + () + () + () + () + () + () + (); Image img = (path); Graphics gs = (img); ArrayList loca = GetLocation(location, img, size, ); Font font = new Font("Song-style", size); Brush br = new SolidBrush(color); (letter, font, br, (loca[0].ToString()), (loca[1].ToString())); (); string newpath = (path) + filename + kz_name; (newpath); (); (newpath, path, true); if ((newpath)) { (newpath); } } return path; #endregion } /// <summary> /// Method of text watermark position /// </summary> /// <param name="location">location code</param> /// <param name="img">Image object</param> /// <param name="width">Width (when the watermark type is text, the size of the font is transmitted)</param> /// <param name="height">High (when the watermark type is text, the length of the character is transmitted)</param> private static ArrayList GetLocation(string location, Image img, int width, int height) { #region ArrayList loca = new ArrayList(); //Define the array storage location float x = 10; float y = 10; if (location == "LT") { (x); (y); } else if (location == "T") { x = / 2 - (width * height) / 2; (x); (y); } else if (location == "RT") { x = - width * height; } else if (location == "LC") { y = / 2; } else if (location == "C") { x = / 2 - (width * height) / 2; y = / 2; } else if (location == "RC") { x = - height; y = / 2; } else if (location == "LB") { y = - width - 5; } else if (location == "B") { x = / 2 - (width * height) / 2; y = - width - 5; } else { x = - width * height; y = - width - 5; } (x); (y); return loca; #endregion } #endregion #region Adjust light and darkness /// <summary> /// Adjust the light and dark /// </summary> /// <param name="mybm">original picture</param> /// <param name="width">Length of the original image</param> /// <param name="height">height of the original picture</param> /// <param name="val">Increased or decreased light and dark value</param> public Bitmap LDPic(Bitmap mybm, int width, int height, int val) { Bitmap bm = new Bitmap(width, height);//Initialize a processed image object int x, y, resultR, resultG, resultB;//x and y are the number of cycles, and the next three are the three values of red, green and blue. Color pixel; for (x = 0; x < width; x++) { for (y = 0; y < height; y++) { pixel = (x, y);//Get the current pixel value resultR = + val;//Check if the red value exceeds [0, 255] resultG = + val;//Check whether the green value will exceed [0, 255] resultB = + val;//Check whether the blue value will exceed [0, 255] (x, y, (resultR, resultG, resultB));//Drawing } } return bm; } #endregion #region color reverse processing /// <summary> /// Inverse color processing /// </summary> /// <param name="mybm">original picture</param> /// <param name="width">Length of the original image</param> /// <param name="height">height of the original picture</param> public Bitmap RePic(Bitmap mybm, int width, int height) { Bitmap bm = new Bitmap(width, height);//Initialize an object of the image processed after recording int x, y, resultR, resultG, resultB; Color pixel; for (x = 0; x < width; x++) { for (y = 0; y < height; y++) { pixel = (x, y);//Get the pixel value of the current coordinate resultR = 255 - ;//Reverse Red resultG = 255 - ;//Anti-green resultB = 255 - ;//Anti-blue (x, y, (resultR, resultG, resultB));//Drawing } } return bm; } #endregion #region relief processing /// <summary> /// Relief treatment /// </summary> /// <param name="oldBitmap">original picture</param> /// <param name="Width">Length of the original picture</param> /// <param name="Height">height of the original picture</param> public Bitmap FD(Bitmap oldBitmap, int Width, int Height) { Bitmap newBitmap = new Bitmap(Width, Height); Color color1, color2; for (int x = 0; x < Width - 1; x++) { for (int y = 0; y < Height - 1; y++) { int r = 0, g = 0, b = 0; color1 = (x, y); color2 = (x + 1, y + 1); r = ( - + 128); g = ( - + 128); b = ( - + 128); if (r > 255) r = 255; if (r < 0) r = 0; if (g > 255) g = 255; if (g < 0) g = 0; if (b > 255) b = 255; if (b < 0) b = 0; (x, y, (r, g, b)); } } return newBitmap; } #endregion #region Stretching pictures /// <summary> /// Stretch picture /// </summary> /// <param name="bmp">original picture</param> /// <param name="newW">New Width</param> /// <param name="newH">New height</param> public static Bitmap ResizeImage(Bitmap bmp, int newW, int newH) { try { Bitmap bap = new Bitmap(newW, newH); Graphics g = (bap); = .; (bap, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, , ), ); (); return bap; } catch { return null; } } #endregion #region Color filtering /// <summary> /// Color filtering /// </summary> /// <param name="mybm">original picture</param> /// <param name="width">Length of the original image</param> /// <param name="height">height of the original picture</param> public Bitmap FilPic(Bitmap mybm, int width, int height) { Bitmap bm = new Bitmap(width, height);//Initialize an image object that records the color filter effect int x, y; Color pixel; for (x = 0; x < width; x++) { for (y = 0; y < height; y++) { pixel = (x, y);//Get the pixel value of the current coordinate (x, y, (0, , ));//Drawing } } return bm; } #endregion #region Flip left and right /// <summary> /// Flip left and right /// </summary> /// <param name="mybm">original picture</param> /// <param name="width">Length of the original image</param> /// <param name="height">height of the original picture</param> public Bitmap RevPicLR(Bitmap mybm, int width, int height) { Bitmap bm = new Bitmap(width, height); int x, y, z; //x,y is the number of loops, z is used to record the change of x coordinates of pixel points. Color pixel; for (y = height - 1; y >= 0; y--) { for (x = width - 1, z = 0; x >= 0; x--) { pixel = (x, y);//Get the current pixel value (z++, y, (, , ));//Drawing } } return bm; } #endregion #region Flip up and down /// <summary> /// Flip up and down /// </summary> /// <param name="mybm">original picture</param> /// <param name="width">Length of the original image</param> /// <param name="height">height of the original picture</param> public Bitmap RevPicUD(Bitmap mybm, int width, int height) { Bitmap bm = new Bitmap(width, height); int x, y, z; Color pixel; for (x = 0; x < width; x++) { for (y = height - 1, z = 0; y >= 0; y--) { pixel = (x, y);//Get the current pixel value (x, z++, (, , ));//Drawing } } return bm; } #endregion #region compressed images /// <summary> /// Compress to specified size /// </summary> /// <param name="oldfile">original file</param> /// <param name="newfile">New file</param> public bool Compress(string oldfile, string newfile) { try { img = (oldfile); thisFormat = ; Size newSize = new Size(100, 125); Bitmap outBmp = new Bitmap(, ); Graphics g = (outBmp); = ; = ; = ; (img, new Rectangle(0, 0, , ), 0, 0, , , ); (); EncoderParameters encoderParams = new EncoderParameters(); long[] quality = new long[1]; quality[0] = 100; EncoderParameter encoderParam = new EncoderParameter(, quality); [0] = encoderParam; ImageCodecInfo[] arrayICI = (); ImageCodecInfo jpegICI = null; for (int x = 0; x < ; x++) if (arrayICI[x].("JPEG")) { jpegICI = arrayICI[x]; //Set JPEG encoding break; } (); if (jpegICI != null) (newfile, ); (); return true; } catch { return false; } } #endregion #region Grayscale image public Color Gray(Color c) { int rgb = Convert.ToInt32((double)(((0.3 * ) + (0.59 * )) + (0.11 * ))); return (rgb, rgb, rgb); } #endregion #region Convert to black and white pictures /// <summary> /// Convert to black and white pictures /// </summary> /// <param name="mybt">Pictures to be processed</param> /// <param name="width">Length of the picture</param> /// <param name="height">The height of the picture</param> public Bitmap BWPic(Bitmap mybm, int width, int height) { Bitmap bm = new Bitmap(width, height); int x, y, result; //x,y is the number of loops, result is the pixel value after recording processing Color pixel; for (x = 0; x < width; x++) { for (y = 0; y < height; y++) { pixel = (x, y);//Get the pixel value of the current coordinate result = ( + + ) / 3;//Please take the average value of red, green and blue colors (x, y, (result, result, result)); } } return bm; } #endregion #region Gets frames in the picture /// <summary> /// Get the frames in the picture /// </summary> /// <param name="pPath">Picture Path</param> /// <param name="pSavePath">SavePath</param> public void GetFrames(string pPath, string pSavedPath) { Image gif = (pPath); FrameDimension fd = new FrameDimension([0]); int count = (fd); //Get the number of frames (gif pictures may contain multiple frames, and other format pictures are generally only one frame) for (int i = 0; i < count; i++) //Save each frame in Jpeg format { (fd, i); (pSavedPath + "\\frame_" + i + ".jpg", ); } } #endregion } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.