为什么加不了水印呢?

百变猪娃娃 2014-11-25 11:26:27

static void Main(string[] args)
{
MyFun(@"D:\imgs\gsyh_2014-01-01.xls_out.tif", @"D:\imgs\运营业务专用章.png", @"D:\imgs\new.tif");
}

static void MyFun(string oldImgPath,string waterImgPath,string newImgPaht)
{
System.Drawing.Image oldImg = System.Drawing.Image.FromFile(oldImgPath);
System.Drawing.Image waterImg = System.Drawing.Image.FromFile(waterImgPath);

Bitmap bm = new Bitmap(oldImg);
Graphics g = Graphics.FromImage(bm);
g.DrawImage(oldImg, 100, 100, waterImg.Width, waterImg.Height);


oldImg.Save(newImgPaht);
g.Dispose();
oldImg.Dispose();
}


生成的新图像,没看到水印啊,和旧图像一样。
...全文
171 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
kkl_1195854501 2014-11-26
  • 打赏
  • 举报
回复
如何给图片添加水印 Bitmap bt=null; float fontSize = 8; Color fontColor=Color.Black; FontFamily FontF = null; FontStyle fontStyle = FontStyle.Regular; int Fwidth; int Fheight; Bitmap BigBt; Font f; Brush b; private void AddFontWatermark(string txt,string Iname,int i)//预览 { b = new SolidBrush(fontColor); bt = new Bitmap(368,75); BigBt = new Bitmap(Image.FromFile(ImgDirectoryPath + "\\" +Iname)); Graphics g = Graphics.FromImage(bt); Graphics g1 = Graphics.FromImage(BigBt); g.Clear(Color.Gainsboro); pbImgPreview.Image = bt; if (FontF == null) { f = new Font(txt,fontSize); SizeF XMaxSize = g.MeasureString(txt, f); Fwidth = (int)XMaxSize.Width; Fheight = (int)XMaxSize.Height; g.DrawString(txt,f, b, (int)(368 - Fwidth) / 2, (int)(75 - Fheight) / 2); if (cbbPosition.SelectedIndex==0)//正中 { g1.DrawString(txt, f, b, (int)(BigBt.Width - Fwidth) / 2, (int)(BigBt.Height - Fheight) / 2); } if (cbbPosition.SelectedIndex == 1)//左上 { g1.DrawString(txt, f, b, 30,30); } if (cbbPosition.SelectedIndex ==2)//左下 { g1.DrawString(txt, f, b, 30, (int)(BigBt.Height - Fheight)-30); } if (cbbPosition.SelectedIndex == 3) //右上 { g1.DrawString(txt, f, b, (int)(BigBt.Width - Fwidth), 30); } if (cbbPosition.SelectedIndex == 4)//右下 { g1.DrawString(txt, f, b, (int)(BigBt.Width - Fwidth), (int)(BigBt.Height - Fheight)-30); } } else { f = new Font(FontF, fontSize, fontStyle); SizeF XMaxSize = g.MeasureString(txt, f); Fwidth = (int)XMaxSize.Width; Fheight = (int)XMaxSize.Height; g.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int)(368 - Fwidth) / 2, (int)(75 - Fheight) / 2); if (cbbPosition.SelectedIndex == 0)//正中 { g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int)(BigBt.Width - Fwidth) / 2, (int)(BigBt.Height - Fheight) / 2); } if (cbbPosition.SelectedIndex == 1)//左上 { g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, 30, 30); } if (cbbPosition.SelectedIndex == 2)//左下 { g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, 30, (int)(BigBt.Height - Fheight)-30); } if (cbbPosition.SelectedIndex == 3) //右上 { g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int)(BigBt.Width - Fwidth), Fheight); } if (cbbPosition.SelectedIndex == 4)//右下 { g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int)(BigBt.Width - Fwidth), (int)(BigBt.Height - Fheight)-30); } } if (i == 1) { string ipath; if (NewFolderPath.Length == 3) ipath = NewFolderPath.Remove(NewFolderPath.LastIndexOf(":") + 1); else ipath = NewFolderPath; string imgstype = Iname.Substring(Iname.LastIndexOf(".") + 1, Iname.Length - 1 - Iname.LastIndexOf(".")); if (imgstype.ToLower() == "jpeg" || imgstype.ToLower() == "jpg") { BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Jpeg); } if (imgstype.ToLower() == "png") { BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Png); } if (imgstype.ToLower() == "bmp") { BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Bmp); } if (imgstype.ToLower() == "gif") { BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Gif); } g1.Dispose(); BigBt.Dispose(); } if (i == 2) { if (cbbPosition.SelectedIndex == 0)//正中 { g1.DrawImage(effect, (int)(BigBt.Width - effect.Width) / 2, (int)(BigBt.Height - effect.Height) / 2); } if (cbbPosition.SelectedIndex == 1)//左上 { g1.DrawImage(effect, 30, 30); } if (cbbPosition.SelectedIndex == 2)//左下 { g1.DrawImage(effect, 30, (int)(BigBt.Height - effect.Height) - 30); } if (cbbPosition.SelectedIndex == 3) //右上 { g1.DrawImage(effect, (int)(BigBt.Width - effect.Width)-30, 30); } if (cbbPosition.SelectedIndex == 4)//右下 { g1.DrawImage(effect, (int)(BigBt.Width - effect.Width)-30, (int)(BigBt.Height - effect.Height) - 30); } } if (i == 3) { if (cbbPosition.SelectedIndex == 0)//正中 { g1.DrawImage(effect, (int)(BigBt.Width - effect.Width) / 2, (int)(BigBt.Height - effect.Height) / 2); } if (cbbPosition.SelectedIndex == 1)//左上 { g1.DrawImage(effect, 30, 30); } if (cbbPosition.SelectedIndex == 2)//左下 { g1.DrawImage(effect, 30, (int)(BigBt.Height - effect.Height) - 30); } if (cbbPosition.SelectedIndex == 3) //右上 { g1.DrawImage(effect, (int)(BigBt.Width - effect.Width), 30); } if (cbbPosition.SelectedIndex == 4)//右下 { g1.DrawImage(effect, (int)(BigBt.Width - effect.Width), (int)(BigBt.Height - effect.Height) - 30); }//codego.net/tags/1/1/ string ipath; if (NewFolderPath.Length == 3) ipath = NewFolderPath.Remove(NewFolderPath.LastIndexOf(":") + 1); else ipath = NewFolderPath; string imgstype = Iname.Substring(Iname.LastIndexOf(".") + 1, Iname.Length - 1 - Iname.LastIndexOf(".")); if (imgstype.ToLower() == "jpeg" || imgstype.ToLower() == "jpg") { BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Jpeg); } if (imgstype.ToLower() == "png") { BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Png); } if (imgstype.ToLower() == "bmp") { BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Bmp); } if (imgstype.ToLower() == "gif") { BigBt.Save(ipath + "\\_" + Iname, ImageFormat.Gif); } } }
feiyun0112 2014-11-25
  • 打赏
  • 举报
回复
g.DrawImage(waterImg, 100, 100, waterImg.Width, waterImg.Height);
百变猪娃娃 2014-11-25
  • 打赏
  • 举报
回复
能不能就我这个代码看看问题在哪里呢?
天下如山 2014-11-25
  • 打赏
  • 举报
回复
百度一下 代码一批一批的。但是有些还是有瑕疵的。
百变猪娃娃 2014-11-25
  • 打赏
  • 举报
回复
问题找出来了,谢谢: g.DrawImage(waterImg, 100, 100, waterImg.Width, waterImg.Height); bm.Save(newImgPaht);
百变猪娃娃 2014-11-25
  • 打赏
  • 举报
回复
引用 8 楼 sibiyellow 的回复:
换个文件试下呗.
把tif换成了windows系统自带的jpg图,也没有水印。
天下如山 2014-11-25
  • 打赏
  • 举报
回复
换个文件试下呗.
百变猪娃娃 2014-11-25
  • 打赏
  • 举报
回复
引用 6 楼 sibiyellow 的回复:
g.DrawImage(waterImg, 100, 100, waterImg.Width, waterImg.Height); 就是4楼的答案 测试过是可以的。
生成后的图片,文件大小确实是变大了,但是真的没显示水印啊。 难道是因为tif的文件的问题吗?
天下如山 2014-11-25
  • 打赏
  • 举报
回复
g.DrawImage(waterImg, 100, 100, waterImg.Width, waterImg.Height); 就是4楼的答案 测试过是可以的。
百变猪娃娃 2014-11-25
  • 打赏
  • 举报
回复
引用 4 楼 feiyun0112 的回复:
g.DrawImage(waterImg, 100, 100, waterImg.Width, waterImg.Height);
我按照你的改了后,还是没有水印。

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧