zoukankan      html  css  js  c++  java
  • 用.net实现按透明度生成水印文件

             /// <summary>
      /// 生成水印,可按左上、左下、右上、右下、居中、透明度生成文件,只对jpeg或jpg格式有效!
      /// </summary>
      /// <param name="sourceFile">底图</param>
      /// <param name="waterMarkFile">水印图</param>
      /// <param name="saveFile">要保存的文件</param>
      /// <param name="local">位置:左上(1)、左下(2)、右上(3)、右下(4)、居中(5)</param>
      /// <param name="alpha">透明度(1-100)</param>
      /// <returns>bool,是否成功</returns>
      public static bool MakeWaterImage(string sourceFile,string waterMarkFile,string saveFile,int local,int alpha)
      {  
      bool result;
      
      if( !File.Exists(sourceFile) || !File.Exists(waterMarkFile)) //如果源图或水印图不存在
      return false;
      
      FileInfo fi = new FileInfo(sourceFile);
      //判断文件类型是否合法  
      if(fi.Extension.ToLower()!=".jpg" & fi.Extension.ToLower()!=".jpeg")
      return false;
      
      try
      {
      //原图
      Bitmap sImage = new Bitmap(sourceFile);
      int sWidth  = sImage.Width;
      int sHeight  = sImage.Height;
      
      //水印图
      Bitmap wImage = new Bitmap(waterMarkFile);
      int wWidth  = wImage.Width;
      int wHeight  = wImage.Height;

      //make Graphics.
      Graphics g = Graphics.FromImage(sImage);
      int x; //临时变量
      int y; //监时变量
      int x1 = 0; //原图和水印图的宽度差,即开始绘图的X位置
      int y1 = 0; //原图和水印图的高度差,即开始绘图的Y位置
      int w = 0; //生成的水印图的宽度,即结束绘图的X位置
      int h = 0; //生成的水印图的高度,即结束绘图的Y位置
      int al; //alpha
      int rl; //Red
      int gl; //Green
      int bl; //Blue

      //校验透明度
      if(alpha < 1 || alpha > 100)
      al = 80;
      else
      al = alpha;
      if(sWidth > wWidth & sHeight > wHeight) //如果源图比水印图大
      {    
      switch(local)
      {
      case 1: //左上
      x1 = 0;
      y1 = 0;      

  • 相关阅读:
    python day01
    Mac上安装pexpect
    raid
    SSL证书制作
    linux grep命令详解
    第一轮迭代小组成员分数分配
    M1事后分析报告(Postmortem Report)
    软件发布说明
    测试报告
    week 9 scenario testing
  • 原文地址:https://www.cnblogs.com/Spring/p/422530.html
Copyright © 2011-2022 走看看