时间:2016-02-15 21:34 来源: 我爱IT技术网 作者:佚名
欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【C# 添加图片水印类实现代码】,下面是详细的讲解!
C# 添加图片水印类实现代码
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# 添加图片水印类实现代码的用户互动如下:
相关问题:
答: >>详细
相关问题:
答: >>详细
相关问题:
答: >>详细
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【服务器】asp.net页面状态管理cookie和服务器状
- 【Repeater控件】.NET实现Repeater控件+AspNetPag
- 【客户端】获取客户端IP地址c#/vb.net各自实现代
- 【asp】asp.net上传execl文件后 在页面上加载显示
- 【ref】asp.net(c#)ref out params的区别-out-pa
- 【数据控件】asp.net获得数据控件事件索引并获取
- 【NET】10个.NET中删除空白字符串的方法-删除空白
- 【web】web.config配置连接字符串的方法-config配
- 【asp】asp.net 计划任务管理程序实现,多线程任务
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
