AiQinZi 2014-01-16 02:19 采纳率: 0%
浏览 2525

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);

}

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制
    • ¥20 usb设备兼容性问题
    • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
    • ¥15 安装svn网络有问题怎么办