Image::Width Property
Gets the width, in pixels, of this Image.
Assembly: System.Drawing (in System.Drawing.dll)
The following code example demonstrates how to construct a new Bitmap from a file, using the GetPixel and SetPixel methods to recolor the image. It also uses the PixelFormat, Width, and Height properties.
This example is designed to be used with a Windows Forms that contains a Label ,PictureBox and Button named Label1, PictureBox1 and Button1, respectively. Paste the code into the form and associate the Button1_Click method with the button's Click event.
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." ); } }
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.