Bitmap Class
Encapsulates a GDI+ bitmap, which consists of the pixel data for a graphics image and its attributes. A Bitmap is an object used to work with images defined by pixel data.

Namespace: System.Drawing
Assembly: System.Drawing (in system.drawing.dll)

Syntax

Visual Basic (Declaration)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class Bitmap
    Inherits Image
Visual Basic (Usage)
Dim instance As Bitmap
C#
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public sealed class Bitmap : Image
C++
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class Bitmap sealed : public Image
J#
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class Bitmap extends Image
JScript
SerializableAttribute 
ComVisibleAttribute(true) 
public final class Bitmap extends Image
XAML
Not applicable.
Remarks

A bitmap consists of the pixel data for a graphics image and its attributes. You can create images from files, streams, and other sources by using one of the Bitmap constructors and save them to a stream or to the file system with the Save method. Images are drawn to the screen or to memory by using the DrawImage method of the Graphics object. For a list of topics about working with image files, see Working with Images, Bitmaps, and Metafiles.

NoteNote:

   The Bitmap class is not accessible across application domains. For example, if you create a dynamic AppDomain and create several brushes, pens, and bitmaps in that domain, then pass these objects back to the main application domain, you can successfully use the pens and brushes. However, if you call the DrawImage method to draw the marshaled Bitmap, you receive the following exception.

Remoting cannot find field "native image" on type "System.Drawing.Image".

Example

The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from the SampImag.jpg file in the folder of the example.

  • Creates a point at which to draw the upper-left corner of the image.

  • Draws the unscaled image to the screen.

Visual Basic
Private Sub DrawImagePoint(ByVal e As PaintEventArgs)

    ' Create image.
    Dim newImage As Image = Image.FromFile("SampImag.jpg")

    ' Create Point for upper-left corner of image.
    Dim ulCorner As New Point(100, 100)

    ' Draw image to screen.
    e.Graphics.DrawImage(newImage, ulCorner)
End Sub
C#
private void DrawImagePoint(PaintEventArgs e)
{         
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create Point for upper-left corner of image.
    Point ulCorner = new Point(100, 100);
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, ulCorner);
}
C++
private:
   void DrawImagePoint( PaintEventArgs^ e )
   {
      // Create image.
      Image^ newImage = Image::FromFile( "SampImag.jpg" );

      // Create Point for upper-left corner of image.
      Point ulCorner = Point(100,100);

      // Draw image to screen.
      e->Graphics->DrawImage( newImage, ulCorner );
   }
Inheritance Hierarchy

System.Object
   System.MarshalByRefObject
     System.Drawing.Image
      System.Drawing.Bitmap
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

Version Information

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
See Also

Tags :


Community Content

David M. Kean - MSFT
Convert a Bitmap to an Icon

You can convert a bitmap to a icon by doing the following:

[C#]
 
Bitmap bitmap = new Bitmap(16, 16);

 
[..]
 
Icon icon = Icon.FromHandle(bitmap.GetHicon());
Tags :

sinm
To resize and comment out an image
[C#]
void scaleAndComment(Bitmap original, ref Bitmap scaled, string comment, SmoothingMode mode, Font comment_font)
{
using (Graphics g = Graphics.FromImage((Image)scaled))
{
g.SmoothingMode = mode;
g.DrawImage(original, 0, 0, scaled.Width, scaled.Height);
if (!String.IsNullOrEmpty(comment))
{
StringFormat sFormat = new StringFormat(StringFormat.GenericTypographic);
SizeF textArea = g.MeasureString(comment, comment_font, new PointF(0, 0), sFormat);
float lx = (scaled.Width - textArea.Width) / 2;
float ly = -8 + scaled.Height - textArea.Height;
PointF location = new PointF(lx, ly);
g.FillRectangle(System.Drawing.Brushes.Black, new RectangleF(location, textArea));
g.DrawString(comment, comment_font, System.Drawing.Brushes.White, location, sFormat);
}
}
}
void usage()
{
[..]
using (Bitmap original)
{
using (Bitmap scaled = new Bitmap(scaled_width, scaled_height))
{
scaleAndComment(original, ref scaled, "Comment", SmoothingMode.AntiAlias, new Font("Arial", 8))
[..]
}
}
}
Tags :

Page view tracker