1 / 22

Images and Bitmaps (in C#)

Images and Bitmaps (in C#). By Matthew Miling Advanced Windows Programming. Contents. Overview – Images and Bitmaps Fundamental Windows Classes System.Drawing.Image System.Drawing.Bitmap Other Image/Bitmap Types. Overview – Images and Bitmaps. In computers, two types of graphics

aruff
Download Presentation

Images and Bitmaps (in C#)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Images and Bitmaps (in C#) By Matthew Miling Advanced Windows Programming

  2. Contents • Overview – Images and Bitmaps • Fundamental Windows Classes • System.Drawing.Image • System.Drawing.Bitmap • Other Image/Bitmap Types

  3. Overview – Images and Bitmaps • In computers, two types of graphics • Vector - graphics made of lines and curves - fonts, CAD programs, Photoshop • Raster - rectangular arrays called bitmaps - almost all internet images are bitmaps

  4. Fundamental Windows Classes • System.Windows.FormsLocation of the Form class used for Windows • System.DrawingContains our Image and Bitmap classes Coffee Bean.bmp

  5. System.Drawing.Image • HierarchyObject MarshalByRefObject Image • Used to declare an image object: Image image; • Used to load various bitmap formats .bmp .gif .jpg .png .tiff .exif .wmf .emf

  6. Opening a file for an Image object • Three different methods:Obtain an image from a bitmap file on disk: Image.FromFile(string filename)Obtain an image from a valid bitmap stream Image.FromStream(System.IO.Stream stream)Obtain an image using a Win32 GDI bitmap Image.FromHbitmap(System.IntPtr hbitmap)

  7. Drawing the image • Use the OnPaint handler to initiate the draw • Use the DrawImage() method from the System.Drawing.Graphics class to draw the actual image • Sample Codeprotected override void OnPaint(PaintEventArgs e){ e.Graphics.DrawImage(image);}

  8. Graphics.DrawImage() • Currently, 30 different instances exist for the DrawImage() method • Appropriate method is application specific • Most common overloaded methodsvoid DrawImage(Image image)void DrawImage(Image image, int x, int y)void DrawImage(Image image, int x, int y, int cx, int cy)void DrawImage(Image image, Rectangle rect) • Example 1

  9. Metrics vs. Pixels • Images by default draw with metrics for image device independence • a 3 inch image is 3 inches wide on monitor and printer • relies heavily on embedded resolution (image dpi) • Pixels are a straightforward mapping of bits onto the screen • One pixel (dot) on screen = one location on bitmap • Image with high dpi will be smaller than image with low dpi, but will have better visual quality

  10. Rectangular fitting • Shows the different uses of rectangular fitting • Use DrawImage() with different arguments for different effects • Examples 2, 3

  11. Rotating/Shearing/Scaling • Provide a means to transform the image in certain ways using DrawImage() • Accomplish this with a set of points • Example 4

  12. Partial image display • Ability to draw only a portion of the image we would like to see • Example 5

  13. Drawing on the image • Ability to overlap text onto the image • Use the DrawString() method • Cannot use indexed bitmaps! (i.e. gifs) • Example 6

  14. Drawing Extras • Many other classes exist to interact with attributes in the image class. • PixelFormat – page 492 • ColorPalette – page 494 • ImageFormat – page 494

  15. System.Drawing.Bitmap • Derives from Image • For applications with images that do more than just draw • Allow developer/user to manipulate graphics at the bit level • Icons, Animation, Image List, Picturebox

  16. System.Drawing.Bitmap • Image has no constructors • Bitmap has 12 constructors: • Bitmap(string strFilename) • Bitmap(Stream stream) • Bitmap(Image image) • Bitmap(Image image, Size size) • Bitmap(int cx, int cy) • Page 519

  17. Bitmap Example • Begin with two bitmaps for text length • We see text placed on a bitmap • Example 7 • Try different dpi!

  18. Other Image/Bitmap Types • Icons, Cursors and Bitmaps • Image arrays (Animation) • ImageList • PictureBox

  19. Binary Resources • Embed icons and cursors within the software • Use the Icon data type, which is global to the Form • Example 8

  20. Animation • Uses a Timer data type to load a new image each tick • Loads a set of bitmaps into an array for quick run-through • See page 530 for ‘Wink’ example

  21. Image List • List of Image objects with same size and color format • Useful for Toolbars, some animation • See page 536 for example code

  22. Picture Box • Derives from Control class • Displays a box with an image, so entire box is a Control • Useful for Project 1 • Example 9

More Related