C# 添加图片水印类
来源:互联网 发布:一人一首成名曲网络篇 编辑:程序博客网 时间:2024/05/01 11:43
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
{ }
}
}
}
}
如果提示访问权限不够:
在web.config中
<system.web>中加
<identity impersonate="true"/> 提高权限
- C# 添加图片水印类
- C# 添加 图片 水印
- c# 添加图片水印
- C#图片添加水印
- c#图片添加水印
- C# 添加图片水印类实现代码
- C#中添加图片水印
- C# 批量添加图片水印
- C# 给图片添加水印
- C#图片水印类
- C#图片水印类
- C# 对Word文件添加水印图片
- C#为图片添加水印,生成缩略图
- C# 添加水印图片、文字、缩略图处理
- c# 添加图片水印,可以指定水印位置+生成缩略图
- c# 添加图片水印,可以指定水印位置+生成缩略图
- c# 添加图片水印,可以指定水印位置+生成缩略图
- c# 添加图片水印,可以指定水印位置+生成缩略图
- 软件管理74条体会自我检查
- 为了下载而攒分
- 添加修改日志步骤
- 李鸿章十四首律诗
- C# 添加文字水印类
- C# 添加图片水印类
- JQuery:单选框,复选框取值
- C# 给站点指定位置的某种格式的图片添加水印
- html中分页
- DateTime数据类型的XML序列化及时区问题(存档)
- mysql两种查询方式对比--子查询与join查询
- 博士生传给硕士生的经验
- SQL/PLUS
- sharepoint的单点登录