- •Contents
- •What Is C#?
- •C# Versus Other Programming Languages
- •Preparing to Program
- •The Program Development Cycle
- •Your First C# Program
- •Types of C# Programs
- •Summary
- •Workshop
- •C# Applications
- •Basic Parts of a C# Application
- •Structure of a C# Application
- •Analysis of Listing 2.1
- •Object-Oriented Programming (OOP)
- •Displaying Basic Information
- •Summary
- •Workshop
- •Variables
- •Using Variables
- •Understanding Your Computer’s Memory
- •C# Data Types
- •Numeric Variable Types
- •Literals Versus Variables
- •Constants
- •Reference Types
- •Summary
- •Workshop
- •Types of Operators
- •Punctuators
- •The Basic Assignment Operator
- •Mathematical/Arithmetic Operators
- •Relational Operators
- •Logical Bitwise Operators
- •Type Operators
- •The sizeof Operator
- •The Conditional Operator
- •Understanding Operator Precedence
- •Converting Data Types
- •Understanding Operator Promotion
- •For Those Brave Enough
- •Summary
- •Workshop
- •Controlling Program Flow
- •Using Selection Statements
- •Using Iteration Statements
- •Using goto
- •Nesting Flow
- •Summary
- •Workshop
- •Introduction
- •Abstraction and Encapsulation
- •An Interactive Hello World! Program
- •Basic Elements of Hello.cs
- •A Few Fundamental Observations
- •Summary
- •Review Questions
- •Programming Exercises
- •Introduction
- •Essential Elements of SimpleCalculator.cs
- •A Closer Look at SimpleCalculator.cs
- •Simplifying Your Code with Methods
- •Summary
- •Review Questions
- •Programming Exercises
- •Introduction
- •Lexical Structure
- •Some Thoughts on Elevator Simulations
- •Concepts, Goals and Solutions in an Elevator Simulation Program: Collecting Valuable Statistics for Evaluating an Elevator System
- •A Deeper Analysis of SimpleElevatorSimulation.cs
- •Class Relationships and UML
- •Summary
- •Review Questions
- •Programming Exercises
- •The Hello Windows Forms Application
- •Creating and Using an Event Handler
- •Defining the Border Style of the Form
- •Adding a Menu
- •Adding a Menu Shortcut
- •Handling Events from Menus
- •Dialogs
- •Creating Dialogs
- •Using Controls
- •Data Binding Strategies
- •Data Binding Sources
- •Simple Binding
- •Simple Binding to a DataSet
- •Complex Binding of Controls to Data
- •Binding Controls to Databases Using ADO.NET
- •Creating a Database Viewer with Visual Studio and ADO.NET
- •Resources in .NET
- •Localization Nuts and Bolts
- •.NET Resource Management Classes
- •Creating Text Resources
- •Using Visual Studio.NET for Internationalization
- •Image Resources
- •Using Image Lists
- •Programmatic Access to Resources
- •Reading and Writing RESX XML Files
- •The Basic Principles of GDI+
- •The Graphics Object
- •Graphics Coordinates
- •Drawing Lines and Simple Shapes
- •Using Gradient Pens and Brushes
- •Textured Pens and Brushes
- •Tidying up Your Lines with Endcaps
- •Curves and Paths
- •The GraphicsPath Object
- •Clipping with Paths and Regions
- •Transformations
- •Alpha Blending
- •Alpha Blending of Images
- •Other Color Space Manipulations
- •Using the Properties and Property Attributes
- •Demonstration Application: FormPaint.exe
- •Why Use Web Services?
- •Implementing Your First Web Service
- •Testing the Web Service
- •Implementing the Web Service Client
- •Understanding How Web Services Work
- •Summary
- •Workshop
- •How Do Web References Work?
- •What Is UDDI?
- •Summary
- •Workshop
- •Passing Parameters and Web Services
- •Accessing Data with Web Services
- •Summary
- •Workshop
- •Managing State in Web Services
- •Dealing with Slow Services
- •Workshop
- •Creating New Threads
- •Synchronization
- •Summary
- •The String Class
- •The StringBuilder Class
- •String Formatting
- •Regular Expressions
- •Summary
- •Discovering Program Information
- •Dynamically Activating Code
- •Reflection.Emit
- •Summary
- •Simple Debugging
- •Conditional Debugging
- •Runtime Tracing
- •Making Assertions
- •Summary
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