C#.NET windows控件实现水印

Windows控件实现在Web应用
本文介绍如何创建一个Windows控件并在Web应用中使用它实现图片水印功能。通过修改控件代码和利用HTML对象标签,实现了动态加载和设置控件属性。
 

Windows控件在Web下的引用,首先我们建立一个window控件,项目名称为EditImagePint,然后,

C#.NET windows控件实现水印(图一)

我们把项目下的UserControl1.cs改为ImagePint.cs,切记文件名和构造函数都改!

先制作界面,首先在设计模式下,我们将一个PictureBox拖放到解面上,命名为pictureBox1

C#.NET windows控件实现水印(图二)

C#.NET windows控件实现水印(图三)

下面我们转入代码页,也就是ImagePint.cs

我们需要引用的对象有:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Net;

然后我们写一个函数

private void ImagePint_Load(object sender, System.EventArgs e)
{
/*具体代码我们下面介绍*/
}

这个函数,是为了在web页面使用时候加载使用的.

然后我们看控件自己的生成代码,把
this.Load += new System.EventHandler(this.ImagePint_Load);
加如InitializeComponent()中
#region 组件设计器生成的代码:

 

///
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
///
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// ImagePint
//
this.Controls.Add(this.pictureBox1);
this.Name = "ImagePint";
//看到了吗?很明显,先面的是我们加上去的
this.Load += new System.EventHandler(this.ImagePint_Load);
this.ResumeLayout(false);

}
#endregion

有了这些东西,组件就可以在web下使用了,但是既然是水印,就应该有原始图片,和水印图片,及一些其他的参数,这里我们就用公共函数,至于web怎么把值传进去,我们到下面再说

 

#region 公共属性

//显示宽度
public int ImgWidht
{
get {return _ImgWidth;}
set {_ImgWidth = value;}
}

//显示高度
public int ImgHeight
{
get {return _ImgHeight;}
set {_ImgHeight = value;}
}

//透明度
private int Alpha
{
get {return _Alpha;}
set {_Alpha = value;}
}

//主图片地址
public string ZPicture
{
get {return _ZPicture;}
set {_ZPicture = value;}
}

//水印图片地址
public string FPicture
{
get {return _FPicture;}
set {_FPicture = value;}
}

#endregion

 

下面把完整的代码贡献给大家:

代码拷贝框
using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms; using System.IO; using System.Net; namespace EditImagePint { /// <summary> /// UserControl1 的摘要说明。 /// </summary> public class ImagePint : System.Windows.Forms.UserControl { /// <summary> /// 必需的<a href="http://www.qqread.com/keywords/photoshop_e.html" target="_blank">设计</a>器变量。 /// </summary> private System.ComponentModel.Container components = null; public ImagePint() { // 该调用是 Windows.Forms 窗体设计器所必需的。 InitializeComponent(); // TODO: 在 InitComponent 调用后添加任何初始化 } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if( components != null ) components.Dispose(); } base.Dispose( disposing ); } #region 组件设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器 /// 修改此方法的内容。 /// </summary> private void InitializeComponent() { this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.SuspendLayout(); // // pictureBox1 // this.pictureBox1.Location = new System.Drawing.Point(0, 0); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; // // ImagePint // this.Controls.Add(this.pictureBox1); this.Name = "ImagePint"; this.Load += new System.EventHandler(this.ImagePint_Load); this.ResumeLayout(false); } #endregion private Bitmap sImage; private int sWidth; private int sHeight; private int _ImgWidth; private System.Windows.Forms.PictureBox pictureBox1; private int _ImgHeight; private int _Alpha; private string _ZPicture = String.Empty; private string _FPicture = String.Empty; #region 公共属性 //显示宽度 public int ImgWidht { get {return _ImgWidth;} set {_ImgWidth = value;} } //显示高度 public int ImgHeight { get {return _ImgHeight;} set {_ImgHeight = value;} } //透明度 private int Alpha { get {return _Alpha;} set {_Alpha = value;} } //主图片地址 public string ZPicture { get {return _ZPicture;} set {_ZPicture = value;} } //水印图片地址 public string FPicture { get {return _FPicture;} set {_FPicture = value;} } #endregion private void ImagePint_Load(object sender, System.EventArgs e) { string sourceFile = _ZPicture; string waterMarkFile = _FPicture; //string sourceFile = @"http://localhost/Image/my.jpg"; //string waterMarkFile = @"http://localhost/Image/make.jpg"; sImage = tmpImage(sourceFile); //sImage.Width = _ImgWidth==0?sImage.Width:_ImgWidth; //sImage.Height = _ImgHeight==0?sImage.Height:_ImgHeight; sWidth = _ImgWidth==0?sImage.Width:_ImgWidth; sHeight = _ImgHeight==0?sImage.Height:_ImgHeight; this.Width = sWidth; this.Height = sHeight; pictureBox1.Width = sWidth; pictureBox1.Height = sHeight; this.MakeWaterImage(waterMarkFile); } public Bitmap tmpImage(string strUrl) { Bitmap bitmap; if(strUrl.IndexOf("http")<0) { bitmap = new Bitmap(strUrl); return bitmap; } else { WebClient webClient = new WebClient(); Stream data = webClient.OpenRead(strUrl); bitmap = new Bitmap(data); return bitmap; } } //方法作用:生成水印图片 //sourceFile:要生成水印的图片文件 //WaterMarkFile:存放水印的图片文件 public void MakeWaterImage(string waterMarkFile) { try { //水印图 Bitmap wImage = tmpImage(waterMarkFile); int wWidth = wImage.Width; int wHeight = wImage.Height; //make Graphics. Graphics g = Graphics.FromImage(sImage); int x; //临时变量 int y; //监时变量 int x1; //原图和水印图的宽度差 int y1; //原图和水印图的高度差 int w; //生成的水印图的宽度 int h; //生成的水印图的高度 int al; //alpha int rl; //Red int gl; //Green int bl; //Blue if(sWidth > wWidth) { x1 = sWidth - wWidth; y1 = sHeight - wHeight; w = wWidth; h = wHeight; } else { x1 = 0; y1 = 0; w = sWidth; h = sHeight; } //开始绘图 for(x = 1; x < w; x++) { for(y = 1; y < h; y++) { al = wImage.GetPixel(x,y).A; rl = wImage.GetPixel(x,y).R; gl = wImage.GetPixel(x,y).G; bl = wImage.GetPixel(x,y).B; al = _Alpha==0?30:_Alpha; 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(); pictureBox1.Image = sImage; } catch(Exception ex) { MessageBox.Show(ex.Message); } } } }
[Ctrl+A 全部选择 然后拷贝]

然后我们把这个控件生成一个Release版本,然后把生成的dll文件copy到你的虚拟目录下,然后就是web调用了,我们先建立一个虚拟目录,比如说我们建立的虚拟目录及地址为:
localhost/Object/ImagePrint/
我们就把生成的 EditImagePint.dll 文件copy到这个目录下
并建立一个html文件,把以下的代码放进去:

<object id="print" classid="http://localhost/Object/ImagePrint/
EditImagePint.dll#EditImagePint.ImagePint"
Width="177" Height="144" VIEWASTEXT >
<param name="ImgWidht" value="177">
<param name="ImgHeight" value="144">
<param name="Alpha" value="40">
<param name="ZPicture" value="http://localhost/Object/ImagePrint/my.jpg">
<param name="FPicture" value="http://localhost/Object/ImagePrint/make.jpg">
</object>

看到了把,这些param就是我们的公共属性了:),这样就把值传递进去了

最终显示如下:

C#.NET windows控件实现水印(图四)

转自图中的那位帅哥!

转载于:https://www.cnblogs.com/tanghuawei/archive/2007/01/12/618935.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值