• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#添加图片水印类

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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 { /// /// 给图片上水印 /// /// 原图片地址 /// 水印图片地址 /// 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 { } } } } } 如果提示访问权限不够: 在web.config中 中加 提高权限

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
XML-RPC 实现C++和C#交互发布时间:2022-07-13
下一篇:
C#找到目录和其子目录的某个文件发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap