C#图片以流的形式加水印 坐标改怎么设置。
用流读取的图片,我在上面加了文字水印。 但是我要怎么控制水印的位置,比如说我要加到右下角。应该怎么算坐标?(同一个坐标 。jpg格式跟tif格式位置不一样。)下面是我的代码:
///
/// 图片加水印
///
/// 图片路径
/// 字体
/// 字体大小
/// 水印位置
/// 水印文字
/// 存储图片的文件夹
public void AddWaterText(string oldPath,string font,int fontSize,string wntType,string Text,string directory)
{
Byte[] photo = getImageByte(oldPath);
MemoryStream stmBLOB = new MemoryStream(photo);
Image pic = Image.FromStream(stmBLOB);
Graphics grap = Graphics.FromImage(pic);
Brush brush = new SolidBrush(Color.Red);//创建一把刷子
int xpos = 10;
int ypos = 10;
switch (wntType)
{
case "WMP_Left_Top":
xpos = 10;
ypos = 10;
break;
case "WMP_Right_Top":
xpos = pic.Width - 10;
ypos = 10;
break;
case "WMP_Right_Bottom":
xpos = pic.Width - 10;
ypos = pic.Height - 10;
break;
case "WMP_Left_Bottom":
xpos = 10;
ypos = pic.Height - 10;
break;
case "WM_ZJ":
xpos = pic.Width / 2;
ypos = pic.Height / 2;
break;
}
grap.DrawString(Text, new Font(font, fontSize), brush, xpos, ypos);//给图片(pic)加水印
grap.Dispose();
string fileName = oldPath.Substring(oldPath.LastIndexOf("\") + 1);
string SavePath = "C:\\ImageAddWaterMark\" + fileName;
if (!Directory.Exists("C:\\ImageAddWaterMark"))
{
Directory.CreateDirectory("C:\\ImageAddWaterMark");
}
pic.Save(SavePath, System.Drawing.Imaging.ImageFormat.Jpeg);//将有水印的图片保存到临时文件夹
pic.Dispose();
if (File.Exists(oldPath))
{
File.Delete(oldPath);
}
File.Move(SavePath,oldPath);
}
这坐标不对啊。。
[解决办法]
这个应该根据图片的大小来选择字体的大小吧,如果字太多超过了图片范围,那怎么设置坐标的位置都没用
[解决办法]
无语,你写的方法和参数注释不对头啊,位置写死,路径写死
[解决办法]
case "WMP_Right_Top":
xpos = pic.Width - 10 - 水印的宽度;
..
case "WMP_Right_Bottom":
xpos = pic.Width - 10 - 水印的宽度;
ypos = pic.Height - 10 - 水印的高度;
case "WMP_Left_Bottom":
...
ypos = pic.Height - 10 - 水印的高度;
case "WM_ZJ":
xpos = (pic.Width / 2) - (水印的宽度/2);
ypos = (pic.Height / 2) -(水印的高度/2);;
[解决办法]
可以截一个图来看看,再说期望的效果