Title: Introduction to GDI
1Lecture 14 Introduction to GDI
2Drawing Demo
picturebox1
txtSize
button1
button2
txtX
txtY
3clicking mouse in picturebox1 draws a red circle
at that location and update the X and Y
textboxes. clicking the Draw Box button draws a
blue box of the specified Size at the X and Y
location.
4positions box relative to upper left corner of
picturebox1
draws a blue box of specified size
5gets mousepoint (position x and y)
displays mouse position
draws red circle of size 20
6clears picturebox1
7allows user to type in a different size
8Graphics - Drawing Demo 2005
image goofy.jpg located inside Folder, Debug
picBox
txtFileName
btnInvert
btnGrayscale
btnLoad
9Method to Read Image File
10class Form1
declare img as a Bitmap
button event passes image by reference
. . .
11Method Invert( )
inverts level of each color value of each pixel
12Method Grayscale( )
sets all three color values to the average level
13OpenFileDialog
Instead of requiring the user to type in a
filename, we can use the OpenFileDialog class to
bring up a file directory. This directory will
show the folder and files at the current level.
We can limit the files to those with a particular
set of extensions as shown. When the appropriate
file is selected, the picBox.Image can be
assigned a new Bitmap using the selected image
file.
14Avast, ye mateys! There be rough seas
ahead! Lubbers should steer to the shore.
Proceed at yer own risk!
The following section is optional, for those who
are interested in learning how to make
pixel-level image operations faster (i.e.
practical).
such as C, C, C with pointers !!!!
15Image Pixel Format
24 bit per pixel is 1 byte per color
scan line
red byte
blue byte
green byte
scan line must be a multiple of 4 bytes, so each
line is padded with between 0 and 3 bytes
16Locking and Unlocking the Bitmap
pointer to the beginning of the first scan line
of the image
17Pixel Operations
18Fast Grayscale Method
19Fast Invert Method
20Form1 Button Events to Access ImgPro Methods
21Disposing of Objects
It is important to call the Dispose( ) method if
you get a Graphics object by calling
CreateGraphics( ).
Alternatively, you can use the using construct,
which automatically calls Dispose( ) when an
object goes out of scope. using (Graphics g
this.CreateGraphics()) g.DrawLine(Pens.Black
, new Point(0, 0), new Point(3, 5))
which is equivalent to... Graphics g
this.CreateGraphics() try
g.DrawLine(Pens.Black, new Point(0, 0), new
Point(3, 5) finally if(g ! null)
((IDisposable)g).Dispose()
22(No Transcript)
23(No Transcript)
24(No Transcript)
25(No Transcript)
26(No Transcript)
27(No Transcript)
28(No Transcript)
29(No Transcript)
30(No Transcript)