C#生成透明图片,去掉文字水印中的文字阴影 本帖最后由 legends2012 于 2014-02-17 22:20:42 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我也遇到了这样的情况,是这样解决的:在输出文本之前加上下面这句代码g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; /**/ /// <summary> /// 水印文字是否使用阴影 /// </summary> public bool Shadow { get; set; }Shadow=false 在图片上添加水印图片/// <summary> /// 设置水印图片 /// </summary> /// <param name="path">要设置水印图片的路径</param> public void WaterMark(string path) { //获取要水印的图片 Bitmap bmp = new Bitmap(HttpContext.Current.Server.MapPath(".") + "/Alex.gif"); //设置水印图片的透明度 ImageAttributes imageAttr = new ImageAttributes(); imageAttr.SetColorKey(bmp.GetPixel(20, 20), bmp.GetPixel(20, 20)); //获取要设置水印图片的扩展名 string extension = Path.GetExtension(path).ToUpper(); //设置临时图片名称 string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString(); //初始化要加水印的图片 System.Drawing.Image image = System.Drawing.Image.FromFile(path); //初始化水印图片 System.Drawing.Image copyImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(".") + "/Alex.gif"); //创建绘图区域 Graphics g = Graphics.FromImage(image); //将水印图片,合成到指定图片上 g.DrawImage(copyImage, new Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel, imageAttr); g.Dispose(); //保存加水印过后的图片,删除原始图片 string newPath = HttpContext.Current.Server.MapPath(".") + "/" + fileName + "_new" + extension; //保存设置完成后的水印图片到临时位置 image.Save(newPath); image.Dispose(); // File.Copy(newPath, path, true); //删除水印 if (File.Exists(newPath)) { //删除临时存储的图片 File.Delete(newPath); } } 源文件为什么会变繁体 想用jquery+vs2008做一个动态树 奇怪:未将对象引用设置到对象的实例 问各位高手一个问题:关于预编译DLL和自动编译 字符截取的问题(有点难度) 大家帮我看看,这是什么原因,点击登陆没有反应! session VB 如何传值 DataGrid删除列问题 Duwamish7分布式部署的问题,高手看过来 有谁有DATAGRID组件的*.ASPX的例子的,传过来-马上给分100 关于onbeforeunload方法的触发条件 .net程序怎样调用oracle的MERGE
在输出文本之前加上下面这句代码
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
/// <summary>
/// 水印文字是否使用阴影
/// </summary>
public bool Shadow { get; set; }Shadow=false
/// <summary>
/// 设置水印图片
/// </summary>
/// <param name="path">要设置水印图片的路径</param>
public void WaterMark(string path)
{
//获取要水印的图片
Bitmap bmp = new Bitmap(HttpContext.Current.Server.MapPath(".") + "/Alex.gif");
//设置水印图片的透明度
ImageAttributes imageAttr = new ImageAttributes();
imageAttr.SetColorKey(bmp.GetPixel(20, 20), bmp.GetPixel(20, 20));
//获取要设置水印图片的扩展名
string extension = Path.GetExtension(path).ToUpper();
//设置临时图片名称
string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
//初始化要加水印的图片
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
//初始化水印图片
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(".") + "/Alex.gif");
//创建绘图区域
Graphics g = Graphics.FromImage(image);
//将水印图片,合成到指定图片上
g.DrawImage(copyImage, new Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel, imageAttr);
g.Dispose();
//保存加水印过后的图片,删除原始图片
string newPath = HttpContext.Current.Server.MapPath(".") + "/" + fileName + "_new" + extension;
//保存设置完成后的水印图片到临时位置
image.Save(newPath);
image.Dispose();
//
File.Copy(newPath, path, true);
//删除水印
if (File.Exists(newPath))
{
//删除临时存储的图片
File.Delete(newPath);
}
}