C# 在图片上写入水印

/// <summary>
        /// 在图片上生成图片水印
        /// </summary>
        /// <param name="Path">原服务器图片路径</param>
        /// <param name="Path_syp">生成的带图片水印的图片路径</param>
        /// <param name="Path_sypf">水印图片路径</param>
        public void AddWaterPic(string Path, string Path_syp, string Path_sypf, int x = 0, int y = 0)
        {
            using (System.Drawing.Image srcImg = System.Drawing.Image.FromFile(Path))
            {
                //将水印文件加载到内存中
                using (System.Drawing.Image waterMark = System.Drawing.Image.FromFile(Path_sypf))
                {
                    //实例化一块画布
                    using (Graphics g = Graphics.FromImage(srcImg))
                    {

                        if (x == 0)
                        {
                            x = (int)(((double)srcImg.Width - (double)waterMark.Width) / 2);

                        }
                        if (y == 0)
                        {
                            y = (int)(((double)srcImg.Height - ((double)waterMark.Height / 2)) / 2);
                        }
                        //写文字
                        //g.DrawString("17cn商城版权所有...",new Font("Microsoft Yahei",20),new SolidBrush(Color.Red),20,20); 
                        //画图片

                        //按图片大小给定水印
                        //if (srcImg.Width > 100000000)
                        //{
                        // Path_sypf = Path_sypf.Replace("logo_1.png", "logo_1.png");
                        //}
                        //else if(srcImg.Width > 500000)
                        //{
                        // Path_sypf = Path_sypf.Replace("logo_1.png", "logo_1.png");
                        //}

                        //水印的宽度大于图片宽度时进行水印压缩
                        if (waterMark.Width > srcImg.Width)
                        {
                            x = 0;
                            int waterh = (int)((double)srcImg.Width / (double)waterMark.Width * (double)waterMark.Height);
                            g.DrawImage(waterMark.GetThumbnailImage(srcImg.Width, waterh, null, IntPtr.Zero), new System.Drawing.Rectangle(x, y, waterMark.Width, waterMark.Height), 0, 0, waterMark.Width, waterMark.Height, GraphicsUnit.Pixel);
                        }
                        else
                        {
                            g.DrawImage(waterMark, new System.Drawing.Rectangle(x, y, waterMark.Width, waterMark.Height), 0, 0, waterMark.Width, waterMark.Height, GraphicsUnit.Pixel);
                        }
                        srcImg.Save(Path_syp);
                    }
                }

            }
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值