ASP.NET在圖片上加入圖片著作權資訊(在圖片上打浮水印)

來源:互聯網
上載者:User

N年前寫的在圖片上打浮水印的程式:

  /// <summary>
  /// 在圖片上加入圖片著作權資訊
  /// </summary>
  /// <param name="strFileName">輸入圖片(絕對路徑)</param>
  /// <param name="strOutfileName">輸出圖片(絕對路徑)</param>
  /// <param name="strCopyRightFile">浮水印圖片(絕對路徑)</param>
  /// <param name="StrPlace">浮水印位置</param>
  /// <param name="bolFileName">是否刪除輸入圖片</param>
  public static void CreateCopyRightImage(string strFileName, string strOutfileName, string strCopyRightFile, string StrPlace, bool bolFileName)
  {
   System.Drawing.Image MyImage     = System.Drawing.Image.FromFile( strFileName );
   System.Drawing.Image CopyMyImage = System.Drawing.Image.FromFile( strCopyRightFile );
   System.Drawing.Bitmap OutPut     = new System.Drawing.Bitmap( MyImage );
   System.Drawing.Graphics GImage   = System.Drawing.Graphics.FromImage( OutPut );

   int IntX = 0;
   int IntY = 0;
   if (StrPlace == "1")   // 在左上
   {
    IntX = 0;
    IntY = 0;
   }
   else if (StrPlace == "2")  // 在正上
   {
    IntX = (MyImage.Width-CopyMyImage.Width)/2;
    IntY = 0;
   }
   else if (StrPlace == "3")  // 在右上
   {
    IntX = MyImage.Width-CopyMyImage.Width;
    IntY = 0;
   }
   else if (StrPlace == "4")  // 在正左
   {
    IntX = 0;
    IntY = (MyImage.Height-CopyMyImage.Height)/2;
   }
   else if (StrPlace == "5")  // 在中間
   {
    IntX = (MyImage.Width-CopyMyImage.Width)/2;
    IntY = (MyImage.Height-CopyMyImage.Height)/2;
   }
   else if (StrPlace == "6")  // 在正右
   {
    IntX = MyImage.Width-CopyMyImage.Width;
    IntY = (MyImage.Height-CopyMyImage.Height)/2;
   }
   else if (StrPlace == "7")  // 在左下
   {
    IntX = 0;
    IntY = MyImage.Height-CopyMyImage.Height;
   }
   else if (StrPlace == "8")  // 在正下
   {
    IntX = (MyImage.Width-CopyMyImage.Width)/2;
    IntY = MyImage.Height-CopyMyImage.Height;
   }
   else if (StrPlace == "9")  // 在右下
   {
    IntX = MyImage.Width-CopyMyImage.Width;
    IntY = MyImage.Height-CopyMyImage.Height;
   }
   else
   {
    IntX = MyImage.Width-CopyMyImage.Width-10;
    IntY = MyImage.Height-CopyMyImage.Height-10;
   }

   // 畫出浮水印的位置
   GImage.DrawImage(CopyMyImage, IntX, IntY, CopyMyImage.Width, CopyMyImage.Height);

   string strExtend = strFileName.Substring(strFileName.LastIndexOf(".") + 1).ToLower();

   switch ( strExtend )
   {
    case "bmp" :
     OutPut.Save(strOutfileName, System.Drawing.Imaging.ImageFormat.Bmp);
     break;
    case "jpg" :
     OutPut.Save(strOutfileName, System.Drawing.Imaging.ImageFormat.Jpeg);
     break;
    case "gif" :
     OutPut.Save(strOutfileName, System.Drawing.Imaging.ImageFormat.Gif);
     break;
    case "icon" :
     OutPut.Save(strOutfileName, System.Drawing.Imaging.ImageFormat.Icon);
     break;
    case "png" :
     OutPut.Save(strOutfileName, System.Drawing.Imaging.ImageFormat.Png);
     break;
    case "tif" :
     OutPut.Save(strOutfileName, System.Drawing.Imaging.ImageFormat.Tiff);
     break;
    default :
     OutPut.Save(strOutfileName, System.Drawing.Imaging.ImageFormat.Jpeg);
     break;
   }
  
   GImage.Dispose();
   OutPut.Dispose();
   MyImage.Dispose();
   CopyMyImage.Dispose();

   if( bolFileName )
    File.Delete( strFileName );
  } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.