Bitmap Constructeurs

Définition

Initialise une nouvelle instance de la classe Bitmap.

Surcharges

Bitmap(Image)

Initialise une nouvelle instance de la classe Bitmap à partir de l'image existante spécifiée.

Bitmap(Stream)

Initialise une nouvelle instance de la classe Bitmap à partir du flux de données spécifié.

Bitmap(String)

Initialise une nouvelle instance de la classe Bitmap à partir du fichier spécifié.

Bitmap(Image, Size)

Initialise une nouvelle instance de la classe Bitmap à partir de l'image existante spécifiée, ajustée à la taille spécifiée.

Bitmap(Int32, Int32)

Initialise une nouvelle instance de la classe Bitmap avec la taille spécifiée.

Bitmap(Stream, Boolean)

Initialise une nouvelle instance de la classe Bitmap à partir du flux de données spécifié.

Bitmap(String, Boolean)

Initialise une nouvelle instance de la classe Bitmap à partir du fichier spécifié.

Bitmap(Type, String)

Initialise une nouvelle instance de la classe Bitmap à partir d'une ressource spécifiée.

Bitmap(Image, Int32, Int32)

Initialise une nouvelle instance de la classe Bitmap à partir de l'image existante spécifiée, ajustée à la taille spécifiée.

Bitmap(Int32, Int32, Graphics)

Initialise une nouvelle instance de la classe Bitmap avec la taille spécifiée et la résolution de l'objet Graphics spécifié.

Bitmap(Int32, Int32, PixelFormat)

Initialise une nouvelle instance de la classe Bitmap avec la taille et le format spécifiés.

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

Initialise une nouvelle instance de la classe Bitmap avec la taille ainsi que le format et les données de pixels spécifiés.

Bitmap(Image)

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

Initialise une nouvelle instance de la classe Bitmap à partir de l'image existante spécifiée.

public:
 Bitmap(System::Drawing::Image ^ original);
public Bitmap (System.Drawing.Image original);
new System.Drawing.Bitmap : System.Drawing.Image -> System.Drawing.Bitmap
Public Sub New (original As Image)

Paramètres

original
Image

Image à partir duquel créer le Bitmap.

S’applique à

Bitmap(Stream)

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

Initialise une nouvelle instance de la classe Bitmap à partir du flux de données spécifié.

public:
 Bitmap(System::IO::Stream ^ stream);
public Bitmap (System.IO.Stream stream);
new System.Drawing.Bitmap : System.IO.Stream -> System.Drawing.Bitmap
Public Sub New (stream As Stream)

Paramètres

stream
Stream

Flux de données utilisé pour charger l'image.

Exceptions

stream ne contient pas de donnée image ou est null.

- ou -

stream contient un fichier image PNG avec une seule dimension supérieure à 65 535 pixels.

Exemples

L’exemple de code suivant montre comment charger une bitmap à partir d’un flux.

Cet exemple est conçu pour être utilisé avec Windows Forms. Create un formulaire qui contient un PictureBox nommé PictureBox1. Collez le code dans le formulaire et appelez la InitializeStreamBitmap méthode à partir du constructeur du formulaire ou Load de la méthode de gestion des événements du formulaire.

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 = gcnew Bitmap( responseStream );
      PictureBox1->Image = bitmap2;
   }
   catch ( System::Net::WebException^ ) 
   {
      MessageBox::Show( "There was an error opening the image file."
      "Check the URL" );
   }

}
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");
    }
}
Private Sub InitializeStreamBitmap()
    Try
        Dim request As System.Net.WebRequest = _
            System.Net.WebRequest.Create( _
            "http://www.microsoft.com//h/en-us/r/ms_masthead_ltr.gif")
        Dim response As System.Net.WebResponse = request.GetResponse()
        Dim responseStream As System.IO.Stream = response.GetResponseStream()
        Dim bitmap2 As New Bitmap(responseStream)
        PictureBox1.Image = bitmap2

    Catch ex As System.Net.WebException
        MessageBox.Show("There was an error opening the image file. Check the URL")
    End Try
End Sub

Remarques

Vous devez conserver le flux ouvert pendant toute la durée de vie de .Bitmap

En raison d’une limitation du décodeur GDI+, une System.ArgumentException est levée si vous construisez une bitmap à partir d’un fichier image .png avec une dimension unique supérieure à 65 535 pixels.

Voir aussi

S’applique à

Bitmap(String)

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

Initialise une nouvelle instance de la classe Bitmap à partir du fichier spécifié.

public:
 Bitmap(System::String ^ filename);
public Bitmap (string filename);
new System.Drawing.Bitmap : string -> System.Drawing.Bitmap
Public Sub New (filename As String)

Paramètres

filename
String

Nom de fichier et chemin d'accès de l'image bitmap.

Exceptions

Le fichier spécifié est introuvable.

Remarques

Le nom de fichier et le chemin d’accès peuvent être relatifs à l’application ou à un chemin absolu. Utilisez ce constructeur pour ouvrir des images avec les formats de fichier suivants : BMP, GIF, EXIF, JPG, PNG et TIFF. Pour plus d’informations sur les formats pris en charge, consultez Types de bitmaps. Le fichier reste verrouillé jusqu’à ce que le Bitmap soit supprimé.

Voir aussi

S’applique à

Bitmap(Image, Size)

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

Initialise une nouvelle instance de la classe Bitmap à partir de l'image existante spécifiée, ajustée à la taille spécifiée.

public:
 Bitmap(System::Drawing::Image ^ original, System::Drawing::Size newSize);
public Bitmap (System.Drawing.Image original, System.Drawing.Size newSize);
new System.Drawing.Bitmap : System.Drawing.Image * System.Drawing.Size -> System.Drawing.Bitmap
Public Sub New (original As Image, newSize As Size)

Paramètres

original
Image

Image à partir duquel créer le Bitmap.

newSize
Size

Structure Size représentant la taille du nouveau Bitmap.

Exceptions

L'opération a échoué.

S’applique à

Bitmap(Int32, Int32)

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

Initialise une nouvelle instance de la classe Bitmap avec la taille spécifiée.

public:
 Bitmap(int width, int height);
public Bitmap (int width, int height);
new System.Drawing.Bitmap : int * int -> System.Drawing.Bitmap
Public Sub New (width As Integer, height As Integer)

Paramètres

width
Int32

Largeur, en pixels, du nouveau Bitmap.

height
Int32

Hauteur, en pixels, du nouveau Bitmap.

Exceptions

L'opération a échoué.

Remarques

Ce constructeur crée un Bitmap avec une valeur d’énumération PixelFormat de Format32bppArgb.

S’applique à

Bitmap(Stream, Boolean)

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

Initialise une nouvelle instance de la classe Bitmap à partir du flux de données spécifié.

public:
 Bitmap(System::IO::Stream ^ stream, bool useIcm);
public Bitmap (System.IO.Stream stream, bool useIcm);
new System.Drawing.Bitmap : System.IO.Stream * bool -> System.Drawing.Bitmap
Public Sub New (stream As Stream, useIcm As Boolean)

Paramètres

stream
Stream

Flux de données utilisé pour charger l'image.

useIcm
Boolean

true pour appliquer la correction des couleurs à ce Bitmap ; sinon, false.

Exceptions

stream ne contient pas de donnée image ou est null.

- ou -

stream contient un fichier image PNG avec une seule dimension supérieure à 65 535 pixels.

Remarques

Vous devez conserver le flux ouvert pendant toute la durée de vie de .Bitmap

En raison d’une limitation du décodeur GDI+, une System.ArgumentException est levée si vous construisez une bitmap à partir d’un fichier image .png avec une dimension unique supérieure à 65 535 pixels.

Voir aussi

S’applique à

Bitmap(String, Boolean)

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

Initialise une nouvelle instance de la classe Bitmap à partir du fichier spécifié.

public:
 Bitmap(System::String ^ filename, bool useIcm);
public Bitmap (string filename, bool useIcm);
new System.Drawing.Bitmap : string * bool -> System.Drawing.Bitmap
Public Sub New (filename As String, useIcm As Boolean)

Paramètres

filename
String

Nom du fichier bitmap.

useIcm
Boolean

true pour appliquer la correction des couleurs à ce Bitmap ; sinon, false.

Exemples

L’exemple de code suivant montre comment construire une nouvelle bitmap à partir d’un fichier. L’exemple utilise les GetPixel méthodes et SetPixel pour recolorier l’image. Il utilise également la PixelFormat propriété .

Cet exemple est conçu pour être utilisé avec un Windows Form qui contient un Label, PictureBox et Button nommé Label1, PictureBox1 et Button1, respectivement. Collez le code dans le formulaire et associez la Button1_Click méthode à l’événement du Click bouton.

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

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

Remarques

Utilisez ce constructeur pour ouvrir des images avec les formats de fichier suivants : BMP, GIF, EXIF, JPG, PNG et TIFF. Pour plus d’informations sur les formats pris en charge, consultez Types de bitmaps. Le fichier reste verrouillé jusqu’à ce que le Bitmap soit supprimé.

Voir aussi

S’applique à

Bitmap(Type, String)

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

Initialise une nouvelle instance de la classe Bitmap à partir d'une ressource spécifiée.

public:
 Bitmap(Type ^ type, System::String ^ resource);
public Bitmap (Type type, string resource);
new System.Drawing.Bitmap : Type * string -> System.Drawing.Bitmap
Public Sub New (type As Type, resource As String)

Paramètres

type
Type

Classe utilisée pour extraire la ressource.

resource
String

Nom de la ressource.

Exemples

L’exemple de code suivant montre comment construire une bitmap à partir d’un type et comment utiliser la Save méthode . Pour exécuter cet exemple, collez le code dans un Windows Form. Gérer l’événement du Paint formulaire et appeler la ConstructFromResourceSaveAsGif méthode, en passant e comme PaintEventArgs

private:
    void ConstructFromResourceSaveAsGif(PaintEventArgs^ e)
    {
        // Construct a bitmap from the button image resource.
        Bitmap^ bmp1 = gcnew Bitmap(Button::typeid, "Button.bmp");
        String^ savePath =  
            Environment::GetEnvironmentVariable("TEMP") + "\\Button.bmp";

        try
        {
            // Save the image as a GIF.
            bmp1->Save(savePath, System::Drawing::Imaging::ImageFormat::Gif);
        }
        catch (IOException^)
        {
            // Carry on regardless
        }

        // Construct a new image from the GIF file.
        Bitmap^ bmp2 = nullptr;
        if (File::Exists(savePath))
        {
            bmp2 = gcnew Bitmap(savePath);
        }

        // Draw the two images.
        e->Graphics->DrawImage(bmp1, Point(10, 10));

        // If bmp1 did not save to disk, bmp2 may be null
        if (bmp2 != nullptr)
        {
            e->Graphics->DrawImage(bmp2, Point(10, 40));
        }

        // Dispose of the image files.
        delete bmp1;
        if (bmp2 != nullptr)
        {
            delete bmp2;
        }
    }
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();
}
Private Sub ConstructFromResourceSaveAsGif(ByVal e As PaintEventArgs)

    ' Construct a bitmap from the button image resource.
    Dim bmp1 As New Bitmap(GetType(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.
    Dim bmp2 As 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()
End Sub

Remarques

Ce constructeur combine l’espace de noms du type donné avec le nom de chaîne de la ressource et recherche une correspondance dans le manifeste de l’assembly. Par exemple, vous pouvez passer le Button type et Button.bmp à ce constructeur pour rechercher une ressource nommée System.Windows.Forms.Button.bmp.

Voir aussi

S’applique à

Bitmap(Image, Int32, Int32)

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

Initialise une nouvelle instance de la classe Bitmap à partir de l'image existante spécifiée, ajustée à la taille spécifiée.

public:
 Bitmap(System::Drawing::Image ^ original, int width, int height);
public Bitmap (System.Drawing.Image original, int width, int height);
new System.Drawing.Bitmap : System.Drawing.Image * int * int -> System.Drawing.Bitmap
Public Sub New (original As Image, width As Integer, height As Integer)

Paramètres

original
Image

Image à partir duquel créer le Bitmap.

width
Int32

Largeur, en pixels, du nouveau Bitmap.

height
Int32

Hauteur, en pixels, du nouveau Bitmap.

Exceptions

L'opération a échoué.

S’applique à

Bitmap(Int32, Int32, Graphics)

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

Initialise une nouvelle instance de la classe Bitmap avec la taille spécifiée et la résolution de l'objet Graphics spécifié.

public:
 Bitmap(int width, int height, System::Drawing::Graphics ^ g);
public Bitmap (int width, int height, System.Drawing.Graphics g);
new System.Drawing.Bitmap : int * int * System.Drawing.Graphics -> System.Drawing.Bitmap
Public Sub New (width As Integer, height As Integer, g As Graphics)

Paramètres

width
Int32

Largeur, en pixels, du nouveau Bitmap.

height
Int32

Hauteur, en pixels, du nouveau Bitmap.

g
Graphics

Objet Graphics spécifiant la résolution du nouveau Bitmap.

Exceptions

g a la valeur null.

Remarques

Le nouveau Bitmap créé par cette méthode prend sa résolution horizontale et verticale à partir des DpiX propriétés et DpiY de g, respectivement.

S’applique à

Bitmap(Int32, Int32, PixelFormat)

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

Initialise une nouvelle instance de la classe Bitmap avec la taille et le format spécifiés.

public:
 Bitmap(int width, int height, System::Drawing::Imaging::PixelFormat format);
public Bitmap (int width, int height, System.Drawing.Imaging.PixelFormat format);
new System.Drawing.Bitmap : int * int * System.Drawing.Imaging.PixelFormat -> System.Drawing.Bitmap
Public Sub New (width As Integer, height As Integer, format As PixelFormat)

Paramètres

width
Int32

Largeur, en pixels, du nouveau Bitmap.

height
Int32

Hauteur, en pixels, du nouveau Bitmap.

format
PixelFormat

Format de pixel du nouveau Bitmap. Cela doit spécifier une valeur qui commence Formatpar .

Exceptions

Une valeur PixelFormat dont le nom ne commence pas par Format est spécifiée. Par exemple, Gdi provoquera une ArgumentException, mais pas Format48bppRgb.

S’applique à

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

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

Initialise une nouvelle instance de la classe Bitmap avec la taille ainsi que le format et les données de pixels spécifiés.

public:
 Bitmap(int width, int height, int stride, System::Drawing::Imaging::PixelFormat format, IntPtr scan0);
public Bitmap (int width, int height, int stride, System.Drawing.Imaging.PixelFormat format, IntPtr scan0);
new System.Drawing.Bitmap : int * int * int * System.Drawing.Imaging.PixelFormat * nativeint -> System.Drawing.Bitmap
Public Sub New (width As Integer, height As Integer, stride As Integer, format As PixelFormat, scan0 As IntPtr)

Paramètres

width
Int32

Largeur, en pixels, du nouveau Bitmap.

height
Int32

Hauteur, en pixels, du nouveau Bitmap.

stride
Int32

Entier qui spécifie l'offset d'octet entre le début d'une ligne de numérisation et la suivante. Il s'agit généralement (mais pas nécessairement) du nombre d'octets en format de pixel (par exemple, 2 pour 16 bits par pixel) multiplié par la largeur de la bitmap. La valeur passée à ce paramètre doit être un multiple de quatre.

format
PixelFormat

Format de pixel du nouveau Bitmap. Cela doit spécifier une valeur qui commence Formatpar .

scan0
IntPtr

nativeint

Pointeur vers un tableau d'octets qui contient les données de pixels.

Exceptions

Une valeur PixelFormat dont le nom ne commence pas par Format est spécifiée. Par exemple, Gdi provoquera une ArgumentException, mais pas Format48bppRgb.

Exemples

L’exemple de code suivant montre comment utiliser le Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr) constructeur. Cet exemple est conçu pour être utilisé avec Windows Forms et nécessite un PaintEventArgs paramètre, qui est un paramètre de l’événementPaint.

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);
}
Private Sub BitmapConstructorEx(ByVal e As PaintEventArgs)

    ' Create a bitmap.
    Dim bmp As New Bitmap("c:\fakePhoto.jpg")

    ' Retrieve the bitmap data from the bitmap.
    Dim bmpData As System.Drawing.Imaging.BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), _
        ImageLockMode.ReadOnly, bmp.PixelFormat)

    'Create a new bitmap.
    Dim newBitmap As New Bitmap(200, 200, bmpData.Stride, bmp.PixelFormat, bmpData.Scan0)

    bmp.UnlockBits(bmpData)

    ' Draw the new bitmap.
    e.Graphics.DrawImage(newBitmap, 10, 10)

End Sub

Remarques

L’appelant est responsable de l’allocation et de la libération du bloc de mémoire spécifié par le scan0 paramètre. Toutefois, la mémoire ne doit pas être libérée tant que la mémoire associée Bitmap n’est pas libérée.

S’applique à