
我们把项目下的UserControl cs改为ImagePint cs 切记文件名和构造函数都改!
先制作界面 首先在设计模式下 我们将一个PictureBox拖放到解面上 命名为pictureBox
下面我们转入代码页 也就是ImagePint cs
我们需要引用的对象有 using Systemusing System Collectionsusing System ComponentModelusing System Drawingusing System Datausing System Windows Formsusing System IOusing 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 pictureBox = new System Windows Forms PictureBox()this SuspendLayout()// // pictureBox // this pictureBox Location = new System Drawing Point( )this pictureBox Name = pictureBox this pictureBox TabIndex = this pictureBox TabStop = false// // ImagePint// this Controls Add(this pictureBox )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 Systemusing System<fp class='fp-6u1wv'></fp>Collectionsusing System<fp class='fp-jrh3l'></fp>ComponentModelusing System<fp class='fp-46co0'></fp>Drawingusing System<fp class='fp-6u1wv'></fp>Datausing System<fp class='fp-ir9zx'></fp>Windows<fp class='fp-46co0'></fp>Formsusing System<fp class='fp-jrh3l'></fp>IOusing System<fp class='fp-6u1wv'></fp>Netnamespace EditImagePint{ /// <summary>/// UserControl<fp class='fp-2norb'></fp>的摘要说明<fp class='fp-fk4z2'></fp>/// </summary>public class ImagePint : System<fp class='fp-ir9zx'></fp>Windows<fp class='fp-46co0'></fp>Forms<fp class='fp-jrh3l'></fp>UserControl { /// <summary>/// 必需的<a _blank<fp class='fp-107o5'></fp>>设计</a>器变量<fp class='fp-3ivmu'></fp>/// </summary>private System<fp class='fp-46co0'></fp>ComponentModel<fp class='fp-jrh3l'></fp>Container ponents = nullpublic ImagePint() { // 该调用是 Windows<fp class='fp-jrh3l'></fp>Forms 窗体设计器所必需的<fp class='fp-ybmj6'></fp>InitializeComponent()// TODO: 在 InitComponent 调用后添加任何初始化 } /// <summary>/// 清理所有正在使用的资源<fp class='fp-fk4z2'></fp>/// </summary>protected override void Dispose( bool disposing ) { if( disposing ) { if( ponents != null ) ponents<fp class='fp-46co0'></fp>Dispose()} base<fp class='fp-46co0'></fp>Dispose( disposing )} #region 组件设计器生成的代码 /// <summary>/// 设计器支持所需的方法 <fp class='fp-16ygi'></fp>不要使用代码编辑器 /// 修改此方法的内容<fp class='fp-ybmj6'></fp>/// </summary>private void InitializeComponent() { this<fp class='fp-46co0'></fp>pictureBox<fp class='fp-naa43'></fp>= new System<fp class='fp-46co0'></fp>Windows<fp class='fp-jrh3l'></fp>Forms<fp class='fp-46co0'></fp>PictureBox()this<fp class='fp-46co0'></fp>SuspendLayout()// // pictureBox<fp class='fp-naa43'></fp>// this<fp class='fp-jrh3l'></fp>pictureBox<fp class='fp-x5mm4'></fp><fp class='fp-ir9zx'></fp>Location = new System<fp class='fp-46co0'></fp>Drawing<fp class='fp-jrh3l'></fp>Point(<fp class='fp-lrm6f'></fp><fp class='fp-g8k18'></fp><fp class='fp-gr3w3'></fp>)this<fp class='fp-6u1wv'></fp>pictureBox<fp class='fp-x5mm4'></fp><fp class='fp-46co0'></fp>Name = <fp class='fp-61so8'></fp>pictureBox<fp class='fp-x5mm4'></fp><fp class='fp-aduwd'></fp>this<fp class='fp-46co0'></fp>pictureBox<fp class='fp-kfx6r'></fp><fp class='fp-46co0'></fp>TabIndex = <fp class='fp-gr3w3'></fp>this<fp class='fp-jrh3l'></fp>pictureBox<fp class='fp-2norb'></fp><fp class='fp-ir9zx'></fp>TabStop = false// // ImagePint // this<fp class='fp-jrh3l'></fp>Controls<fp class='fp-jrh3l'></fp>Add(this<fp class='fp-46co0'></fp>pictureBox<fp class='fp-2norb'></fp>)this<fp class='fp-6u1wv'></fp>Name = <fp class='fp-bd212'></fp>ImagePint<fp class='fp-aduwd'></fp>this<fp class='fp-6u1wv'></fp>Load += new System<fp class='fp-ir9zx'></fp>EventHandler(this<fp class='fp-ir9zx'></fp>ImagePint_Load)this<fp class='fp-jrh3l'></fp>ResumeLayout(false)} #endregion private Bitmap sImageprivate int sWidthprivate int sHeightprivate int _ImgWidthprivate System<fp class='fp-ir9zx'></fp>Windows<fp class='fp-6u1wv'></fp>Forms<fp class='fp-jrh3l'></fp>PictureBox pictureBox<fp class='fp-naa43'></fp>private int _ImgHeightprivate int _Alphaprivate string _ZPicture = String<fp class='fp-46co0'></fp>Emptyprivate string _FPicture = String<fp class='fp-jrh3l'></fp>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<fp class='fp-56ktp'></fp>System<fp class='fp-ir9zx'></fp>EventArgs e) { string sourceFile = _ZPicturestring waterMarkFile = _FPicture//string sourceFile = @<fp class='fp-107o5'></fp><fp class='fp-bd212'></fp>//string waterMarkFile = @<fp class='fp-107o5'></fp><fp class='fp-107o5'></fp>sImage = tmpImage(sourceFile)//sImage<fp class='fp-6u1wv'></fp>Width = _ImgWidth==<fp class='fp-mmu1y'></fp>?sImage<fp class='fp-46co0'></fp>Width:_ImgWidth//sImage<fp class='fp-46co0'></fp>Height = _ImgHeight==<fp class='fp-lrm6f'></fp>?sImage<fp class='fp-jrh3l'></fp>Height:_ImgHeightsWidth = _ImgWidth==<fp class='fp-gr3w3'></fp>?sImage<fp class='fp-6u1wv'></fp>Width:_ImgWidthsHeight = _ImgHeight==<fp class='fp-gr3w3'></fp>?sImage<fp class='fp-jrh3l'></fp>Height:_ImgHeightthis<fp class='fp-46co0'></fp>Width = sWidththis<fp class='fp-ir9zx'></fp>Height = sHeightpictureBox<fp class='fp-2norb'></fp><fp class='fp-6u1wv'></fp>Width = sWidthpictureBox<fp class='fp-kfx6r'></fp><fp class='fp-jrh3l'></fp>Height = sHeightthis<fp class='fp-46co0'></fp>MakeWaterImage(waterMarkFile)} public Bitmap tmpImage(string strUrl) { Bitmap bitmapif(strUrl<fp class='fp-ir9zx'></fp>IndexOf(<fp class='fp-61so8'></fp><fp class='fp-bd212'></fp>)<<fp class='fp-lrm6f'></fp>) { bitmap = new Bitmap(strUrl)return bitmap} else { WebClient webClient = new WebClient()Stream data = webClient<fp class='fp-6u1wv'></fp>OpenRead(strUrl)bitmap = new Bitmap(data)return bitmap} } //方法作用:生成水印图片 //sourceFile:要生成水印的图片文件 //WaterMarkFile:存放水印的图片文件 public void MakeWaterImage(string waterMarkFile) { try { //水印图 Bitmap wImage = tmpImage(waterMarkFile)int wWidth = wImage<fp class='fp-6u1wv'></fp>Widthint wHeight = wImage<fp class='fp-46co0'></fp>Height//make Graphics<fp class='fp-6u1wv'></fp>Graphics g = Graphics<fp class='fp-ir9zx'></fp>FromImage(sImage)int x//临时变量 int y//监时变量 int x<fp class='fp-naa43'></fp>//原图和水印图的宽度差 int y<fp class='fp-2norb'></fp>//原图和水印图的高度差 int w//生成的水印图的宽度 int h//生成的水印图的高度 int al//alpha int rl//Red int gl//Green int bl//Blue if(sWidth >wWidth) { x<fp class='fp-kfx6r'></fp>= sWidth <fp class='fp-th *** g'></fp>wWidthy<fp class='fp-2norb'></fp>= sHeight <fp class='fp-th *** g'></fp>wHeightw = wWidthh = wHeight} else { x<fp class='fp-naa43'></fp>= <fp class='fp-mmu1y'></fp>y<fp class='fp-naa43'></fp>= <fp class='fp-lrm6f'></fp>w = sWidthh = sHeight} //开始绘图 for(x = <fp class='fp-naa43'></fp>x <wx++) { for(y = <fp class='fp-naa43'></fp>y <hy++) { al = wImage<fp class='fp-ir9zx'></fp>GetPixel(x<fp class='fp-g8k18'></fp>y)<fp class='fp-ir9zx'></fp>Arl = wImage<fp class='fp-ir9zx'></fp>GetPixel(x<fp class='fp-56ktp'></fp>y)<fp class='fp-46co0'></fp>Rgl = wImage<fp class='fp-46co0'></fp>GetPixel(x<fp class='fp-g8k18'></fp>y)<fp class='fp-jrh3l'></fp>Gbl = wImage<fp class='fp-jrh3l'></fp>GetPixel(x<fp class='fp-56ktp'></fp>y)<fp class='fp-ir9zx'></fp>Bal = _Alpha==<fp class='fp-gr3w3'></fp>?<fp class='fp-zz2lc'></fp><fp class='fp-gr3w3'></fp>:_Alphaif(rl + <fp class='fp-l3e9x'></fp><fp class='fp-qaocl'></fp><<fp class='fp-k3649'></fp><fp class='fp-7dhov'></fp><fp class='fp-qaocl'></fp>) rl += <fp class='fp-cwit2'></fp><fp class='fp-qaocl'></fp>if(gl + <fp class='fp-k3649'></fp><fp class='fp-qaocl'></fp><<fp class='fp-v4hjb'></fp><fp class='fp-xcdeh'></fp><fp class='fp-qaocl'></fp>) gl += <fp class='fp-v4hjb'></fp><fp class='fp-7dhov'></fp>if(bl + <fp class='fp-k3649'></fp><fp class='fp-xcdeh'></fp><<fp class='fp-cwit2'></fp><fp class='fp-qaocl'></fp><fp class='fp-qaocl'></fp>) bl += <fp class='fp-v4hjb'></fp><fp class='fp-7dhov'></fp>g<fp class='fp-jrh3l'></fp>DrawEllipse(new Pen(new SolidBrush(Color<fp class='fp-6u1wv'></fp>FromArgb(al<fp class='fp-8p5pj'></fp>rl<fp class='fp-g8k18'></fp>gl<fp class='fp-56ktp'></fp>bl)))<fp class='fp-g8k18'></fp>x<fp class='fp-naa43'></fp>+x<fp class='fp-56ktp'></fp>y<fp class='fp-kfx6r'></fp>+y<fp class='fp-gkc4q'></fp><fp class='fp-2norb'></fp><fp class='fp-gkc4q'></fp><fp class='fp-2norb'></fp>)} } g<fp class='fp-6u1wv'></fp>Save()pictureBox<fp class='fp-naa43'></fp><fp class='fp-46co0'></fp>Image = sImage} catch(Exception ex) { MessageBox<fp class='fp-46co0'></fp>Show(ex<fp class='fp-46co0'></fp>Message)} } }} [Ctrl+A 全部选择 然后拷贝]
然后我们把这个控件生成一个Release版本 然后把生成的dll文件copy到你的虚拟目录下 然后就是web调用了 我们先建立一个虚拟目录 比如说我们建立的虚拟目录及地址为 localhost/Object/ImagePrint/我们就把生成的 EditImagePint dll 文件copy到这个目录下并建立一个文件 把以下的代码放进去
<object id= print classid=//localhost/Object/ImagePrint/EditImagePint dll#EditImagePint ImagePint Width= Height= VIEWASTEXT ><param name= ImgWidht value= ><param name= ImgHeight value= ><param name= Alpha value= ><param name= ZPicture value= ><param name= FPicture value= ></object>
看到了把 这些param就是我们的公共属性了 ) 这样就把值传递进去了
最终显示如下
对不起大家 目前还不支持GIF
lishixinzhi/Article/program/net/201311/13537需要引入 System.Drawing命名空间(必要的话还有System.Drawing.Imaging),调用 Graphics对象 (有静态方法可以从Image对象中创建),重新绘图就可以了,或者你的水印就是个图,图片叠加,当然也可以绘制文字了,具体Graphics的使用可以参看 MSDN。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)