DekGenius.com
[ Team LiB ] Previous Section Next Section

Bitmapserializable, marshal by reference, disposable

System.Drawing (system.drawing.dll)sealed class

This subclass of Image encapsulates a picture represented by a 2D array of pixel data. It supports a range of PixelFormats, including both true-color and palette-color bitmaps, and those with an alpha channel for transparency.

A Bitmap conceptually represents a system bitmap and is ultimately implemented through a native GDI+ bitmap handle. As with most drawing classes that wrap limited resources (Pen and Brush being other examples), you should have a well-defined lifetime management plan, calling Dispose() when you are finished to release the resources back to the system (the using idiom in C# is useful here: see the main body of the text for details). If you rely on the garbage collector to do this, the Bitmap object will be collected but its pixel data will be leaked until the garbage collector happens to run, which may be too late.

There are several ways to construct a Bitmap object. One of the most is common is to provide the filename of the picture that you want to load. The framework will use the installed CODECs (BMP, PNG, JPG, and GIF) to attempt to load the image. Note that the file itself will remain locked until the Bitmap that loaded it is disposed. However, you can Clone() the bitmap and Dispose() the original to release the file.

Alternatively, you can construct bitmaps of a specific size and PixelFormat, or bitmaps compatible with a particular Graphics surface. You can also construct from another Image (optionally rescaling the original in the process), or from a block of pixel data referenced via an IntPtr. The latter is most useful in interop scenarios, as are the static methods FromHicon() and FromResource(), which return a Bitmap wrapper for Win32 icons and bitmap resources. (By preference, you would use the Icon class instead, for a fully managed solution.)

You can also Clone() a specific Rectangle from a Bitmap, optionally changing its PixelFormat in the process, or use the override of GetThumbnailImage() to retrieve a smaller version of the original. This method does expose its unmanaged origins somewhat, inconveniently requiring you to pass an Abort delegate that is required to do nothing, and a null pointer (IntPtr.Zero). It is recommended that you use the rescaling constructor instead.

You can manipulate the image using the GetPixel() and SetPixel() methods, but this is not especially efficient! A better approach for image processing is to use the LockBits() method to retrieve a BitmapData object, which allows direct access to the underlying pixel data. LockBits() actually ensures that the pixel data is fixed into system memory, and will not move while you are using it. For heavy duty processing, you would typically hand this off through interop to an unmanaged routine, but managed code will often perform adequately if care is taken with the selection of the processing algorithm, and you are prepared to support unsafe code in your assembly. When you have finished, you can use UnlockBits() to unlock the pixel data again.

There are a couple of built-in manipulation functions, including RotateFlip() to let you perform fixed rotations and flipping, and MakeTransparent(), which lets you make a selected color transparent when drawing the image. Classes providing more advanced manipulation of the bitmap can be found in the System.Drawing.Imaging namespace.

To draw an image on a Graphics surface, use Graphics.DrawImage() and Graphics.DrawImageUnscaled(). The Graphics.CompositingMode, Graphics.CompositingQuality, and Graphics.InterpolationMode determine how the pixel data will be drawn onto the surface.

If you want to paint onto the Bitmap itself, you can use Graphics.FromImage() to create a Graphics surface bound to the bitmap object. Any painting on that surface will be reflected on the Bitmap.

public sealed class Bitmap : Image {
// Public Constructors
   public Bitmap(Image original);
   public Bitmap(Image original, int width, int height);
   public Bitmap(Image original, Size newSize);
   public Bitmap(int width, int height);
   public Bitmap(int width, int height, Graphics g);
   public Bitmap(int width, int height, int stride, System.Drawing.Imaging.PixelFormat format, IntPtr scan0);
   public Bitmap(int width, int height, System.Drawing.Imaging.PixelFormat format);
   public Bitmap(System.IO.Stream stream);
   public Bitmap(System.IO.Stream stream, bool useIcm);
   public Bitmap(string filename);
   public Bitmap(string filename, bool useIcm);
   public Bitmap(Type type, string resource);
// Public Static Methods
   public static Bitmap FromHicon(IntPtr hicon);
   public static Bitmap FromResource(IntPtr hinstance, string bitmapName);
// Public Instance Methods
   public Bitmap Clone(RectangleF rect, System.Drawing.Imaging.PixelFormat format);
   public Bitmap Clone(Rectangle rect, System.Drawing.Imaging.PixelFormat format);
   public IntPtr GetHbitmap();
   public IntPtr GetHbitmap(Color background);
   public IntPtr GetHicon();
   public Color GetPixel(int x, int y);
   public BitmapData LockBits(Rectangle rect, System.Drawing.Imaging.ImageLockMode flags, 
        System.Drawing.Imaging.PixelFormat format);
   public void MakeTransparent();
   public void MakeTransparent(Color transparentColor);
   public void SetPixel(int x, int y, Color color);
   public void SetResolution(float xDpi, float yDpi);
   public void UnlockBits(System.Drawing.Imaging.BitmapData bitmapdata);
}

Hierarchy

System.Object System.MarshalByRefObject Image(System.Runtime.Serialization.ISerializabl, System.ICloneable, System.IDisposable) Bitmap

Returned By

System.Drawing.Design.ToolboxItem.Bitmap, Icon.ToBitmap(), Image.FromHbitmap(), System.Windows.Forms.Design.PropertyTab.Bitmap

Passed To

System.Drawing.Design.ToolboxItem.Bitmap, System.Windows.Forms.ControlPaint.{CreateHBitmap16Bit(), CreateHBitmapColorMask(), CreateHBitmapTransparencyMask()}

    [ Team LiB ] Previous Section Next Section