用.net实现按透明度生成水印文件

转载 2004年09月22日 23:00:00
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
  {
  /// <summary>
  /// Form1 的摘要说明。
  /// </summary>
  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;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  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";
  }
  
  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
{
  if( disposing )
  {
  if (components != null)
  {
  components.Dispose();
  }
  }
  base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  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

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [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);
  } 
  
  }
/// <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; 
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();
  }
  }

  
  }
  }

iTextSharp简单生成pdf和操作pdf添加水印

转自 http://www.cnblogs.com/LL-723/archive/2013/12/25/3490699.html public class PDFSetWaterMark { ...
  • shouhou_Bingo
  • shouhou_Bingo
  • 2016年08月30日 17:57
  • 2842

php文字水印和php图片水印实现代码

文字水印文字水印就是在图片上加上文字,主要使用gd库的imagefttext方法,并且需要字体文件。参考链接地址:http://www.zhiliaoke.com.cn/ 案例中将图片生成存入文件夹...
  • vailook
  • vailook
  • 2016年08月30日 11:59
  • 5129

C#中基于GDI+(Graphics)图像处理系列之文字或者图片水印(透明、任意角度旋转)

简介 给图片添加水印是比较常用的功能,通常是用于给图片添加版权的信息或者作者信息。 本文将重点向大家介绍怎么使用GDI+(Graphics)给图像添加图片水印和文字水印。 图片水印 技...
  • lhtzbj12
  • lhtzbj12
  • 2017年01月07日 18:00
  • 1712

asp.net中文水印透明度

  • 2009年11月11日 18:57
  • 1006KB
  • 下载

asp.net上传功能(单文件,多文件,自定义生成缩略图,水印)

前言 上传功能,是大家经常用到了,可能每一个项目都可以会用到。网上到处都有上传功能的代码。比我写的好的有很多。我这里也仅是分享我的代码。   功能实现点 1.单个文件上传; 2.多个文件上传...
  • zhanzkw
  • zhanzkw
  • 2011年11月01日 12:30
  • 544

asp.net上传图片并自动生成缩略图和给图片水印

无标题页 upfile.aspx.cs文件 using System; u...
  • youngshao1025
  • youngshao1025
  • 2012年09月26日 17:43
  • 350

Aspose.Words for .NET动态生成word文档中的图片或水印

1、概述   在项目中生成word文档,这个功能很普遍的,一般生成都是纯文字或是列表的比较多,便于客户打印,而要把图片也生成到word文档中的需求有些客户也是需要的,例如产品图片。这次我们介绍的...
  • ysq5202121
  • ysq5202121
  • 2012年10月26日 16:28
  • 1810

asp.net上传图片+自动生成缩略图+图片水印功能

实现过程:选择图片上传成功后,取得已经存在服务器的文件生成缩略图,并且判断是否是图片类型的文件,这个的判断可以在程序中修改,本程序只是判断了“image/bmp”、“image/gif”、“image...
  • vera514514
  • vera514514
  • 2012年08月03日 14:31
  • 288

使用ASP.NET Image Generation生成图片缩略图及水印

ASP.NET Image Generation内置了ImageResizeTransform类,可以实现图片大小调整功能。也可以扩 展ImageTransform实现自己的图片变换类。 下面使用A...
  • xiaoyecanfeng
  • xiaoyecanfeng
  • 2011年11月12日 09:01
  • 178

asp.net如何在图片上加水印文字具体实现

这篇文章主要介绍了asp.net如何在图片上加水印文字具体实现,有需要的朋友可以参考一下 第一步,添加一个一般处理程序(Handler),本例是ImageHandler 复制代...
  • my98800
  • my98800
  • 2017年12月13日 09:09
  • 77
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:用.net实现按透明度生成水印文件
举报原因:
原因补充:

(最多只允许输入30个字)