Presentation is loading. Please wait.

Presentation is loading. Please wait.

Images and Bitmaps (in C#)

Similar presentations


Presentation on theme: "Images and Bitmaps (in C#)"— Presentation transcript:

1 Images and Bitmaps (in C#)
Li-Chih Hsu With a few modifications 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 Bitmap Support Overview
The System.Drawing namespace has two classes, named Image and Bitmap. The Bitmap class and Metafile class are derived from Image class. Bitmap file formats A bitmap has a particular height and width measured in pixels. A bitmap also has a particular color depth,which is the number of bits per pixel(bpp).

5 System.Drawing.Image Hierarchy Object 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 Loading and Drawing 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 a bitmap object using a Win32 GDI bitmap Image.FromHbitmap(System.IntPtr hbitmap)

7 Image Information Image properties
Type Property Accessiblity Description Size Size get int Width get int Height get float HorizontalResolution get In dots per inch float VerticalResolution get In dots per inch

8 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 Code protected override void OnPaint(PaintEventArgs e) { e.Graphics.DrawImage(image); }

9 Graphics.DrawImage() Currently, 30 different instances exist for the DrawImage() method Appropriate method is application specific void 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,2

10 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

11 Rectangular fitting Shows the different uses of rectangular fitting
Use DrawImage() with different arguments for different effects Examples 3, 4

12 Image Reflection If we specify a negative width, the image is flipped around the vertical axis – it’s mirror image. Sample Code: grfx.DrawImage(image cx/2,cy/2, cxImage,cyImage); grfx.DrawImage(image cx/2,cy/2, -cxImage,cyImage); grfx.DrawImage(image cx/2,cy/2, cxImage, -cyImage); grfx.DrawImage(image cx/2,cy/2, -cxImage,-cyImage); example5

13 Rotate and Shear These two methods effectively translate,scale,shear,rotate an image into a parallelogram. Void DrawImage(Image image, Point[ ] apt) Void DrawImage(Image image, PointF[ ] aptf) The array argument must contain exactly three points Apt[0] = destination of upper left corner of image. Apt[1] = destination of upper right corner of image. Apt[2] = destination of lower left corner of image. See example 6

14 Partial image display Ability to draw only a portion of the image we would like to see Let you specify a rectangular subsection of the bitmap to display. Example code: new Rectangle(0, 0 ,image.Width, image.Height) See example 7

15 Drawing on the image Ability to overlap text onto the image
To draw on an image , we need to obtain a Graphics object that refer to the image. Use the DrawString() method Cannot use indexed bitmaps! (i.e. gifs) Example 8

16 Thumbnail The GeThumbnailImage method is intend to be used to create a
thumbnail of an image, which is a smaller version of the image that an application can use to convey the contents of the image while saving time and space. Image GEtThumbnailImage(int cx,int cy,Image GetThumbnailImageAbort gtia,InPtr pData); See example 9

17 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

18 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)

19 Bitmap Example Begin with two bitmaps for text length
We see text placed on a bitmap Example 10 Try different dpi!

20 Binary Resources Embed icons and cursors within the software
Whenever you create a bitmap, a cursor,or an icon file you want to use as resource. In visual C#.NET,when you select any bitmap,icon, or cursor file in solution Explorer that is part of a project, you’ll see a properties window for the file. Change the Build Action property to Embedded Resource. Example 11

21 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

22 Image List An image list is essentially just a flexible array of Image objects with the same size and color format. To create an object: ImageList imglst = new ImageList(); To add Image object to the image list: Imglist.Images.Add(image); Number of images in an imageList imglst.Images.Count;

23 Image List (cont.) To return to image object imglst.Images[2];
ImageList.imageCollection Methods bool contains(image image) Int IndexOf(image image) void RemoveAt(int index) void Clear()

24 Image List (cont.) Draw Methods
void Draw(Graphics grfx, Point pt,int index) void Draw(Graphics grfx, int x, int y,int index) void Draw(Graphics grfx, int x,int y, int cx,int cy,int index)

25 Picture Box Another image-related control Descended from Control
Properties Type property Accessibility image image get/set BorderStyle borderStyle get/set PictureBoxSizeMode SizeMode get/set

26 Question?


Download ppt "Images and Bitmaps (in C#)"

Similar presentations


Ads by Google