Image 类
为源自 Bitmap 和 Metafile 的类提供功能的抽象基类。
**命名空间:**System.Drawing
**程序集:**System.Drawing(在 system.drawing.dll 中)
语法
声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public MustInherit Class Image
Inherits MarshalByRefObject
Implements ISerializable, ICloneable, IDisposable
用法
Dim instance As Image
[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class Image : MarshalByRefObject, ISerializable, ICloneable, IDisposable
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class Image abstract : public MarshalByRefObject, ISerializable, ICloneable, IDisposable
/** @attribute SerializableAttribute() */
/** @attribute ComVisibleAttribute(true) */
public abstract class Image extends MarshalByRefObject implements ISerializable, ICloneable,
IDisposable
SerializableAttribute
ComVisibleAttribute(true)
public abstract class Image extends MarshalByRefObject implements ISerializable, ICloneable,
IDisposable
示例
下面的代码示例演示了如何使用 GetPixel 和 SetPixel 方法从文件构造新的 Bitmap,为图像重新着色。它还使用 PixelFormat、Width 和 Height 属性。
此示例旨在用于包含名为 Label1
的 Label、名为 PictureBox1
的 PictureBox 和名为 Button1
的 Button 的 Windows 窗体。将代码粘贴到该窗体中,并将 Button1_Click
方法与按钮的 Click 事件关联。
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
继承层次结构
System.Object
System.MarshalByRefObject
System.Drawing.Image
System.Drawing.Bitmap
System.Drawing.Imaging.Metafile
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0