分享
 
 
 

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

王朝c#·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

说明:生成水印,可按左上、左下、右上、右下、居中、透明度生成文件,只对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

{

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

}

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有