C#写的给图片加水印的实例代码
2009-11-07 10:39
573 查看
发现以前写的给图片加水印的实例代码,实际上是就是图片叠加的代码。
源码
using System; using System.Collections.Generic; using System.Collections; using System.Text; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; namespace Enter_name { 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 + "//4.jpg"; favoriteImage[] FaImage = new favoriteImage[2]; FaImage[0].x = -3; FaImage[0].y = 70; FaImage[0].imagePath = CurrentDirectory + "//1.jpg"; FaImage[1].x = 20;//65; FaImage[1].y = -12; FaImage[1].imagePath = CurrentDirectory + "//2.jpg"; generateWinterMark(CurrentDirectory, body_path, FaImage); } /// <summary> /// /// </summary> /// <param name="savePath"></param> /// <param name="body_path"></param> /// <param name="favorite"></param> /// <returns></returns> private static string generateWinterMark(string savePath, string body_path, favoriteImage[] favorite) { //create a image object containing the photograph to watermark Bitmap imgPhoto = new Bitmap(body_path); int phWidth = imgPhoto.Width; int phHeight = imgPhoto.Height; 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 + ".jpg"; //create a Bitmap the Size of the original photograph Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb); //setResolution 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; 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 Bitmap imgWatermark = new Bitmap(favorite[i].imagePath); int wmWidth = imgWatermark.Width; int wmHeight = imgWatermark.Height; imgWatermark.MakeTransparent(); //使默认的透明颜色对此 Bitmap 透明。 //Create a Bitmap based on the previously modified photograph Bitmap Bitmap bmWatermark = new Bitmap(bmPhoto); //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;//age(FromHbitmap(bmWatermark; grWatermark.Dispose(); imgWatermark.Dispose(); } bmPhoto.Dispose(); grPhoto.Dispose(); //save new image to file system. imgPhoto.Save(saveImagePath, ImageFormat.Jpeg); imgPhoto.Dispose(); return saveImagePath; } } }
源码
相关文章推荐
- C#图片加水印实例与代码
- php图片加水印原理(超简单的实例代码)
- asp.net上传图片并作处理水印与缩略图的实例代码
- c#多图片上传并生成缩略图的实例代码
- c#多图片上传并生成缩略图的实例代码
- java实现文章图片水印效果的代码实例
- c# 图片加密解密的实例代码
- Java图片处理 (文字水印、图片水印、缩放、补白)代码实例
- C#中按指定质量保存图片的实例代码 24位深度
- Android给图片加文字和图片水印实例代码
- C# 添加图片水印类实现代码
- C# 手动/自动保存图片的实例代码
- java图片添加水印实例代码分享
- C#.NET 图片水印添加代码
- php图片加水印原理(超简单的实例代码)
- C#给图片添加水印完整实例
- ASP.NET图片加水印的实现代码(C#)
- C#实现在listview中插入图片实例代码
- java 图片加水印实例代码