Bitmap Constructor (String, Boolean)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

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

Public Sub New (
	filename As String,
	useIcm As Boolean


Type: System.String

The name of the bitmap file.

Type: System.Boolean

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

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.

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.

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", _

        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


for calling into unmanaged code. Related enumeration: UnmanagedCode

.NET Framework
Available since 1.1
Return to top