Image.PixelFormat 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取此 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
属性值
PixelFormat,表示此 Image 的像素格式。
示例
下面的代码示例演示如何使用 GetPixel 和 SetPixel 方法从文件构造新位图,以重新着色图像。 它还使用 PixelFormat 属性。
此示例旨在与 Windows 窗体一起使用,该窗体分别包含 Label、 PictureBox 和 Button 名为 Label1
、 PictureBox1
和 Button1
。 将代码粘贴到窗体中,并将 Button1_Click
方法与按钮的事件 Click 相关联。
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