Professional Documents
Culture Documents
5 Gdi
5 Gdi
Tng quan
Th vin gip v ln mn hnh hoc my in
m khng cn quan tm n cu trc phn
cng c lp thit b
GDI+ bao gm 3 nhm dch v chnh:
2D vector graphics: cho php to hnh t cc hnh
c bn (primitive): ng thng, trn, eclipse,
ng cong,
Imaging: lm vic vi cc tp tin hnh nh
(bitmap, metafile)
Typography: v ch
GDI+ namespace
System.Drawing
System.Drawing. Drawing2D
System.Drawing.Imaging
System.Drawing.Printing
System.Drawing.Text
Cc khi nim
B mt v: Graphics (System.Drawing)
- Ly t Paint event (form)
- CreateGraphics (trong control)
Invalidate();
Invalidate(myRect);
Mt s cu trc
Color
Point, PointF
Rectangle, RectangleF
Size, SizeF
Point, PointF X,Y
+, -, ==, !=, IsEmpty
Rectangle, X,Y
RectangleF Top, Left, Botton, Right
Height, Width
Inflate(), IntersSec,() Union()
Contain()
Size, SizeF +, -, ==, !=
Height, Width
Region phn rut ca khun hnh hc
Rectangle rect=new Rectangle(0,0,100,100)
Region rgn= new Region(rect)
Mt s enumeration
ContentAlignment
FontStyle
GraphicsUnit
KnowColor
RotateFlipType
StringAlignment
..
2D vector graphics
Pen & brush
Pen, Pens, SystemPens
Brush, Brushes, SystemBrushes,
SolidBrushes, TextureBrushes,
(System.Drawing.Drawing2D)
HatchBrush, LinearGradientBrush,
PathGradientBrush
DrawPie
DrawArc
FillPie
2D vector graphics
Path: kt hp nhiu loi ng nt thnh mt i tng duy
nht. Cc nt khng nht thit phi lin nhau.
System.Drawing.Drawing2D
Region.Intersect, Union, Xor,
Exclude, Complement
2D vector graphics
Clipping: gii hn cc hnh v vo trong mt region,
path hoc rectangle
Graphics.SetClip(<region>)
Graphics.SetClip(<path>)
Graphics.SetClip(<rectangle>)
V d
V d
Image
Cho php v cc hnh nh.
To cc hnh nh thng qua class Image (Bitmap,
Metafile, Icon, )
Class Bitmap h tr cc nh dng chun GIF, JPG,
BMP, PNG, TIFF.
Dng Graphics.DrawImage, DrawIcon,
DrawIconUnstretched, DrawImageUnscaled
Bitmap
Bitmap bmp = new Bitmap(filename, )
RotateFlip: xoay lt, hnh
MakeTransparent: t mu trong sut.
GetPixel, SetPixel: v bng cch chm tng im!
V ch
Cho php v cc cu ch trn Graphics
To cc i tng Font ch nh cc thuc tnh ch
(nh font, style, ) (chng 5)
To pen v brush
Graphics.DrawString
o kch thc chui (di,rng) , dng
Graphics.MeasureString
System.Drawing namespace
Bitmap Pixel image (GIF, JPEG, PNG, BMP, TIFF)
Brush Abstract base class.
Brushes Brushes for all basic colors
Color
Font Defines a format for text, including font face, and
sizeEncapsulates a typeface, size, style, and effects.
FontFamily Group of type faces with the same basic design.
Graphics
Icon Transparent bitmaps used for Windows icons.
Image Abstract base class common to the Bitmap, Icon, and
Metafile classes.
Pen Defines an object used to draw lines and curves.
Pens Provides static Pen definitions for all the standard colors.
System.Drawing namespace
Point Structure used to represent an ordered pair of integers.
Typically used to specify two-dimensional Cartesian
coordinates.
PointF Same as Point, but uses a floating-point number (float in C#,
Single in VB.NET) rather than an integer.
Rectangle Structure that represents the location and size of a rectangular
region.
RectangleF Same as Rectangle, but uses floating-point values (float in C#,
single in VB.NET) rather than integers.
Size Structure that represents the size of a rectangular region as
an order pair (Point) representing width and height.
SizeF Same as Size, but uses PointF rather than Point.
SystemBrushes A utility class with 21 static, read-only properties that return
objects of type Brush (each of a different
color).
SystemPens A utility class with 15 static, read-only properties that return
objects of type Pen (each of a different color).
tng to animation vi GDI+
Xa c - v mi l sai lm!
Frame-based animation: v li ton b form theo tc
nht nh. Kim sot bng cc bin trng thi.
Form.DoubleBuffered = true
Mi thao tc v din ra ti back-buffer.
Khi hon tt, ni dung ca back-buffer c
hon chuyn (flip) ln front-buffer (form).
* Thao tc flip c thc hin bng phn cng
nn thng rt nhanh.
Sprites
Mi bitmap mt frame => np hnh nhiu ln, kh qun
l.
Dng mt hnh ln cha nhiu hnh nh kch thc bng
nhau (sprites)
y
Transformation bin i h trc
H trc th gii (o c s ca cc lnh Draw, Fill)
y
Transformation bin i h trc
World coordinate h trc o c s ca cc lnh Draw, Fill
100 x
(0,0)
50
x
(25,50)
(75,150)
g.TranslateTransform(100,50)
(t h trc world ti v tr (100,50)
ca h trc world hin ti = h trc
device)
y y
Transformation bin i h trc
World coordinate h trc o c s ca cc lnh Draw, Fill
100 x
(0,0)
50
x
g.TranslateTransform(100,50)
g.DrawRectangle(pen,0,0,50,50)
y y
Transformation bin i h trc
World coordinate h trc o c s ca cc lnh Draw, Fill
100 x
(-25,-25)
(0,0)
50
x
g.TranslateTransform(100,50)
g.DrawRectangle(pen,-25,-25,50,50)
y y
Transformation bin i h trc
World coordinate h trc o c s ca cc lnh Draw, Fill
450
y
g.TranslateTransform(100,50)
g.RotateTransform(45)
g.DrawRectangle(pen,-25,-25,50,50)
y x
Th t php bin i
Th t php bin i l quan trng, p dng th t bin i
khc nhau s to ra hiu ng khc nhau.
g.RotateTransform(45)
g.TranslateTransform(100,50)
g.DrawRectangle(pen,-25,-25,50,50)
y x
y
Th t php bin i
Th t php bin i l quan trng, p dng th t bin i
khc nhau s to ra hiu ng khc nhau.
g.RotateTransform(45)
g.TranslateTransform(100,50)
g.DrawRectangle(pen,-25,-25,50,50)
50
100
y x
y
Bin i h trc bng ma trn
2 3 1 2 3 5
4 5 + 3 4 = 7 9
6 7 5 6 11 13
i cng v ma trn
2 3 11
1 2
4 5 =
3 4
6 7
2 3 11 16
1 2
4 5 =
3 4
6 7
2 3 11 16
1 2
4 5 = 19
3 4
6 7
Mt im l ma trn 12 (P)
Php bin i l ma trn: 22 (T)
Bin i: P = P T
1 0
5 10 = 5 -10
0 -1
Mt im l ma trn 12 (P)
Php bin i l ma trn: 22 (T)
Bin i: P = P T
0 1
5 10 = -10 5
-1 0
Xoay 900
Bin i ta im bng ma trn
Mt im l ma trn 12 (P)
Php bin i l ma trn: 22 (T)
Bin i: P = P T
3 0
3 2 = 9 2
0 1
Dn trc x 3 ln
Bin i ta im bng ma trn
0 0 0
x y 1 = x+10 y+20 1
0 0 0
10 20 1
Bin i ta im bng ma trn
0 1 0
x y 1 = -y x 1
-1 0 0
0 0 1
1 0 0
x y 1 = x -y 1
0 -1 0
0 0 1
Bin i ta im bng ma trn