Image.PixelFormat Propriété
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient le format de pixel de ce Image.
public:
property System::Drawing::Imaging::PixelFormat PixelFormat { System::Drawing::Imaging::PixelFormat get(); };
public System.Drawing.Imaging.PixelFormat PixelFormat { get; }
member this.PixelFormat : System.Drawing.Imaging.PixelFormat
Public ReadOnly Property PixelFormat As PixelFormat
Valeur de propriété
PixelFormat qui représente le format de pixel de ce Image.
Exemples
L’exemple de code suivant montre comment construire une nouvelle bitmap à partir d’un fichier, à l’aide des GetPixel méthodes et SetPixel pour recolorer l’image. Il utilise également la PixelFormat propriété .
Cet exemple est conçu pour être utilisé avec un Windows Form qui contient respectivement , LabelPictureBox et Button nommé Label1
, PictureBox1
et , et Button1
. 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