封装 GDI+ 位图,此位图由图像及其属性的像素数据组成。 Bitmap 是用于处理由像素数据定义的图像的对象。
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public sealed class Bitmap : System.Drawing.Image
可以看出来,它继承于System.Drawing.Image 类,所以可以直接赋值给Image对象,因为派生类对象可以用基类引用
private Bitmap image1;//定义一个Bitmap对象
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;//直接将Bitmap赋值给PictureBox的Image属性
}
catch(ArgumentException)
{
MessageBox.Show("There was an error." +
"Check the path to the image file.");
}
}