[. Net] watermarks for Windows controls

Source: Internet
Author: User
Tags watermark images

To reference Windows controls on the web, first create a window control named editimagepint. Then,

Change usercontrol1.cs in the project to imagepint. cs. Remember to change the file name and constructor!

Create an interface first. In the design mode, drag and drop a picturebox to the solution and name it picturebox1.

Next we transfer inCodePage, that is, imagepint. CS

We need to reference the following objects:
Using system;
Using system. collections;
Using system. componentmodel;
Using system. drawing;
Using system. Data;
Using system. Windows. forms;
Using system. IO;
Using system. net;

Then we write a function.

Private void imagepint_load (Object sender, system. eventargs E)
{
/* The specific code is described below */
}

This function is used to load web pages.

Then, let's look at the code generated by the control
This. Load + = new system. eventhandler (this. imagepint_load );
Such as initializecomponent ()
# Code generated by the region component designer:

///
/// The designer supports the required methods-do not use the code editor
/// Modify the content of this method.
///
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 ";
// Have you seen it? Obviously, the first thing we add is
This. Load + = new system. eventhandler (this. imagepint_load );
This. resumelayout (false );

}
# Endregion

With these things, components can be used on the Web. However, since they are watermarks, they should have original images, watermark images, and some other parameters, here we will use public functions. As for how the Web transfers values, let's talk about them below.

# Region Public attributes

// Display width
Public int imgwidht
{
Get {return _ imgwidth ;}
Set {_ imgwidth = value ;}
}

// Display height
Public int imgheight
{
Get {return _ imgheight ;}
Set {_ imgheight = value ;}
}

// Transparency
Private int alpha
{
Get {return _ Alpha ;}
Set {_ alpha = value ;}
}

// Master image address
Public String zpicture
{
Get {return _ zpicture ;}
Set {_ zpicture = value ;}
}

// Watermark image address
Public String fpicture
{
Get {return _ fpicture ;}
Set {_ fpicture = value ;}
}

# Endregion

 

The complete code is provided below:

Code copy box
Using system; <br/> using system. collections; <br/> using system. componentmodel; <br/> using system. drawing; <br/> using system. data; <br/> using system. windows. forms; <br/> using system. io; <br/> using system. net; <br/> namespace editimagepint <br/>{< br/> /// <summary> <br/> /// Summary of usercontrol1. <Br/> // </Summary> <br/> public class imagepint: system. windows. forms. usercontrol <br/> {<br/> /// <summary> <br/> // required designer variables. <Br/> // </Summary> <br/> private system. componentmodel. container components = NULL; <br/> Public imagepint () <br/>{< br/> // The call is windows. required by the forms Form Designer. <Br/> initializecomponent (); <br/> // todo: after initcomponent calls, add any initialization <br/>}< br/> /// <summary> <br/> /// clear all resources in use. <Br/> // </Summary> <br/> protected override void dispose (bool disposing) <br/>{< br/> If (disposing) <br/>{< br/> If (components! = NULL) <br/> components. dispose (); <br/>}< br/> base. dispose (disposing ); <br/>}< br/> # code generated by the region component designer <br/> /// <summary> <br/> // The method required by the designer -Do not use the Code Editor <br/> /// to modify the content of this method. <Br/> // </Summary> <br/> private void initializecomponent () <br/>{< br/> This. picturebox1 = new system. windows. forms. picturebox (); <br/> This. suspendlayout (); <br/> // picturebox1 <br/> // <br/> This. picturebox1.location = new system. drawing. point (0, 0); <br/> This. picturebox1.name = "picturebox1"; <br/> This. picturebox1.tabindex = 0; <br/> This. picturebox1.tabstop = false; <br/> // imagepi NT <br/> // <br/> This. controls. add (this. picturebox1); <br/> This. name = "imagepint"; <br/> This. load + = new system. eventhandler (this. imagepint_load); <br/> This. resumelayout (false); <br/>}< br/> # endregion <br/> private bitmap simage; <br/> private int swidth; <br/> private int sheight; <br/> private int _ imgwidth; <br/> private system. windows. forms. picturebox picturebox1; <br/> private int _ imgheight; <br/> Private int _ Alpha; <br/> private string _ zpicture = string. empty; <br/> private string _ fpicture = string. empty; <br/> # Region Public attributes <br/> // display width <br/> Public int imgwidht <br/>{< br/> get {return _ imgwidth ;} <br/> set {_ imgwidth = value ;} <br/>}< br/> // display height <br/> Public int imgheight <br/>{< br/> get {return _ imgheight ;} <br/> set {_ imgheight = value ;} <br/>}< br/> // transparency <br/> private int Alpha <br/>{< br/> G Et {return _ Alpha ;}< br/> set {_ alpha = value ;} <br/>}< br/> // master image address <br/> Public String zpicture <br/>{< br/> get {return _ zpicture ;} <br/> set {_ zpicture = value ;} <br/>}< br/> // watermark image address <br/> Public String fpicture <br/>{< br/> get {return _ fpicture ;} <br/> set {_ fpicture = value ;}< br/>}< br/> # endregion <br/> private void imagepint_load (Object sender, system. eventargs e) <br/>{< br/> string sourcefile = _ Zpicture; <br/> string watermarkfile = _ fpicture; <br/> // string sourcefile = @ "http: // localhost/image/my.jpg "; <br/> // string watermarkfile = @ "http: // localhost/image/make.jpg"; <br/> simage = tmpimage (sourcefile ); <br/> // simage. width = _ imgwidth = 0? Simage. Width: _ imgwidth; <br/> // simage. Height = _ imgheight = 0? Simage. Height: _ imgheight; <br/> swidth = _ imgwidth = 0? Simage. Width: _ imgwidth; <br/> sheight = _ imgheight = 0? Simage. height: _ imgheight; <br/> This. width = swidth; <br/> This. height = sheight; <br/> picturebox1.width = swidth; <br/> picturebox1.height = sheight; <br/> This. makewaterimage (watermarkfile); <br/>}< br/> Public bitmap tmpimage (string strurl) <br/>{< br/> Bitmap bitmap; <br/> If (strurl. indexof ("HTTP") <0) <br/>{< br/> bitmap = new Bitmap (strurl); <br/> return bitmap; <br/>}< br/> else <br/> {<br/> WebClient W Ebclient = new WebClient (); <br/> Stream Data = WebClient. openread (strurl); <br/> bitmap = new Bitmap (data); <br/> return bitmap; <br/>}< br/> // method: generate a watermark image <br/> // sourcefile: image File for generating the watermark <br/> // watermarkfile: Image File for storing the watermark <br/> Public void makewaterimage (string watermarkfile) <br/>{< br/> try <br/> {<br/> // watermark image <br/> bitmap wimage = tmpimage (watermarkfile ); <br/> int wwidth = wimage. width; <br/> int wheig Ht = wimage. height; <br/> // Make graphics. <br/> graphics G = graphics. fromimage (simage); <br/> int X; // Temporary Variable <br/> int y; // monitoring variable <br/> int x1; // width difference between the source image and the watermark image <br/> int Y1; // height difference between the source image and the watermark image <br/> int W; // width of the generated watermark image <br/> int h; // height of the generated watermark image <br/> int al; // Alpha <br/> int RL; // red <br/> int GL; // green <br/> int BL; // blue <br/> If (swidth> wwidth) <br/>{< br/> X1 = swidth-wwidth; <br/> Y1 = sheight-wheight; <br/> W = wwidth; <br/> H = wheight; <br/>}< br/> else <br/>{< br/> X1 = 0; <br/> Y1 = 0; <br/> W = swidth; <br/> H = sheight; <br/>}< br/> // start plotting <br/> for (x = 1; x <W; X ++) <br/> {<br/> for (y = 1; y <p; y ++) <br/> {<br/> Al = wimage. getpixel (x, y ). a; <br/> RL = wimage. getpixel (x, y ). r; <br/> GL = wimage. getpixel (x, y ). g; <br/> BL = wimage. getpixel (x, y ). b; <br/> Al = _ alpha = 0? 30: _ Alpha; <br/> If (RL + 25 <255) <br/> RL + = 25; <br/> If (gL + 25 <255) <br/> gL + = 25; <br/> If (BL + 25 <255) <br/> BL + = 25; <br/> G. drawellipse (new pen (New solidbrush (color. fromargb (Al, rl, GL, BL), X1 + X, Y1 + Y, 1, 1); <br/>}< br/> G. save (); <br/> picturebox1.image = simage; <br/>}< br/> catch (exception ex) <br/>{< br/> MessageBox. show (ex. message); <br/>}< br/>
[Ctrl + A select all and copy]

Then, we generate a release version for this control, and copy the generated DLL file to your virtual directory, which is then called by the web. First, we create a virtual directory, for example, the created virtual directory andAddress:
Localhost/Object/imageprint/
Copy the generated editimagepint. dll file to this directory.
Create an HTML file and put the following code in it:

<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>

As you can see, these Param are our public attributes :), so that the value is passed in.

The result is as follows:

Sorry, GIF is not supported yet.

Download source code:Click to download

Remember that the object classid in HTML needs to be changed according to your virtual directory

Classic forum discussion posts:
Http://www.blueidea.com/bbs/newsdetail.asp? Id = 2553689

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.