Professional Documents
Culture Documents
NHM 33
11520097: Phm Nguyn Hi ___ 11520115: Dng Thanh Ha
NI DUNG
Tng Quan Bitmap Support Overview Loading Image System.Drawing.Image Image Information System.Drawing.Bitmap Bitmap Methods Binary Resources Animation Picture Box Sprite Matrix Tho lun
Tng Quan
c to thnh t nhng ng thng, ng cong. Vector c to thnh da trn biu thc ton hc. Vd: Fonts, CAD Program, Adobe Illustrator,
Raster
nh
c to thnh t nhng hng pixel hnh ch nht (rectangular arrays) bitmap Hu ht nh u dng bitmap
System.Drawing namespace c 2 lp: Image v Bitmap Bitmap class v Metafile class k tha t Image class (abstract) Bitmap file formats
Mt
bitmap c mt chiu cao v rng c th c o lng bng pixel. Mt bitmap cng c mt color depth c th, l s bit trn mi pixel (bpp)
Loading Image
C 3 cch:
Ly
Ly Ly
Image.FromFile
Image.FromStream
Image.FromHbitmap
System.Drawing.Image
Cy phn cp (hierarchy)
Object MarshalByRefObject Image
image;
H tr cc nh dng bitmap:
.gif .jpg .png .tiff .exif .wmf .emf Cho php v hnh nh dng Graphics.DrawImage, DrawIcon, DrawIconUnstretched, DrawImageUnscaled
.bmp
System.Drawing.Image
Lp Image cn cung vp phng thc RotateFlip dng quay v flip nh RotateFlipType xc nh kiu quay ca i tng
Quay 90 RotateFlipType.RotateNoneFlipX: Flip theo chiu X
RotateFlipType.Rotate90FlipNone:
private void Button1_Click(System.Object sender, System.EventArgs e) {if (bitmap1 != null) { bitmap1.RotateFlip(RotateFlipType.Rotate180FlipY); PictureBox1.Image = bitmap1; } }
Image Information
Kiu
Size int int float float SizeF
Thuc tnh
Size Width Height HorizontalResolution VerticalResolution PhysicalDimension
M t
System.Drawing.Bitmap
Dn xut t lp Image Cho php developer/user thao tc ha cp bit Bitmap Methods: Clone, GetHbitmap, SetResolution, MakeTransparent, FromHicon, GetHicon, LockBits, UnlockBits, Save, SaveAdd, GetPixel, SetPixel Icons, Animation, Picture Box
System.Drawing.Bitmap (tt)
Bitmap Bitmap Bitmap Bitmap Bitmap Bitmap Bitmap Bitmap Bitmap Bitmap Bitmap Bitmap IntPtr
(string strFilename) //To mt bitmap t mt file (string strFilename, bool bUseImageColorManagement) (Stream stream) (Stream stream, bool bUseImageColorManagement) (Type type, string strResource) (Image image) //To mt bitmap t mt i tng image (Image image, Size size) //To mt bitmap vi size (Image, int cx, int cy) (int cx, int cy) //To mt bitmap khng c d liu nh (int cx, int cy, PixelFormat pf) (int cx, int cy, Graphics grfx) (int cx, int cy, int cxRowBytes, PixelFormat pf, pBytes)
Bitmap Methods
Creates a copy of the section of this Bitmap defined by Rectangle structure and with a specified PixelFormat enumeration.
Creates a copy of the section of this Bitmap defined with a specified PixelFormat enumeration.
GetHbitmap Method: To mt i tng GDI bitmap t mt bitmap. Vd: GetHbitmap(), GetHbitmap(Color) SetResolution(float xDpi, float yDpi)
private void Form1_Paint(object sender, PaintEventArgs e) { Bitmap bmpBuilding = new Bitmap("building.bmp"); e.Graphics.DrawImage(bmpBuilding, 0, 0); Bitmap bmpMonument = new Bitmap("monument.bmp"); bmpMonument.MakeTransparent(); e.Graphics.DrawImage(bmpMonument, 200, 260); }
private void Form1_Paint(object sender, PaintEventArgs e){ Bitmap bmpFlying = new Bitmap("flying.bmp"); e.Graphics.DrawImage(bmpFlying, 0, 0); Bitmap bmpGlobe = new Bitmap("globe.bmp"); bmpGlobe.MakeTransparent(Color.Black); e.Graphics.DrawImage(bmpGlobe, 20, 120); }
[System.Runtime.InteropServices.DllImportAttribute("user32.dll", CharSet = CharSet.Unicode)] private static extern IntPtr LoadImage(int Hinstance, string name, int type, int width, int height, int load);
private void Hicon_Example(PaintEventArgs e) { // Get a handle to an icon. IntPtr Hicon = LoadImage(0, "smile.ico", 1, 0, 0, 16); // Create a Bitmap object from the icon handle. Bitmap iconBitmap = Bitmap.FromHicon(Hicon); // Draw the Bitmap object to the screen. e.Graphics.DrawImage(iconBitmap, 0, 0);}
private void GetHicon_Example(PaintEventArgs e) { // Create a Bitmap object from an image file. Bitmap myBitmap = new Bitmap(@"c:\FakePhoto.jpg"); // Draw myBitmap to the screen. e.Graphics.DrawImage(myBitmap, 0, 0); // Get an Hicon for myBitmap. IntPtr Hicon = myBitmap.GetHicon(); // Create a new icon from the handle. Icon newIcon = Icon.FromHandle(Hicon); // Set the form Icon attribute to the new icon. this.Icon = newIcon; // You can now destroy the icon, since the form creates // its own copy of the icon accessible through the Form.Icon property. DestroyIcon(newIcon.Handle); }
Saves this Image to the specified file or stream. (Inherited from Image) Saves this image to the specified stream in the specified format. (Inherited from Image) Saves this Image to the specified file in the specified format. (Inherited from Image) Saves this image to the specified stream, with the specified encoder and image encoder parameters.(Inherited from Image.) Saves this Image to the specified file, with the specified encoder and image-encoder parameters.(Inherited from Image.)
Adds a frame to the file or stream specified in a previous call to the Save method. Use this method to save selected frames from a multipleframe image to another multiple-frame image. (Inherited from Image.)
Adds a frame to the file or stream specified in a previous call to the Save method. (Inherited from Image.)
SaveAdd(Image, EncoderParameters)
public Color GetPixel( int x, int y ): Gets the color of the specified pixel in this Bitmap. public void SetPixel( int x, int y, Color color ): Sets the color of the specified pixel in this Bitmap.
// Create a Bitmap object from a file. Bitmap myBitmap = new Bitmap(GetType(), "download (5).jpg"); // Draw myBitmap to the screen. e.Graphics.DrawImage(myBitmap, 0, 0, myBitmap.Width, myBitmap.Height); // Get the color of a pixel within myBitmap. Color pixelColor = myBitmap.GetPixel(100, 300); color. // Set pixel in custom area in myBitmap to Get pixel 's
for (int Xcount = 100; Xcount < 400; Xcount++) { for (int Ycount = 100; Ycount < 400; Ycount++) { myBitmap.SetPixel(Xcount, Ycount, pixelColor);}}
Binary Resources
Nhng cc icons v con tr vo trong phn mm. Nhng file - c th l bitmaps, icons, con tr - c lu tr trong nhng file thc thi c coi l resources Icons, Animation, Image List, Picturebox Visual C#.NET cho php to binary Resources bng cch s dng mt trnh chnh sa hnh nh. thm mt binary resource vo chng trnh: Project. Add New Item. (Bitmap File/ Cursor File/ Icon File). Hoc khi chn bt k file bitmap, icon, cursor trong Solution Explorer (thuc project), t ca s properties, ti thuc tnh Build Action, thay i thnh Embedded Resource
Icon constructors
Icon (Type type, string strResource) Icon (Icon icon, Size size) Icon (Icon icon, int cx, int cy)
Graphics Methods
Void DrawIcon (Icon icon, int x, int y) Void DrawIcon (Icon icon, Rectangle rect) Void DrawIconUnstretched (Icon icon, Rectangle rect) Cursor (Type type, string strResource) class ProgramWithIcon: Form { public ProgramWithIcon() { Icon = new Icon(typeof(ProgramWithIcon), "ProgramWithIcon.ProgramWithIcon.ico");}}
Animation
S dng kiu d liu Timer v phng thc TimerOnTick Np mt b nh vo mt mng thc thi nhanh hn Vd: wink.cs
Chng trnh wink.cs np 4 nh trn qua resources v s dng s kin timer hin th winking eye.
class Wink: Form { protected Image[] aimage = new Image[4]; protected int iImage = 0, iIncr = 1;
Animation (p2)
public Wink() { Text = "Wink"; ResizeRedraw = true; BackColor = Color.White; for (int i = 0; i < 4; i++) aimage[i] = new Bitmap(GetType(), "Wink.Eye" + (i+1) + ".png"); Timer timer = new Timer(); timer.Interval = 100; timer.Tick += new EventHandler(TimerOnTick); timer.Enabled = true; }
Animation (p3)
protected virtual void TimerOnTick(object obj, EventArgs ea) { Graphics grfx = CreateGraphics(); grfx.DrawImage(aimage[iImage], (ClientSize.Width aimage[iImage].Width) / 2, (ClientSize.Height aimage[iImage].Height) / 2, aimage[iImage].Width, aimage[iImage].Height); grfx.Dispose(); iImage += iIncr; if (iImage == 3) iIncr = -1; else if (iImage == 0) iIncr = 1; } }
Animation (p4)
to animation: ta v li ton b form theo tc nht nh. Kim sot bng cc bin trng thi. Vd: bounce.cs class Bounce: Form { const int iTimerInterval = 25; // In milliseconds const int iBallSize = 16; // As fraction of client area const int iMoveSize = 4; // As fraction of ball size Bitmap bitmap; int xCenter, yCenter; int cxRadius, cyRadius, cxMove, cyMove, cxTotal, cyTotal; public Bounce() { Text = "Bounce"; ResizeRedraw = true; BackColor = Color.White; Timer timer = new Timer(); timer.Interval = iTimerInterval; timer.Tick += new EventHandler(TimerOnTick); timer.Start(); OnResize(EventArgs.Empty);}
Animation (p5)
protected override void OnResize(EventArgs ea) { Graphics grfx = CreateGraphics(); grfx.Clear(BackColor); float fRadius = Math.Min(ClientSize.Width / grfx.DpiX, ClientSize.Height / grfx.DpiY) / iBallSize; cxRadius = (int) (fRadius * grfx.DpiX); cyRadius = (int) (fRadius * grfx.DpiY); grfx.Dispose(); cxMove = Math.Max(1, cxRadius / iMoveSize); cyMove = Math.Max(1, cyRadius / iMoveSize); cxTotal = 2 * (cxRadius + cxMove); cyTotal = 2 * (cyRadius + cyMove);
bitmap = new Bitmap(cxTotal, cyTotal); grfx = Graphics.FromImage(bitmap); grfx.Clear(BackColor); DrawBall(grfx, new Rectangle(cxMove, cyMove, 2 * cxRadius, 2 * cyRadius)); grfx.Dispose(); xCenter = ClientSize.Width / 2; } yCenter = ClientSize.Height / 2;
Animation (p6)
protected virtual void DrawBall(Graphics grfx, Rectangle rect) { grfx.FillEllipse(Brushes.Red, rect); }
void TimerOnTick(object obj, EventArgs ea) { Graphics grfx = CreateGraphics(); grfx.DrawImage(bitmap, xCenter - cxTotal / 2, yCenter cyTotal / 2, cxTotal, cyTotal); grfx.Dispose(); xCenter += cxMove; yCenter += cyMove; if ((xCenter + cxRadius >= ClientSize.Width) || - cxRadius <= 0)) cxMove = -cxMove; (xCenter
if ((yCenter + cyRadius >= ClientSize.Height) || (yCenter - cyRadius <= 0)) cyMove = -cyMove; } }
Picture Box
PictureBoxSizeMode
Sprite
Mi bitmap mt frame => Np hnh nhiu ln, kh qun l. => Dng mt hnh ln cha nhiu hnh nh kch thc bng nhau (sprites) Hm DrawImage cho php v mt phn hnh ch nht ca image ln Graphic Cch extract frames t animated GIF files ..
Sprite (p2)
Image img; Bitmap bmp; FrameDimension dimension; int frameCount; private void openToolStripMenuItem_Click(object sender, EventArgs e){ if (openFileDialog1.ShowDialog() == DialogResult.OK) { img = Image.FromFile(openFileDialog1.FileName); dimension = new FrameDimension(img.FrameDimensionsList[0]); frameCount = img.GetFrameCount(dimension); pictureBox1.Image = img;}} Bitmap makeSpriteSheet(){ Bitmap bmpp = new Bitmap(img.Width * frameCount, img.Height); Graphics g = Graphics.FromImage(bmpp); int xPos = 0; for (int i = 0; i < frameCount; i++){ img.SelectActiveFrame(dimension,i); g.DrawImage(img,xPos,0, img.Width, img.Height); xPos += img.Width;} return bmpp; }
Sprite (p3)
Sprite (p4)
Vd s dng sprite
private Bitmap sprite; // Bitmap dng cho nh sprite private Bitmap backBuffer; // Back buffer private Timer timer; public Graphics graphics; private int index; // S th t ca frame private int curFrameColumn; // ct hin ti ca frame private int curFrameRow; // dng hin ti ca frame public Form1() { InitializeComponent(); graphics = this.CreateGraphics(); SetStyle(ControlStyles.AllPaintingInWmPaint, true); // To back buffer backBuffer = new Bitmap(this.ClientSize.Width, this.ClientSize.Height); sprite = new Bitmap(GetType(), "sprite2.png"); // Ly nh sprite index = 0; timer = new Timer(); // Khi to mt ng h timer.Enabled = true; timer.Interval = 30; timer.Tick += new EventHandler(timer_Tick); }
Sprite (p5)
private void Render() // V mt phn ca nh sprite { // Ly i tng graphics v ln back buffer Graphics g = Graphics.FromImage(backBuffer); g.Clear(Color.White); // Xc dnh s dng, ct ca mt frame trn nh sprite curFrameColumn = index % 6; curFrameRow = index / 6;
// V ln buffer g.DrawImage(sprite, 40, 40, new Rectangle(curFrameColumn *128, curFrameRow * 128, 128, 128), GraphicsUnit.Pixel); g.Dispose(); index++; // Tng th t frame ly frame tip theo if (index > 16 * 16) index = 0; else index++; } private void timer_Tick(object sender, EventArgs e) { Render(); graphics.DrawImageUnscaled(backBuffer, 0, 0); // V ln mn hnh }
Matrix
Nhc li: Ma trn l mt bng 2 chiu, mi l mt s thc. Bng cch s dng ma trn ta c th thc hin cc thao tc bin i h trc, bin i mu sc ca hnh nh. Li th quan trng nht ca vic s dng ma trn l tt c cc thao tc bin i mu sc c th thc hin bng php nhn ma trn chun, v tc thc thi. Nhc li: Php nhn tch v hng 2 ma trn: Amn x Bnp= Cmp Trong : Cij = tch v hng ca (hng i ca A) v (ct j ca B)
Matrix
2 4 6
A
3 5 7
1 3
B
2 4
11
=
C
Matrix
Ti sao dng nhn ma trn m khng tnh ton trc tip? Ma trn c kh nng ghp nhiu php bin i lm 1. lm 100 php bin i cng lc, ch cn tnh tch ca 100 ma trn bin i, sau cng nhn ma trn ca im v ma trn tch Nh vy vn phi nhn nhiu ln???? ng qun: mt hnh c nhiu im
Matrix
V d 1: Xoay, lt hnh
Gi s A = (ai,j) l ma trn i din ca hnh A, th hnh B s c ma trn i din l B = (bi,j) = (aj,i) = AT (Ma trn chuyn v ca A), hnh H s c ma trn i din l H = (hi,j) = (aj,k-i+1) (k l di cnh ca hnh)
Matrix
V d 2: Bin nh mu thnh nh en trng.
Ly gi tr trung bnh 3 knh mu (R G B) ca tm hnh, sau ta em gi tr thu c gn cho 3 knh mu, ta s c hnh trng en.
Matrix
V d 3: Median (trung v) b lc dng gim nhiu
Microsoft Windows with C# Book and Code by Charles Petzold Slide bi ging nm 2012, thy Phm Thi Vng http://www.yevol.com, msdn.microsoft.com, stackoverflow.com, codeproject.com