220 likes | 234 Views
Learn how to manipulate images and bitmaps in C# using advanced Windows programming techniques. Understand fundamental classes, drawing methods, file handling, metrics, scaling, and more.
E N D
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 • Vector - graphics made of lines and curves - fonts, CAD programs, Photoshop • Raster - rectangular arrays called bitmaps - almost all internet images are bitmaps
Fundamental Windows Classes • System.Windows.FormsLocation of the Form class used for Windows • System.DrawingContains our Image and Bitmap classes Coffee Bean.bmp
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
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)
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);}
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
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
Rectangular fitting • Shows the different uses of rectangular fitting • Use DrawImage() with different arguments for different effects • Examples 2, 3
Rotating/Shearing/Scaling • Provide a means to transform the image in certain ways using DrawImage() • Accomplish this with a set of points • Example 4
Partial image display • Ability to draw only a portion of the image we would like to see • Example 5
Drawing on the image • Ability to overlap text onto the image • Use the DrawString() method • Cannot use indexed bitmaps! (i.e. gifs) • Example 6
Drawing Extras • Many other classes exist to interact with attributes in the image class. • PixelFormat – page 492 • ColorPalette – page 494 • ImageFormat – page 494
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
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
Bitmap Example • Begin with two bitmaps for text length • We see text placed on a bitmap • Example 7 • Try different dpi!
Other Image/Bitmap Types • Icons, Cursors and Bitmaps • Image arrays (Animation) • ImageList • PictureBox
Binary Resources • Embed icons and cursors within the software • Use the Icon data type, which is global to the Form • Example 8
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
Image List • List of Image objects with same size and color format • Useful for Toolbars, some animation • See page 536 for example code
Picture Box • Derives from Control class • Displays a box with an image, so entire box is a Control • Useful for Project 1 • Example 9