Tuesday, March 8, 2011

Create Image from URI

Image is popular class that inherits from FrameworkElement. Here's the class hierarchy:

Object
--DispatcherObject (abstract)
----DependencyObject
------Visual (abstract)
--------UIElement
------------FrameworkElement
------------------Image

The Image class lets you easily include an image in a document or an application.

Uri uri = new Uri("http://www.images.com/bird.jpg");
BitmapImage bitmap = new BitmapImage(uri);

Image img = new Image();
img.Source = bitmap;


A couple of steps are required to display an image. The program first creates an object of Uri that indicates the location of the bitmap. This is passed to the BitmapImage constructor, which actually loads the image into memory. (Many popular formats are supported, including GIF, TIFF, JPEG, and PNG.) The Image class displays the image in the window.


You can find the Image class in the System.Windows.Controls namespace. Strictly speaking, Image is not considered a control and does not derive from the Control class. But the System.Windows.Controls namespace is so important that I'll include it in most programs from here on. The BitmapImage class is located in the System.Windows.Media.Imaging namespace, which is certainly an important namespace if you're working with bitmaps. However, I won't generally include a using directive for this namespace unless the program requires it.

Alternatively, you can load an image file from a local disk drive. You'll need a fully qualified file name as the argument in the Uri constructor, or you'll need to preface the relative file name with "file://".

Uri uri = new Uri(
System.IO.Path.Combine(
Environment.GetEnvironmentVariable("windir"),"BirdsOnComp.bmp"));




The Environment.GetEnvironmentVariable method retrieves the "windir" environment variable, which is a string like "C:\WINDOWS". The Path.Combine method combines that path name with the file name of the desired bitmap so that I (the lazy programmer) don't have to worry about inserting slashes correctly. You can either preface the Path class with System.IO (as I've done) or include a using directive for that namespace.

Rather than passing the Uri object to the BitmapImage constructor, you can set the Uri object to the UriSource property of BitmapImage. However, it's recommended that you surround the setting of this property with calls to the BeginInit and EndInit methods in BitmapImage:

bitmap.BeginInit();
bitmap.UriSource = uri;
bitmap.EndInit();


As you know, bitmaps have a pixel width and a pixel height, which is to the number of picture elements actually encoded in the file. The BitmapImage class inherits integer read-only PixelWidth and PixelHeight properties from BitmapSource that reveals this information. Bitmaps often (but not always) have embedded resolution information. Sometimes this resolution information is important (a scan of a rare postage stamp) and sometimes it's not (a photo of your postal carrier). The resolution information in dots per inch is available from the read-only double values DpiX and DpiY. The BitmapImage class also includes read-only double properties named Width and Height. These values are calculated with the following formulas:



Without the value of 96 in the numerator, these formulas would give a width and height of the image in inches. The 96 converts inches into device-independent units. The Height and Width properties constitute the metrical size of the bitmap in device-independent units. BitmapImage also inherits from BitmapSource a Format property that provides the color format of the bitmap; for those bitmaps that have a color table, the Palette property gives access to that.

The Image class does not have its own Background and Foreground properties because those properties are defined by the Control class and Image is not derived from Control. The distinction may be a little confusing at first. In earlier Windows application programming interfaces, virtually everything on the screen was considered a control. That is obviously not the case here. A control is really just another visual object, and is characterized mostly by giving feedback to user input. Elements like Image can obtain user input, of course, because all the keyboard, mouse, and stylus input events are defined by UIElement.

No comments:

Post a Comment