This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Download Microsoft Edge
More info about Internet Explorer and Microsoft Edge
public ref class Bitmap sealed : System::Drawing::Image
[System.Serializable]
public sealed class Bitmap : System.Drawing.Image
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class Bitmap : System.Drawing.Image
public sealed class Bitmap : System.Drawing.Image
[<System.Serializable>]
type Bitmap = class
inherit Image
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Bitmap = class
inherit Image
type Bitmap = class
inherit Image
Public NotInheritable Class Bitmap
Inherits Image
Inheritance
Bitmap
Examples
The following code example demonstrates how to construct a new
Bitmap
from a file, using the
GetPixel
and
SetPixel
methods to recolor the image. It also uses the
PixelFormat
,
Width
, and
Height
properties.
This example is designed to be used with a Windows Forms that contains a
Label
,
PictureBox
, and
Button
named
Label1
,
PictureBox1
, and
Button1
, respectively. Paste the code into the form and associate the
Button1_Click
method with the button's
Click
event.
private:
Bitmap^ image1;
void Button1_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
// Retrieve the image.
image1 = gcnew Bitmap( "C:\\Documents and Settings\\All Users\\"
"Documents\\My Music\\music.bmp",true );
int x;
int y;
// Loop through the images pixels to reset color.
for ( x = 0; x < image1->Width; x++ )
for ( y = 0; y < image1->Height; y++ )
Color pixelColor = image1->GetPixel( x, y );
Color newColor = Color::FromArgb( pixelColor.R, 0, 0 );
image1->SetPixel( x, y, newColor );
// Set the PictureBox to display the image.
PictureBox1->Image = image1;
// Display the pixel format in Label1.
Label1->Text = String::Format( "Pixel format: {0}", image1->PixelFormat );
catch ( ArgumentException^ )
MessageBox::Show( "There was an error."
"Check the path to the image file." );
Bitmap image1;
private void Button1_Click(System.Object sender, System.EventArgs e)
// Retrieve the image.
image1 = new Bitmap(@"C:\Documents and Settings\All Users\"
+ @"Documents\My Music\music.bmp", true);
int x, y;
// Loop through the images pixels to reset color.
for(x=0; x<image1.Width; x++)
for(y=0; y<image1.Height; y++)
Color pixelColor = image1.GetPixel(x, y);
Color newColor = Color.FromArgb(pixelColor.R, 0, 0);
image1.SetPixel(x, y, newColor);
// Set the PictureBox to display the image.
PictureBox1.Image = image1;
// Display the pixel format in Label1.
Label1.Text = "Pixel format: "+image1.PixelFormat.ToString();
catch(ArgumentException)
MessageBox.Show("There was an error." +
"Check the path to the image file.");
Dim image1 As Bitmap
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' Retrieve the image.
image1 = New Bitmap( _
"C:\Documents and Settings\All Users\Documents\My Music\music.bmp", _
True)
Dim x, y As Integer
' Loop through the images pixels to reset color.
For x = 0 To image1.Width - 1
For y = 0 To image1.Height - 1
Dim pixelColor As Color = image1.GetPixel(x, y)
Dim newColor As Color = _
Color.FromArgb(pixelColor.R, 0, 0)
image1.SetPixel(x, y, newColor)
' Set the PictureBox to display the image.
PictureBox1.Image = image1
' Display the pixel format in Label1.
Label1.Text = "Pixel format: " + image1.PixelFormat.ToString()
Catch ex As ArgumentException
MessageBox.Show("There was an error." _
& "Check the path to the image file.")
End Try
End Sub
A bitmap consists of the pixel data for a graphics image and its attributes. There are many standard formats for saving a bitmap to a file. GDI+ supports the following file formats: BMP, GIF, EXIF, JPG, PNG, and TIFF. For more information about supported formats, see Types of Bitmaps.
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, Icons, and Metafiles.
In .NET 6 and later versions, the System.Drawing.Common package, which includes this type, is only supported on Windows operating systems. Use of this type in cross-platform apps causes compile-time warnings and run-time exceptions. For more information, see System.Drawing.Common only supported on Windows.
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".
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
(Inherited from MarshalByRefObject)
Releases the unmanaged resources used by the Image and optionally releases the managed resources.
(Inherited from Image)
Obsolete.
Retrieves the current lifetime service object that controls the lifetime policy for this instance.
(Inherited from MarshalByRefObject)
Obsolete.
Obtains a lifetime service object to control the lifetime policy for this instance.
(Inherited from MarshalByRefObject)
Saves this image to the specified stream, with the specified encoder and image encoder parameters.
(Inherited from Image)
Saves this Image to the specified file, with the specified encoder and image-encoder parameters.
(Inherited from Image)
Adds a frame to the file or stream specified in a previous call to the Save method. Use this method to save selected frames from a multiple-frame image to another multiple-frame image.
(Inherited from Image)