Скачиваний:
64
Добавлен:
15.03.2015
Размер:
4.31 Mб
Скачать

GDI+: The .NET Graphics Interface

371

CHAPTER 3.5

FIGURE 3.5.14

Alpha blending a bitmap image.

Other Color Space Manipulations

Having a whole huge matrix for alpha blending alone seems like overkill, until you realize that any manipulation of the color space is possible with such a tool, if only you knew what numbers to plumb in. The technique of manipulating such a matrix for the purpose of changing the color space is called recoloring and is also easy to do with GDI+. Once again, the tool used is the ColorMatrix and the vehicle is the ImageAttributes. Unfortunately, there are no methods on the ColorMatrix to perform color space rotations, but they would be possible, given the correct settings of the linear part of the matrix. As an exercise, you could send in a modification of Listing 3.5.10 to sams@netedgesoftware.com that does color space rotations. The best 10 correct ones within the first year of publication of this book get a free NetEdge Software Polo shirt. To get you started, Listing 3.5.11 is a modification of the previous that allows you to set the red, green, and blue component levels of your chosen image.

LISTING 3.5.11 ColorSpace1.cs: More Color Space Transformations

1:using System;

2:using System.Drawing;

3:using System.Drawing.Drawing2D;

4:using System.Drawing.Imaging;

5:using System.Collections;

6:using System.ComponentModel;

7:using System.Windows.Forms;

8:using System.Data;

9:

10: namespace ColorSpace1

3.5

T GDI+:RAPHICSGNTERFACEI . HE NET

Windows Forms

372

PART III

LISTING 3.5.11 Continued

11:{

12:class Form1 : Form

13:{

14:

15:Button b;

16:TrackBar tr,tg,tb;

17:Image i;

18:

19:void OnPaint(object Sender,PaintEventArgs e)

20:{

21:SolidBrush b=new SolidBrush(Color.Red);

22:Rectangle r=this.ClientRectangle;

23:GraphicsPath pth=new GraphicsPath();

24:if(i!=null)

25:{

26:ColorMatrix m=new ColorMatrix();

27:m.Matrix00=(float)(1.0/256*tr.Value);

28:m.Matrix11=(float)(1.0/256*tg.Value);

29:m.Matrix22=(float)(1.0/256*tb.Value);

30:ImageAttributes ia=new ImageAttributes();

31:ia.SetColorMatrix(m);

32:e.Graphics.DrawImage(i,this.ClientRectangle,0,

33: 0,i.Width,i.Height,GraphicsUnit.Pixel,ia);

34:}

35:}

37:void OnClickB(object sender, EventArgs e)

38:{

39:OpenFileDialog dlg=new OpenFileDialog();

40:dlg.Filter=”Bitmap files(*.bmp)|*.bmp”;

41:if(dlg.ShowDialog()==DialogResult.OK)

42:{

43:i=Image.FromFile(dlg.FileName);

44:Invalidate();

45:}

46:}

47:

48:void OnTrack(object sender, EventArgs e)

49:{

50:Invalidate();

51:}

52:

53:void OnSize(object sender, EventArgs e)

54:{

55:Invalidate();

GDI+: The .NET Graphics Interface

CHAPTER 3.5

LISTING 3.5.11 Continued

56: } 57:

58:public Form1()

59:{

60:this.Paint+=new PaintEventHandler(OnPaint);

61:this.SizeChanged+=new EventHandler(OnSize);

63: b=new Button(); 64:

65: b.Click+=new EventHandler(OnClickB); 66:

67:b.Location=new Point(5,5);

68:b.Size=new Size(60,22);

69:b.Text=”Image...”;

70:

71: this.Controls.Add(b); 72:

73:tr=new TrackBar();

74:tr.Location=new Point(100,5);

75:tr.Size=new Size(200,22);

76:tr.Maximum=255;

77:tr.Minimum=0;

78:tr.ValueChanged+=new EventHandler(OnTrack);

81:

this.Controls.Add(tr);

82:

 

83:tg=new TrackBar();

84:tg.Location=new Point(100,55);

85:tg.Size=new Size(200,22);

86:tg.Maximum=255;

87:tg.Minimum=0;

88:tg.ValueChanged+=new EventHandler(OnTrack);

91:

this.Controls.Add(tg);

92:

 

93:tb=new TrackBar();

94:tb.Location=new Point(100,105);

95:tb.Size=new Size(200,22);

96:tb.Maximum=255;

97:tb.Minimum=0;

98:tb.ValueChanged+=new EventHandler(OnTrack);

100:

373

3.5

T GDI+:RAPHICSGNTERFACEI . HE NET

Windows Forms

374

PART III

LISTING 3.5.11 Continued

101:this.Controls.Add(tb);

102:}

103:

104:static void Main()

105:{

106:Application.Run(new Form1());

107:}

108:}

109:}

Compile this file with the following command line:

csc /t:winexe colorspace1.cs

This file is substantially identical to that in Listing 3.5.10 with the exception that track bars for red, green, and blue are employed, instead of for alpha. The matrix is set up on lines 27–29 to adjust the intensity of each of the R, G, and B color channels individually.

Summary

There is so much in GDI+ to explore that this chapter could go on for another hundred pages or so. We think, however, that what is here will give you the confidence to experiment further and will allow you to understand most of the conventions required to transition from the old GDI that we know and love to the exiting possibilities of GDI+. Read on now to discover more about Windows Forms applications and components in the last chapter of this section, “Practical Windows Forms Applications.”

Practical Windows Forms

CHAPTER

Applications

3.6

 

IN THIS CHAPTER

• Using the Properties and Property

Attributes 376

Demonstration Application:

FormPaint.exe 383