Image-Klasse
Assembly: System.Drawing (in system.drawing.dll)
[SerializableAttribute] [ComVisibleAttribute(true)] public abstract class Image : MarshalByRefObject, ISerializable, ICloneable, IDisposable
/** @attribute SerializableAttribute() */ /** @attribute ComVisibleAttribute(true) */ public abstract class Image extends MarshalByRefObject implements ISerializable, ICloneable, IDisposable
Im folgenden Codebeispiel wird veranschaulicht, wie aus einer Datei eine neue Bitmap erstellt wird, wobei die GetPixel-Methode und die SetPixel-Methode zum Neueinfärben des Bilds verwendet werden. Außerdem werden die Eigenschaften PixelFormat, Width und Height verwendet.
Dieses Beispiel ist für die Verwendung mit einem Windows Form vorgesehen, das jeweils eine Instanz von Label, PictureBox und Button mit den Bezeichnungen Label1, PictureBox1 bzw. Button1 enthält. Fügen Sie den Code in das Formular ein, und ordnen Sie die Button1_Click-Methode dem Click-Ereignis der Schaltfläche zu.
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;
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.MarshalByRefObject
System.Drawing.Image
System.Drawing.Bitmap
System.Drawing.Imaging.Metafile
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.