This article describes the method of C# using the overlap of the original image and the watermark image to simply implement watermark. Share it for your reference, as follows:
Image operation category
/// <summary> /// Get the size of a picture after being reduced in proportion./// </summary> /// <param name="maxWidth">Width to be reduced to</param>/// <param name="maxHeight">height to be reduced to</param>/// <param name="imageOriginalWidth">Original width of the picture</param>/// <param name="imageOriginalHeight">Print height of the picture</param>/// <returns>Return the actual size of the picture after it is reduced in proportion</returns>public static Size GetNewSize(int maxWidth, int maxHeight, int imageOriginalWidth, int imageOriginalHeight) { double w = 0.0; double h = 0.0; double sw = (imageOriginalWidth); double sh = (imageOriginalHeight); double mw = (maxWidth); double mh = (maxHeight); if (sw < mw && sh < mh) { w = sw; h = sh; } else if ((sw / sh) > (mw / mh)) { w = maxWidth; h = (w * sh) / sw; } else { h = maxHeight; w = (h * sw) / sh; } return new Size(Convert.ToInt32(w), Convert.ToInt32(h)); } /// <summary> /// Generate a thumbnail of the specified size for a given image (Image object)./// </summary> /// <param name="originalImage">original image</param>/// <param name="thumMaxWidth">Width of thumbnail</param>/// <param name="thumMaxHeight">Height of thumbnail</param>/// <returns>Returns Image object to the thumbnail</returns>public static GetThumbNailImage( originalImage, int thumMaxWidth, int thumMaxHeight) { Size thumRealSize = ; newImage = originalImage; Graphics graphics = null; try { thumRealSize = GetNewSize(thumMaxWidth, thumMaxHeight, , ); newImage = new Bitmap(, ); graphics = (newImage); = ; = ; = ; (); (originalImage, new Rectangle(0, 0, , ), new Rectangle(0, 0, , ), ); } catch { } finally { if (graphics != null) { (); graphics = null; } } return newImage; } /// <summary> /// Generate a thumbnail of the specified size for a given image file./// </summary> /// <param name="originalImage">Physical file address of the picture</param>/// <param name="thumMaxWidth">Width of thumbnail</param>/// <param name="thumMaxHeight">Height of thumbnail</param>/// <returns>Returns Image object to the thumbnail</returns>public static GetThumbNailImage(string imageFile, int thumMaxWidth, int thumMaxHeight) { originalImage = null; newImage = null; try { originalImage = (imageFile); newImage = GetThumbNailImage(originalImage, thumMaxWidth, thumMaxHeight); } catch { } finally { if (originalImage != null) { (); originalImage = null; } } return newImage; } /// <summary> /// Generate a thumbnail of the specified size for a given image file and save the thumbnail to the specified location./// </summary> /// <param name="originalImageFile">Physical file address of the picture</param>/// <param name="thumbNailImageFile">Physical file address of thumbnail</param>/// <param name="thumMaxWidth">Width of thumbnail</param>/// <param name="thumMaxHeight">Height of thumbnail</param>public static void MakeThumbNail(string originalImageFile, string thumbNailImageFile, int thumMaxWidth, int thumMaxHeight) { newImage = GetThumbNailImage(originalImageFile, thumMaxWidth, thumMaxHeight); try { (thumbNailImageFile, ); } catch { } finally { (); newImage = null; } } /// <summary> /// Resizes the memory stream of an image to a specified size and returns the adjusted memory stream./// </summary> /// <param name="originalImageStream">Memory Stream of the Original Image</param>/// <param name="newWidth">Width of new picture</param>/// <param name="newHeight">Height of new picture</param>/// <returns>Returns the memory flow of the adjusted image</returns>public static MemoryStream ResizeImage(Stream originalImageStream, int newWidth, int newHeight) { MemoryStream newImageStream = null; newImage = ((originalImageStream), newWidth, newHeight); if (newImage != null) { newImageStream = new MemoryStream(); (newImageStream, ); } return newImageStream; } /// <summary> /// Save a memory stream as a disk file./// </summary> /// <param name="stream">Memory Stream</param>/// <param name="newFile">Target disk file address</param>public static void SaveStreamToFile(Stream stream, string newFile) { if (stream == null || == 0 || (newFile)) { return; } byte[] buffer = new byte[]; = 0; (buffer, 0, ); FileStream fileStream = new FileStream(newFile, , ); (buffer, 0, ); (); (); (); } /// <summary> /// Add image watermark effect to a specified image./// </summary> /// <param name="imageFile">Image file address</param>/// <param name="waterImage">Watermark image (Image object)</param>public static void CreateImageWaterMark(string imageFile, waterImage) { if ((imageFile) || !(imageFile) || waterImage == null) { return; } originalImage = (imageFile); if ( - 10 < || - 10 < ) { return; } Graphics graphics = (originalImage); int x = - - 10; int y = - - 10; int width = ; int height = ; (waterImage, new Rectangle(x, y, width, height), 0, 0, width, height, ); (); MemoryStream stream = new MemoryStream(); (stream, ); (); imageWithWater = (stream); (imageFile); (); } /// <summary> /// Add text watermark effect to a specified image./// </summary> /// <param name="imageFile">Image file address</param>/// <param name="waterText">Watermark text content</param>public static void CreateTextWaterMark(string imageFile, string waterText) { if ((imageFile) || (waterText) || !(imageFile)) { return; } originalImage = (imageFile); Graphics graphics = (originalImage); = ; = ; = ; = ; SolidBrush brush = new SolidBrush((153, 255, 255, 255)); Font waterTextFont = new Font("Arial", 16, ); SizeF waterTextSize = (waterText, waterTextFont); float x = (float) - - 10F; float y = (float) - - 10F; (waterText, waterTextFont, brush, x, y); (); (); MemoryStream stream = new MemoryStream(); (stream, ); (); imageWithWater = (stream); (imageFile); (); } /// <summary> /// Determine whether the upload component contains content./// </summary> /// <param name="fileUpload"> 2.0 standard upload component</param>/// <returns> Return True if the data is valid, otherwise False</returns>public static bool IsAttachmentValid(FileUpload fileUpload) { if (fileUpload != null && != null && !() && > 0) { return true; } return false; }
public class ImageHelper { #region " Relative path to watermark storage" public static string GetLogoPath() { return "/images/"; ///Watermark image path } #endregion #region " Image Watermark " // <summary> // Generate image watermark on the image. This method does not support Gif-type images // </summary> // <param name="Path">original server image path</param> // <param name="Path_syp">The generated image path with image watermark</param> // <param name="Path_sypf">Watermark image path</param> public static void MarkImage(Stream InUploadImagePath, string inLogoImagePath, string InSavePath) { Image = (InUploadImagePath); newimage = ((inLogoImagePath)); Graphics g = (Image); (newimage, new Rectangle( - , - , , ), 0, 0, , , ); try { ((InSavePath)); } catch (Exception ex) { } finally { (); (); (); } } #endregion }
For more information about C# related content, please check out the topic of this site:Summary of C# picture operation skills》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Tutorial on the usage of common C# controls》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming》
I hope this article will be helpful to everyone's C# programming.