Edit

Share via


Bitmap Constructors

Definition

Initializes a new instance of the Bitmap class.

Overloads

Bitmap(Image)

Initializes a new instance of the Bitmap class from the specified existing image.

Bitmap(Stream)

Initializes a new instance of the Bitmap class from the specified data stream.

Bitmap(String)

Initializes a new instance of the Bitmap class from the specified file.

Bitmap(Image, Size)

Initializes a new instance of the Bitmap class from the specified existing image, scaled to the specified size.

Bitmap(Int32, Int32)

Initializes a new instance of the Bitmap class with the specified size.

Bitmap(Stream, Boolean)

Initializes a new instance of the Bitmap class from the specified data stream.

Bitmap(String, Boolean)

Initializes a new instance of the Bitmap class from the specified file.

Bitmap(Type, String)

Initializes a new instance of the Bitmap class from a specified resource.

Bitmap(Image, Int32, Int32)

Initializes a new instance of the Bitmap class from the specified existing image, scaled to the specified size.

Bitmap(Int32, Int32, Graphics)

Initializes a new instance of the Bitmap class with the specified size and with the resolution of the specified Graphics object.

Bitmap(Int32, Int32, PixelFormat)

Initializes a new instance of the Bitmap class with the specified size and format.

Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr)

Initializes a new instance of the Bitmap class with the specified size, pixel format, and pixel data.

Bitmap(Image)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs

Initializes a new instance of the Bitmap class from the specified existing image.

C#
public Bitmap(System.Drawing.Image original);

Parameters

original
Image

The Image from which to create the new Bitmap.

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Bitmap(Stream)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs

Initializes a new instance of the Bitmap class from the specified data stream.

C#
public Bitmap(System.IO.Stream stream);

Parameters

stream
Stream

The data stream used to load the image.

Exceptions

stream does not contain image data or is null.

-or-

stream contains a PNG image file with a single dimension greater than 65,535 pixels.

Examples

The following code example demonstrates how to load a bitmap from a stream.

This example is designed to be used with Windows Forms. Create a form that contains a PictureBox named PictureBox1. Paste the code into the form and call the InitializeStreamBitmap method from the form's constructor or Load event-handling method.

C#
private void InitializeStreamBitmap()
{
    try
    {
        System.Net.WebRequest request = 
            System.Net.WebRequest.Create(
            "http://www.microsoft.com//h/en-us/r/ms_masthead_ltr.gif");
        System.Net.WebResponse response = request.GetResponse();
        System.IO.Stream responseStream = 
            response.GetResponseStream();
        Bitmap bitmap2 = new Bitmap(responseStream);
        PictureBox1.Image = bitmap2;
    }
    catch(System.Net.WebException)
    {
        MessageBox.Show("There was an error opening the image file."
           + "Check the URL");
    }
}

Remarks

You must keep the stream open for the lifetime of the Bitmap.

Due to a limitation of the GDI+ decoder, an System.ArgumentException is thrown if you construct a bitmap from a .png image file with a single dimension greater than 65,535 pixels.

See also

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Bitmap(String)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs

Initializes a new instance of the Bitmap class from the specified file.

C#
public Bitmap(string filename);

Parameters

filename
String

The bitmap file name and path.

Exceptions

The specified file is not found.

Remarks

The file name and path can be relative to the application or an absolute path. Use this constructor to open images with the following file formats: BMP, GIF, EXIF, JPG, PNG and TIFF. For more information about supported formats, see Types of Bitmaps. The file remains locked until the Bitmap is disposed.

See also

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Bitmap(Image, Size)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs

Initializes a new instance of the Bitmap class from the specified existing image, scaled to the specified size.

C#
public Bitmap(System.Drawing.Image original, System.Drawing.Size newSize);

Parameters

original
Image

The Image from which to create the new Bitmap.

newSize
Size

The Size structure that represent the size of the new Bitmap.

Exceptions

The operation failed.

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Bitmap(Int32, Int32)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs

Initializes a new instance of the Bitmap class with the specified size.

C#
public Bitmap(int width, int height);

Parameters

width
Int32

The width, in pixels, of the new Bitmap.

height
Int32

The height, in pixels, of the new Bitmap.

Exceptions

The operation failed.

Remarks

This constructor creates a Bitmap with a PixelFormat enumeration value of Format32bppArgb.

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Bitmap(Stream, Boolean)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs

Initializes a new instance of the Bitmap class from the specified data stream.

C#
public Bitmap(System.IO.Stream stream, bool useIcm);

Parameters

stream
Stream

The data stream used to load the image.

useIcm
Boolean

true to use color correction for this Bitmap; otherwise, false.

Exceptions

stream does not contain image data or is null.

-or-

stream contains a PNG image file with a single dimension greater than 65,535 pixels.

Remarks

You must keep the stream open for the lifetime of the Bitmap.

Due to a limitation of the GDI+ decoder, an System.ArgumentException is thrown if you construct a bitmap from a .png image file with a single dimension greater than 65,535 pixels.

See also

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Bitmap(String, Boolean)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs

Initializes a new instance of the Bitmap class from the specified file.

C#
public Bitmap(string filename, bool useIcm);

Parameters

filename
String

The name of the bitmap file.

useIcm
Boolean

true to use color correction for this Bitmap; otherwise, false.

Examples

The following code example demonstrates how to construct a new bitmap from a file. The example uses the GetPixel and SetPixel methods to recolor the image. It also uses the PixelFormat property.

This example is designed to be used with a Windows Form 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.

C#
Bitmap image1;

private void Button1_Click(System.Object sender, System.EventArgs e)
{

    try
    {
        // 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.");
    }
}

Remarks

Use this constructor to open images with the following file formats: BMP, GIF, EXIF, JPG, PNG and TIFF. For more information about supported formats, see Types of Bitmaps. The file remains locked until the Bitmap is disposed.

See also

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Bitmap(Type, String)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs

Initializes a new instance of the Bitmap class from a specified resource.

C#
public Bitmap(Type type, string resource);

Parameters

type
Type

The class used to extract the resource.

resource
String

The name of the resource.

Examples

The following code example demonstrates how to construct a bitmap from a type, and how to use the Save method. To run this example, paste the code into a Windows Form. Handle the form's Paint event and call the ConstructFromResourceSaveAsGif method, passing e as PaintEventArgs

C#
private void ConstructFromResourceSaveAsGif(PaintEventArgs e)
{

    // Construct a bitmap from the button image resource.
    Bitmap bmp1 = new Bitmap(typeof(Button), "Button.bmp");

    // Save the image as a GIF.
    bmp1.Save("c:\\button.gif", System.Drawing.Imaging.ImageFormat.Gif);

    // Construct a new image from the GIF file.
    Bitmap bmp2 = new Bitmap("c:\\button.gif");

    // Draw the two images.
    e.Graphics.DrawImage(bmp1, new Point(10, 10));
    e.Graphics.DrawImage(bmp2, new Point(10, 40));

    // Dispose of the image files.
    bmp1.Dispose();
    bmp2.Dispose();
}

Remarks

This constructor combines the namespace of the given type with the string name of the resource and looks for a match in the assembly manifest. For example you can pass in the Button type and Button.bmp to this constructor and it will look for a resource named System.Windows.Forms.Button.bmp.

See also

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Bitmap(Image, Int32, Int32)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs

Initializes a new instance of the Bitmap class from the specified existing image, scaled to the specified size.

C#
public Bitmap(System.Drawing.Image original, int width, int height);

Parameters

original
Image

The Image from which to create the new Bitmap.

width
Int32

The width, in pixels, of the new Bitmap.

height
Int32

The height, in pixels, of the new Bitmap.

Exceptions

The operation failed.

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Bitmap(Int32, Int32, Graphics)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs

Initializes a new instance of the Bitmap class with the specified size and with the resolution of the specified Graphics object.

C#
public Bitmap(int width, int height, System.Drawing.Graphics g);

Parameters

width
Int32

The width, in pixels, of the new Bitmap.

height
Int32

The height, in pixels, of the new Bitmap.

g
Graphics

The Graphics object that specifies the resolution for the new Bitmap.

Exceptions

Remarks

The new Bitmap that this method creates takes its horizontal and vertical resolution from the DpiX and DpiY properties of g, respectively.

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Bitmap(Int32, Int32, PixelFormat)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs

Initializes a new instance of the Bitmap class with the specified size and format.

C#
public Bitmap(int width, int height, System.Drawing.Imaging.PixelFormat format);

Parameters

width
Int32

The width, in pixels, of the new Bitmap.

height
Int32

The height, in pixels, of the new Bitmap.

format
PixelFormat

The pixel format for the new Bitmap. This must specify a value that begins with Format.

Exceptions

A PixelFormat value is specified whose name does not start with Format. For example, specifying Gdi will cause an ArgumentException, but Format48bppRgb will not.

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs

Initializes a new instance of the Bitmap class with the specified size, pixel format, and pixel data.

C#
public Bitmap(int width, int height, int stride, System.Drawing.Imaging.PixelFormat format, IntPtr scan0);

Parameters

width
Int32

The width, in pixels, of the new Bitmap.

height
Int32

The height, in pixels, of the new Bitmap.

stride
Int32

Integer that specifies the byte offset between the beginning of one scan line and the next. This is usually (but not necessarily) the number of bytes in the pixel format (for example, 2 for 16 bits per pixel) multiplied by the width of the bitmap. The value passed to this parameter must be a multiple of four.

format
PixelFormat

The pixel format for the new Bitmap. This must specify a value that begins with Format.

scan0
IntPtr

Pointer to an array of bytes that contains the pixel data.

Exceptions

A PixelFormat value is specified whose name does not start with Format. For example, specifying Gdi will cause an ArgumentException, but Format48bppRgb will not.

Examples

The following code example shows how to use the Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr) constructor. This example is designed to be used with Windows Forms and requires a PaintEventArgs parameter, which is a parameter of the Paint event.

C#
private void BitmapConstructorEx(PaintEventArgs e)
{

    // Create a bitmap.
    Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");
    
   // Retrieve the bitmap data from the bitmap.
    System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), 
        ImageLockMode.ReadOnly, bmp.PixelFormat);

    //Create a new bitmap.
    Bitmap newBitmap = new Bitmap(200, 200, bmpData.Stride, bmp.PixelFormat, bmpData.Scan0);

    bmp.UnlockBits(bmpData);

    // Draw the new bitmap.
    e.Graphics.DrawImage(newBitmap, 10, 10);
}

Remarks

The caller is responsible for allocating and freeing the block of memory specified by the scan0 parameter. However, the memory should not be released until the related Bitmap is released.

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10