.Net 图片生成水印

借鉴于博客园园友的方法,大神神风(https://www.cnblogs.com/tandyshen/archive/2012/04/14/picwater.html) ,很实用的一种 自定义水印方法,搬运过来,记录方便使用,只针对这一个功能实现,大家可以F12 追着看看

Graphics

画板方法其中的操作。

public static string GetWatermarkPic(string picPath, string format, int size, string text, int alpha, string outPath)
        {
            try
            {

                FileStream fs = new FileStream(picPath, FileMode.Open); //读取文件流 提供文件读写方法
                BinaryReader br = new BinaryReader(fs); //继承object 不能对象,必须串联到stream 数据流  在进行操作
                byte[] bytes = br.ReadBytes((int)fs.Length);//读取图片 转化为 二进制流

                br.Close();
                fs.Close();

                MemoryStream ms = new MemoryStream(bytes);  //把流存储到内存中  memoryStream  在内存中开辟一个 流大小的空间
                System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
                int imgPhotoWidth = imgPhoto.Width;
                int imgPhotoHeight = imgPhoto.Height;

                Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);   // 是用于处理由像素数据定义的图像的对象        System.Drawing.Imaging.PixelFormat.Format24bppRgb颜色数据格式
                bmPhoto.SetResolution(72, 72);  //设置图片分辨率
                Graphics gbmPhoto = Graphics.FromImage(bmPhoto);//创建一个 GDI+ 绘图图面

                //gif背景色
                gbmPhoto.Clear(Color.FromName("white"));
                gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                gbmPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight), 0, 0, imgPhotoWidth, imgPhotoHeight, GraphicsUnit.Pixel);
                System.Drawing.Font font = null;
                font = new Font(format, size, FontStyle.Bold);

                //测量区域
                System.Drawing.SizeF crSize = new SizeF();//存储 尺寸
                crSize = gbmPhoto.MeasureString(text, font);//测量 汇成的图案大小
                float y = imgPhotoHeight - crSize.Height;
                float x = imgPhotoWidth - crSize.Width;
                System.Drawing.StringFormat strFormat = new System.Drawing.StringFormat();
                strFormat.Alignment = System.Drawing.StringAlignment.Center;

                //❀两次 制造透明效果
                System.Drawing.SolidBrush semiTransBrush2 = new System.Drawing.SolidBrush(Color.FromArgb(alpha, 56, 56, 56));
                gbmPhoto.DrawString(text, font, semiTransBrush2, x + 1, y + 1);

                System.Drawing.SolidBrush semiTransBrush = new System.Drawing.SolidBrush(Color.FromArgb(1, 176, 176, 176));
                gbmPhoto.DrawString(text, font, semiTransBrush, x, y);
                bmPhoto.Save(outPath, System.Drawing.Imaging.ImageFormat.Jpeg);


                gbmPhoto.Dispose();
                imgPhoto.Dispose();
                bmPhoto.Dispose();

                return "1";
            }
            catch (Exception ex)
            {

                return ex.ToString();
            }




        }

以上是 图片水印的 生成方法  其中有一点 比较重点的是  下面的这个语句:

Graphics gbmPhoto = Graphics.FromImage(bmPhoto);//创建一个 GDI+ 绘图图面 
作用就是把这个 图片 打开一个 绘画对象,实现 添加水印图片 等内容 ,最后把 编辑好的,直接生成到一个地址.

一下是调用方法:
GetWatermarkPic("图片地址", "微软雅黑", 18, "王天来霸王印章",200, "导出地址,必须添加 图片名称:E:\权衡.jpg");

附带一张效果图片:

原文地址:https://www.cnblogs.com/nnqwbc/p/8892807.html