C# 添加图片水印类实现代码
2009-05-20 00:00
1096 查看
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.IO; using System.Drawing.Imaging; using System.Web; using System.Drawing.Drawing2D; using System.Reflection; namespace Chen { public class warterPic { /// <summary> /// 给图片上水印 /// </summary> /// <param name="filepath">原图片地址</param> /// <param name="waterfile">水印图片地址</param> /// public void markwater(string filepath, string waterfile) { //gif不水印 int i = filepath.LastIndexOf("."); string ex = filepath.Substring(i, filepath.Length - i); if (string.Compare(ex, ".gif", true) == 0) { return; } string modifyimagepath = filepath;//修改的图像路径 int lucencypercent = 25; Image modifyimage = null; Image drawedimage = null; Graphics g = null; try { //建立图形对象 modifyimage = Image.FromFile(modifyimagepath, true); drawedimage = Image.FromFile(waterfile, true); g = Graphics.FromImage(modifyimage); //获取要绘制图形坐标 int x = modifyimage.Width - drawedimage.Width; int y = modifyimage.Height - drawedimage.Height; //设置颜色矩阵 float[][] matrixitems ={ new float[] { 1, 0, 0, 0, 0 }, new float[] { 0, 1, 0, 0, 0 }, new float[] { 0, 0, 1, 0, 0 }, new float[] { 0, 0, 0, (float)lucencypercent / 100f, 0 }, new float[] { 0, 0, 0, 0, 1 } }; ColorMatrix colormatrix = new ColorMatrix(matrixitems); ImageAttributes imgattr = new ImageAttributes(); imgattr.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); //绘制阴影图像 g.DrawImage(drawedimage, new Rectangle(x, y, drawedimage.Width, drawedimage.Height), 10, 10, drawedimage.Width, drawedimage.Height, GraphicsUnit.Pixel, imgattr); //保存文件 string[] allowimagetype ={ ".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico" }; FileInfo fi = new FileInfo(modifyimagepath); ImageFormat imagetype = ImageFormat.Gif; switch (fi.Extension.ToLower()) { case ".jpg": imagetype = ImageFormat.Jpeg; break; case ".gif": imagetype = ImageFormat.Gif; break; case ".png": imagetype = ImageFormat.Png; break; case ".bmp": imagetype = ImageFormat.Bmp; break; case ".tif": imagetype = ImageFormat.Tiff; break; case ".wmf": imagetype = ImageFormat.Wmf; break; case ".ico": imagetype = ImageFormat.Icon; break; default: break; } MemoryStream ms = new MemoryStream(); modifyimage.Save(ms, imagetype); byte[] imgdata = ms.ToArray(); modifyimage.Dispose(); drawedimage.Dispose(); g.Dispose(); FileStream fs = null; //File.Delete(modifyimagepath); fs = new FileStream(modifyimagepath, FileMode.Create, FileAccess.Write); if (fs != null) { fs.Write(imgdata, 0, imgdata.Length); fs.Close(); } } finally { try { drawedimage.Dispose(); modifyimage.Dispose(); g.Dispose(); } catch { } } } } }
相关文章推荐
- C# 添加图片水印类实现代码
- .net c# gif动画如何添加图片水印实现思路及代码学习
- .net c# gif动画如何添加图片水印实现思路及代码
- .net c# gif动画如何添加图片水印实现思路及代码
- C# :实现水印与图片合成,并利用Graphics 压缩图像质量 , (委托实现listBox的动态添加提示)
- ASP.NET图片加水印的实现代码(C#)
- php图片添加文字水印实现代码
- Android图片添加水印图片并把图片保存到文件存储的实现代码
- Java代码实现给图片添加文字水印 详细教程一
- C# :实现水印与图片合成,并利用Graphics 压缩图像质量 , (委托实现listBox的动态添加提示)
- php图片添加文字水印实现代码
- PHP实现图片裁剪、添加水印效果代码
- 利用ffmpeg实现添加图片水印和文字水印,添加多个水印。代码和命令实现及中文水印乱码
- C# :实现水印与图片合成,并利用Graphics 压缩图像质量 , (委托实现listBox的动态添加提示)。。。。。。。。。。。。
- PHP实现图片裁剪、添加水印效果代码
- php图片添加文字水印实现代码
- C#.NET 图片水印添加代码
- C#实现对图片加水印的一段代码.
- C#图片添加水印的实现代码