//下载示例 http://files.cnblogs.com/kaixin110/demo.rar using System; using System.Collections.Generic; using System.Text; using System.Collections; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; namespace Haix.Utils { class GenerateImage { public struct favoriteImage { private string _imagePath; private int _x; private int _y; public int x { get { return _x; } set { _x = value; } } public int y { get { return _y; } set { _y = value; } } public string imagePath { get { return _imagePath; } set { _imagePath = value; } } } [STAThread] static void Main(string[] args) { string CurrentDirectory = System.Environment.CurrentDirectory; string body_path=CurrentDirectory + "//white.png"; favoriteImage[] FaImage = new favoriteImage[2]; FaImage[0].x = -3; FaImage[0].y = 70; FaImage[0].imagePath = CurrentDirectory + "//1.png"; FaImage[1].x = 20;//65; FaImage[1].y = -12; FaImage[1].imagePath = CurrentDirectory + "//2.png"; generateWinterMark(CurrentDirectory,body_path, FaImage); } /// /// 生成水印 /// /// 主图片路径,eg:body /// 要叠加的图片路径 /// 要叠加的图片位置的X坐标 /// 要叠加的图片位置的Y坐标 /// /// 生成图片的路径 private static string generateWinterMark(string savePath,string body_path,favoriteImage[] favorite) { //create a image object containing the photograph to watermark Image imgPhoto = Image.FromFile(body_path); int phWidth = imgPhoto.Width; int phHeight = imgPhoto.Height; //create a Bitmap the Size of the original photograph Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb); //设置此 Bitmap 的分辨率。 bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); //load the Bitmap into a Graphics object Graphics grPhoto = Graphics.FromImage(bmPhoto); //Set the rendering quality for this Graphics object grPhoto.SmoothingMode = SmoothingMode.AntiAlias;//清除锯齿的呈现 //haix for (int i = 0; i < favorite.Length; i++) { //Draws the photo Image object at original size to the graphics object. grPhoto.DrawImage( imgPhoto, // Photo Image object new Rectangle(0, 0, phWidth, phHeight), // Rectangle structure 0, // x-coordinate of the portion of the source image to draw. 0, // y-coordinate of the portion of the source image to draw. phWidth, // Width of the portion of the source image to draw. phHeight, // Height of the portion of the source image to draw. GraphicsUnit.Pixel); // Units of measure //------------------------------------------------------------ //Step #2 - Insert Property image,For example:hair,skirt,shoes etc. //------------------------------------------------------------ //create a image object containing the watermark Image imgWatermark = new Bitmap(favorite[i].imagePath); int wmWidth = imgWatermark.Width; int wmHeight = imgWatermark.Height; //Create a Bitmap based on the previously modified photograph Bitmap Bitmap bmWatermark = new Bitmap(bmPhoto); bmWatermark.MakeTransparent(); //使默认的透明颜色对此 Bitmap 透明。 //bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); //Load this Bitmap into a new Graphic Object Graphics grWatermark = Graphics.FromImage(bmWatermark); int xPosOfWm = favorite[i].x; int yPosOfWm = favorite[i].y; //叠加 grWatermark.DrawImage(imgWatermark,new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), //Set the detination Position 0, // x-coordinate of the portion of the source image to draw. 0, // y-coordinate of the portion of the source image to draw. wmWidth, // Watermark Width wmHeight, // Watermark Height GraphicsUnit.Pixel, // Unit of measurment null); //ImageAttributes Object //Replace the original photgraphs bitmap with the new Bitmap imgPhoto = bmWatermark; //grWatermark.Dispose(); //imgWatermark.Dispose(); //grPhoto.Dispose(); //bmWatermark.Dispose(); } //haix string nowTime = DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString(); nowTime += DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString(); string saveImagePath = savePath + "//FA" + nowTime + ".png"; //save new image to file system. imgPhoto.Save(saveImagePath, ImageFormat.Png); imgPhoto.Dispose(); return saveImagePath; } } } ———————————————— 版权声明:本文为CSDN博主「思月行云」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/kenkao/article/details/3047511