In this demo, you can see how to specify brush to write watermark text. You only need to select a Brush style, foreground color, and the background color, then click the [submit] button. The watermark text will display shortly in the below image.
The code of this demo is a little more complicated. CoolWatermark can support any brush, as long as it inherits from System.Drawing.Brush; also, CoolWatermark provide several built-in BrushStyle. When you select BrushStyle.Customize, you need to specify the Brush property of the Writer. In this demo, I was trying to show you how to use the built-in brushes and some customized brushes. Hence, the code must handle the logic that determines if you want a built-in brush or customized brush. The following code snippet is how I generate the dropdown shape style in this page. You can see I put all HatchStyles and CoolWatermark BrushStyle in one dropdown.
1: private void initDropDown()
2: {
3: string[] fs = Enum.GetNames(typeof(IC.CoolWatermark.BrushStyle));
4:
5: int counter = 0;
6: foreach (string fss in fs)
7: {
8: if (fss != "Customize")
9: {
10: ListItem item = new ListItem(fss, counter.ToString());
11: this.ddlBrush.Items.Add(item);
12: }
13: counter++;
14: }
15:
16: fs = Enum.GetNames(typeof(System.Drawing.Drawing2D.HatchStyle));
17: foreach (string fss in fs)
18: {
19: ListItem item = new ListItem(fss, counter.ToString());
20: counter++;
21: this.ddlBrush.Items.Add(item);
22: }
23:
24: }
In the line 8, I skipped the "Customiz" style, because I will assign this style if necessary. The value of each dropdown item is a counter, which I will use to determine which one is selected. The next code is the main piece to draw the image.
1: Writer writer = new Writer(canvas);
2: int totalBrushStyle = 0;
3: // I want to find out how many IC.CoolWatermark.BrushStyle we have
4: string[] fs = Enum.GetNames(typeof(IC.CoolWatermark.BrushStyle));
5: totalBrushStyle = fs.Length;
6:
7: try
8: {
9: // this number will give me which style the user selects
10: int b = int.Parse(Request.QueryString["brush"]);
11:
12: // if it's a hatchbrush style
13: if (b >= totalBrushStyle)
14: {
15: // then it means we have a customized brush
16: writer.BrushStyle = BrushStyle.Customize;
17:
18: // get the actual select index so that we can do the conversion
19: b = b - totalBrushStyle;
20:
21: // this customized brush is a hatchbrush
22: writer.Brush = new HatchBrush((HatchStyle)b,
23: Color.FromName(Request.QueryString["forecolor"]),
24: Color.FromName(Request.QueryString["backcolor"]));
25: }
26: else
27: {
28: writer.BrushStyle = (BrushStyle)b;
29: writer.Colors = new Color[] {
30: Color.FromName(Request.QueryString["forecolor"]),
31: Color.FromName(Request.QueryString["backcolor"]) };
32: }
33: }
34: catch
35: {
36: // if anything wrong, we will use a default brush
37: writer.BrushStyle = BrushStyle.Default;
38: }
39:
40: writer.PositionStyle = PositionStyle.BottomRight;
41: writer.FontFamily = new FontFamily("Arial");
42: writer.FontSize = 28;
43: System.Drawing.Image result = writer.WriteText("ImageComponent.NET");
44: MemoryStream oStream = new MemoryStream();
45: result.Save(oStream, System.Drawing.Imaging.ImageFormat.Jpeg);
46: Response.BinaryWrite(oStream.ToArray());
You can see at line 13, if the user selects a HatchStyle, it means I need to tell CoolWatermark to use Customize brush. So I specify it at line 16. And I assign a HatchBrush at line 22 by using the calculated HatchStyle, foreground color, and background color. (Sorry the source code in this demo is written in C#, please let me know if you are seeking VB.NET version)
[Back to Top]