C#图片加水印(Generate Watermark Picture)

  C#图片加水印(Generate Watermark Picture)

C#图片加水印(Generate Watermark Picture)


效果图如下:






C#源码:


using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace CSFramework.BLL
{
   public class WatermarkImage
   {
      /// <summary>
      /// 在图片上添加水印文字
      /// </summary>
      /// <param name="sourcePicture">源图片文件</param>
      /// <param name="waterWords">需要添加到图片上的文字</param>
      /// <param name="webName">网站名称</param>
      /// <param name="alpha">透明度</param>
      /// <returns></returns>
      public string DrawWords(string sourcePicture, string waterWords, string webName, float alpha)
      {
         // 判断参数是否有效
         if (sourcePicture == string.Empty || waterWords == string.Empty || alpha == 0.0)
         {
            return sourcePicture;
         }
         
         string PicturePath = Path.GetDirectoryName(sourcePicture) "\\";
         string fileExtension = System.IO.Path.GetExtension(sourcePicture).ToLower();
         
         // 判断文件是否存在,以及文件名是否正确
         if (System.IO.File.Exists(sourcePicture) == false || (
         fileExtension != ".gif" &&&
         fileExtension != ".jpg" &
         fileExtension != ".png"))
         {
            return sourcePicture;
         }
         
         //创建一个图片对象用来装载要被添加水印的图片
         Image imgPhoto = Image.FromFile(sourcePicture);
         
         //获取图片的宽和高
         int phWidth = imgPhoto.Width;
         int phHeight = imgPhoto.Height;
         
         //建立一个bitmap,和我们需要加水印的图片一样大小
         Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
         
         //SetResolution:设置此 Bitmap 的分辨率
         //这里直接将我们需要添加水印的图片的分辨率赋给了bitmap
         bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
         
         //Graphics:封装一个 GDI 绘图图面。
         Graphics grPhoto = Graphics.FromImage(bmPhoto);
         grPhoto.SmoothingMode = SmoothingMode.AntiAlias;//设置图形的品质
         
         //将我们要添加水印的图片按照原始大小描绘(复制)到图形中
         grPhoto.DrawImage(
         imgPhoto, // 要添加水印的图片
         new Rectangle(0, 0, phWidth, phHeight), // 根据要添加的水印图片的宽和高
         0, // X方向从0点开始描绘
         0, // Y方向
         phWidth, // X方向描绘长度
         phHeight, // Y方向描绘长度
         GraphicsUnit.Pixel); // 描绘的单位,这里用的是像素
         
         int m_alpha = Convert.ToInt32(256 * alpha);
         SolidBrush alphaBrush = new SolidBrush(Color.FromArgb(m_alpha, 175, 175, 175)); //设置画笔透明度及颜色
         
         DrawMainWords(imgPhoto, grPhoto, alphaBrush, waterWords);
         DrawWebTitleWords(imgPhoto, grPhoto, alphaBrush, webName);
         
         imgPhoto.Dispose();
         imgPhoto = bmPhoto;//重新设置引用
         grPhoto.Dispose();
         
         // 目标图片名称及全路径
         string targetImage = Path.GetDirectoryName(sourcePicture) "\\" DateTime.Now.ToString("yyyyMMddhhmmss") ".png";
         
         //将grPhoto保存
         imgPhoto.Save(targetImage, ImageFormat.Png);
         imgPhoto.Dispose();
         
         File.Delete(sourcePicture);//删除旧的图片
         File.Move(targetImage, sourcePicture);//文件改名
         
         return targetImage.Replace(PicturePath, "");
      }
      
      private void DrawWebTitleWords(Image imgPhoto, Graphics g, SolidBrush alphaBrush, string waterWords)
      {
         Font font = new Font("黑体", 12, FontStyle.Bold);
         
         SolidBrush brush = new SolidBrush(Color.FromArgb(Convert.ToInt32(0.3 * 256), 175, 175, 175)); //设置画笔透明度及颜色
         
         g.DrawString(waterWords, font, brush, imgPhoto.Width / 2 - 30, 40); //绘制文字
      }
      
      private void DrawMainWords(Image imgPhoto, Graphics g, SolidBrush alphaBrush, string waterWords)
      {
         //**********绘制倾斜文字**************
         Font font = new Font("Verdana", 26, FontStyle.Bold);
         SizeF size = g.MeasureString(waterWords, font);
         Single posX = 10;
         Single posY = Convert.ToSingle(imgPhoto.Height * 0.8); //高度位置80%处
         g.TranslateTransform(posX, posY);
         
         Matrix transform = g.Transform;
         //float shearX = -0.230F; //右倾斜文字
         float shearX = 0.50F; //左倾斜文字
         float shearY = -0.50F;
         
         transform.Shear(shearX, shearY);
         g.Transform = transform;
         
         g.DrawString(waterWords, font, alphaBrush, 0, 0); //绘制文字
      }
   }
}


// 来源:www.CSFramework.com, C/S结构框架学习网




C/S框架网|原创精神.创造价值.打造精品


扫一扫加作者微信
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务

版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
C/S框架网
上一篇:没有了
发表评论

评论内容
昵称:
关联文章

C#图片(Generate Watermark Picture)
C#.NET iTextSharp生成PDF文件源码大全(生成表格、透明,页脚页眉页码等)
C# PictureBox设置背景图片图片源码
C#读取资源文件的图片
C# 不占用文件句柄方式图片文件(载到内存流MemoryStream)
C# Dev GridView表格的CustomUnboundColumnData事件异步图片类AsynDownImage
DevExpress GridView表格CustomUnboundColumnData事件异步图片
静态网页生成器 - HTML Generator
C# RichTextbox中插入图片
C#图片转换类支持按比例缩放图片
Dev异步载动画图片等待窗体frmWaiting,WaitForm,SplashScreenManager
C#.Net 上传图片,限制图片大小,检查类型完整版
C# 窗体上显示透明图片,PNG图片,已审核/未审核图标
类模型生成器子系统(Class Generate System)
C#切割图片自动填充工具栏按钮图片
C# Dev GridView表格使用RepositoryItemPictureEdit显示图标或图片
CSFrameworkV5.1旗舰版代码生成器(Code Generator
C# 本地图片缓存器ImageFileCache (www.csframework.com)
C# 实现条码图片自动生成功能
C# Image图片转Icon图标