用.net實現按透明度產生浮水印檔案

來源:互聯網
上載者:User
         /// <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;      

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.