Freigeben über


Bitmap-Konstruktor (String, Boolean)

Initialisiert eine neue Instanz der Bitmap-Klasse aus der angegebenen Datei.

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

Syntax

'Declaration
Public Sub New ( _
    filename As String, _
    useIcm As Boolean _
)
'Usage
Dim filename As String
Dim useIcm As Boolean

Dim instance As New Bitmap(filename, useIcm)
public Bitmap (
    string filename,
    bool useIcm
)
public:
Bitmap (
    String^ filename, 
    bool useIcm
)
public Bitmap (
    String filename, 
    boolean useIcm
)
public function Bitmap (
    filename : String, 
    useIcm : boolean
)

Parameter

  • filename
    Der Name der Bitmapdatei.
  • useIcm
    true, wenn für diese Bitmap Farbkorrektur verwendet werden soll, andernfalls false.

Ausnahmen

Ausnahmetyp Bedingung

System.ArgumentException

stream enthält keine Bilddaten oder ist NULL (Nothing in Visual Basic).

– oder –

stream enthält eine eindimensionale PNG-Bilddatei, deren Umfang 65.535 Pixel überschreitet.

Hinweise

Die Datei bleibt gesperrt, bis die Bitmap freigegeben wird.

Aufgrund der Beschränkung des GDI+-Decoders wird eine System.ArgumentException ausgelöst, wenn Sie eine Bitmap aus einer eindimensionalen PNG-Bilddatei erstellen, deren Umfang 65.535 Pixel überschreitet.

Beispiel

Im folgenden Codebeispiel wird veranschaulicht, wie aus einer Datei eine neue Bitmap erstellt wird, wobei die GetPixel-Methode und die SetPixel-Methode zum Neueinfärben des Bildes verwendet werden. Die PixelFormat-Eigenschaft wird ebenfalls verwendet.

Dieses Beispiel ist für die Verwendung mit einem Windows Form vorgesehen, das jeweils ein Label, PictureBox und Button mit den Bezeichnungen Label1, PictureBox1 bzw. Button1 enthält. Fügen Sie den Code in das Formular ein, und ordnen Sie dem Click-Ereignis der Schaltfläche die Button1_Click-Methode zu.

Dim image1 As Bitmap

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click

    Try
        ' 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)
            Next
        Next

        ' 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
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.");
    }
}
private:
   Bitmap^ image1;
   void Button1_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      try
      {
         
         // 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." );
      }

   }
private Bitmap image1;

private void button1_Click(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.get_Width(); x++) {
            for (y = 0; y < image1.get_Height(); y++) {
                Color pixelColor = image1.GetPixel(x, y);
                Color newColor = Color.FromArgb(pixelColor.get_R(), 0, 0);

                image1.SetPixel(x, y, newColor);
            }
        }

        // Set the PictureBox to display the image.
        pictureBox1.set_Image(image1);

        // Display the pixel format in label1.
        label1.set_Text("Pixel format: " 
            + image1.get_PixelFormat().ToString());
    }
    catch (ArgumentException exp) {
        MessageBox.Show(("There was an error."  
            + "Check the path to the image file."));
    }
} //button1_Click

.NET Framework-Sicherheit

Plattformen

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

Bitmap-Klasse
Bitmap-Member
System.Drawing-Namespace

Weitere Ressourcen

Bitmaptypen