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();
   }
  } 

  
 }
}

posted on 2006-01-18 09:33  EasyWriter  阅读(375)  评论(0编辑  收藏  举报