產生浮水印,可按左上、左下、右上、右下、置中、透明度組建檔案,只對jpeg或jpg格式有效![原創]

來源:互聯網
上載者:User

/*
 說明:產生浮水印,可按左上、左下、右上、右下、置中、透明度組建檔案,只對jpeg或jpg格式有效!
 作者:Blue.Dream
聲明:最近發現不少網站引用本人的文章,竟將作者資訊都省略了,請引用本文的網站將作者不要省略作者的資訊.
*/
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace test
{
 ///

 /// Form1 的摘要說明。
 ///

 public class Form1 : System.Windows.Forms.Form
 {
  //聲明加密字元產生的隨機數的位元
  public const int LENGTH = 32;
  //聲明加密字元前後的分隔長度
  public const int LEN = 4;
  private System.Windows.Forms.GroupBox groupBox1;
  private System.Windows.Forms.GroupBox groupBox2;
  private System.Windows.Forms.Button btn_Browser;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.PictureBox imgSource;
  private System.Windows.Forms.PictureBox imgObject;
  private System.Windows.Forms.PictureBox imgWater;
  private System.Windows.Forms.Button btn_Water;
  private string SourceFile;
  private string WaterFile;
  private string SaveFile;
  private System.Windows.Forms.TextBox alpha;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.GroupBox groupBox3;
  private System.Windows.Forms.ComboBox local;
  ///

  /// 必需的設計器變數。
  ///

  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 表單設計器支援所必需的
   //
   InitializeComponent();
   this.Init();

   //
   // TODO: 在 InitializeComponent 調用後添加任何建構函式代碼
   //
  }

  private void Init()
  {
   DataTable ht = new DataTable();
   DataColumn objcol = new DataColumn("Keys");
   objcol.DataType = typeof(System.String);
   ht.Columns.Add(objcol);
   objcol = new DataColumn("Value");
   objcol.DataType = typeof(System.Int32);
   ht.Columns.Add(objcol);
   DataRow row = ht.NewRow();
   row[0] = "左上";
   row[1] = 1;
   ht.Rows.Add(row);
   row = ht.NewRow();
   row[0] = "左下";
   row[1] = 2;
   ht.Rows.Add(row);
   row = ht.NewRow();
   row[0] = "右上";
   row[1] = 3;
   ht.Rows.Add(row);
   row = ht.NewRow();
   row[0] = "右下";
   row[1] = 4;
   ht.Rows.Add(row);
   row = ht.NewRow();
   row[0] = "置中";
   row[1] = 5;
   ht.Rows.Add(row);

   this.local.DataSource = ht;
   this.local.DisplayMember = "Keys";
   this.local.ValueMember = "Value";
  }
  
  ///

  /// 清理所有正在使用的資源。
  ///

  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  ///

  /// 設計器支援所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  ///

  private void InitializeComponent()
  {
   this.groupBox1 = new System.Windows.Forms.GroupBox();
   this.groupBox2 = new System.Windows.Forms.GroupBox();
   this.btn_Browser = new System.Windows.Forms.Button();
   this.button2 = new System.Windows.Forms.Button();
   this.imgSource = new System.Windows.Forms.PictureBox();
   this.imgObject = new System.Windows.Forms.PictureBox();
   this.imgWater = new System.Windows.Forms.PictureBox();
   this.btn_Water = new System.Windows.Forms.Button();
   this.local = new System.Windows.Forms.ComboBox();
   this.alpha = new System.Windows.Forms.TextBox();
   this.label1 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
   this.groupBox3 = new System.Windows.Forms.GroupBox();
   this.groupBox1.SuspendLayout();
   this.groupBox2.SuspendLayout();
   this.groupBox3.SuspendLayout();
   this.SuspendLayout();
   //
   // groupBox1
   //
   this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                     this.imgSource});
   this.groupBox1.Name = "groupBox1";
   this.groupBox1.Size = new System.Drawing.Size(288, 152);
   this.groupBox1.TabIndex = 0;
   this.groupBox1.TabStop = false;
   this.groupBox1.Text = "源圖";
   //
   // groupBox2
   //
   this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
                     this.imgObject});
   this.groupBox2.Location = new System.Drawing.Point(304, 0);
   this.groupBox2.Name = "groupBox2";
   this.groupBox2.Size = new System.Drawing.Size(352, 296);
   this.groupBox2.TabIndex = 0;
   this.groupBox2.TabStop = false;
   this.groupBox2.Text = "結果圖";
   //
   // btn_Browser
   //
   this.btn_Browser.Location = new System.Drawing.Point(24, 312);
   this.btn_Browser.Name = "btn_Browser";
   this.btn_Browser.TabIndex = 1;
   this.btn_Browser.Text = "源 圖";
   this.btn_Browser.Click += new System.EventHandler(this.btn_Browser_Click);
   //
   // button2
   //
   this.button2.Location = new System.Drawing.Point(552, 312);
   this.button2.Name = "button2";
   this.button2.TabIndex = 2;
   this.button2.Text = "生 成";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // imgSource
   //
   this.imgSource.Location = new System.Drawing.Point(8, 16);
   this.imgSource.Name = "imgSource";
   this.imgSource.Size = new System.Drawing.Size(272, 128);
   this.imgSource.TabIndex = 0;
   this.imgSource.TabStop = false;
   //
   // imgObject
   //
   this.imgObject.Location = new System.Drawing.Point(8, 16);
   this.imgObject.Name = "imgObject";
   this.imgObject.Size = new System.Drawing.Size(336, 272);
   this.imgObject.TabIndex = 0;
   this.imgObject.TabStop = false;
   //
   // imgWater
   //
   this.imgWater.Location = new System.Drawing.Point(8, 16);
   this.imgWater.Name = "imgWater";
   this.imgWater.Size = new System.Drawing.Size(272, 120);
   this.imgWater.TabIndex = 1;
   this.imgWater.TabStop = false;
   //
   // btn_Water
   //
   this.btn_Water.Location = new System.Drawing.Point(120, 312);
   this.btn_Water.Name = "btn_Water";
   this.btn_Water.TabIndex = 3;
   this.btn_Water.Text = "浮水印圖";
   this.btn_Water.Click += new System.EventHandler(this.btn_Water_Click);
   //
   // local
   //
   this.local.Location = new System.Drawing.Point(256, 312);
   this.local.Name = "local";
   this.local.Size = new System.Drawing.Size(112, 20);
   this.local.TabIndex = 4;
   this.local.Text = "--請選擇--";
   //
   // alpha
   //
   this.alpha.Location = new System.Drawing.Point(448, 312);
   this.alpha.Name = "alpha";
   this.alpha.Size = new System.Drawing.Size(72, 21);
   this.alpha.TabIndex = 5;
   this.alpha.Text = "";
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(216, 312);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(32, 23);
   this.label1.TabIndex = 6;
   this.label1.Text = "位置";
   this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
   //
   // label2
   //
   this.label2.Location = new System.Drawing.Point(392, 312);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(48, 23);
   this.label2.TabIndex = 6;
   this.label2.Text = "透明度";
   this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
   //
   // groupBox3
   //
   this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
                     this.imgWater});
   this.groupBox3.Location = new System.Drawing.Point(0, 160);
   this.groupBox3.Name = "groupBox3";
   this.groupBox3.Size = new System.Drawing.Size(288, 136);
   this.groupBox3.TabIndex = 7;
   this.groupBox3.TabStop = false;
   this.groupBox3.Text = "浮水印圖";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(664, 365);
   this.Controls.AddRange(new System.Windows.Forms.Control[] {
                    this.groupBox3,
                    this.label1,
                    this.alpha,
                    this.local,
                    this.btn_Water,
                    this.button2,
                    this.btn_Browser,
                    this.groupBox1,
                    this.groupBox2,
                    this.label2});
   this.Name = "Form1";
   this.Text = "Form1";
   this.groupBox1.ResumeLayout(false);
   this.groupBox2.ResumeLayout(false);
   this.groupBox3.ResumeLayout(false);
   this.ResumeLayout(false);

  }
  #endregion

  ///

  /// 應用程式的主進入點。
  ///

  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }
  
  private void btn_Browser_Click(object sender, System.EventArgs e)
  {
   using(OpenFileDialog dlg = new OpenFileDialog())
   {
    if(dlg.ShowDialog() == DialogResult.OK)
    {
     string file = dlg.FileName;
     if(File.Exists(file))
     {      
      this.SourceFile = file;
      imgSource.Image = new Bitmap(file);
     }
    }
    dlg.Dispose();
   }
  }

  private void button2_Click(object sender, System.EventArgs e)
  {
   string wf = @"E:/Test.jpg";
   this.SaveFile = wf;
   int local = (int)(this.local.SelectedValue);
   int al = 80;
   try
   {
    al = Int32.Parse(this.alpha.Text.Trim());
   }
   catch
   {
   }
   if(MakeWaterImage(this.SourceFile,this.WaterFile,this.SaveFile,local,al))
   {
    this.imgObject.Image = new Bitmap(this.SaveFile);
   }  
   
  }

  ///

  /// 產生浮水印,可按左上、左下、右上、右下、置中、透明度組建檔案,只對jpeg或jpg格式有效!
  ///

  ///底圖
  ///浮水印圖
  ///要儲存的檔案
  ///位置:左上(1)、左下(2)、右上(3)、右下(4)、置中(5)
  ///透明度(1-100)
  /// bool,是否成功
  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;       
       break;
      case 2: //左下
       x1 = 0;
       if( (sHeight-wHeight) > 0 ) //源圖比浮水印圖高
        y1 = sHeight - wHeight;
       else
        y1 = sWidth;
       break;
      case 3: //右上
       y1 = 0;
       if( (sWidth - wWidth) > 0) // 源圖比浮水印圖寬
        x1 = sWidth - wWidth;
       else
        x1 = sWidth;
       break;
      case 4: //右下
                            //計算高度
       if( (sHeight-wHeight) > 0 ) //源圖比浮水印圖高
        y1 = sHeight - wHeight;
       else
        y1 = sWidth;
       //計算寬度
       if( (sWidth - wWidth) > 0) // 源圖比浮水印圖寬
        x1 = sWidth - wWidth;
       else
        x1 = sWidth;
       break;
      case 5: //置中
       //計算高度
       if( (sHeight-wHeight) > 0 ) //源圖比浮水印圖高
        y1 = (sHeight - wHeight) / 2;
       else
        y1 = sWidth;
       //計算寬度
       if( (sWidth - wWidth) > 0) // 源圖比浮水印圖寬
        x1 = (sWidth - wWidth) / 2;
       else
        x1 = sWidth;
       break;
     }
     if( (sHeight-wHeight) > 0)
      h = wHeight;
     else
      h = sHeight;
     if( (sWidth - wWidth) > 0)
      w = wWidth;
     else
      w = sWidth;
    }
    else //源圖比浮水印圖小
    {
     x1 = 0;
     y1 = 0;
     w = sWidth;
     h = sHeight;
    }
    
    //開始繪圖
    for(x = 1; x < w; x++)
    {
     for(y = 1; y < h; y++)
     {
      al = alpha;
      rl = wImage.GetPixel(x,y).R;
      gl = wImage.GetPixel(x,y).G;
      bl = wImage.GetPixel(x,y).B;
      al = 70;
      
      if(rl + 25 < 255)
       rl += 25;
      if(gl + 25 < 255)
       gl += 25;
      if(bl + 25 < 255)
       bl += 25;
      g.DrawEllipse(new Pen(new SolidBrush(Color.FromArgb(al,rl,gl,bl))),x1+x,y1+y,1,1);     
     }
    }   
    g.Save();
    sImage.Save(saveFile);
    result = true;
   }
   catch
   {    
    result = false;
   }
   return result;
  }

  private void btn_Water_Click(object sender, System.EventArgs e)
  {
   using(OpenFileDialog dlg = new OpenFileDialog())
   {
    if(dlg.ShowDialog() == DialogResult.OK)
    {
     string file = dlg.FileName;
     if(File.Exists(file))
     {
      this.WaterFile = file;
      imgWater.Image = new Bitmap(file);
     }
    }
    dlg.Dispose();
   }
  } 

  
 }
}

聯繫我們

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