Training
Module
Work with files and directories in a .NET app - Training
Learn how to use .NET, C#, and System.IO to work with directories, paths, files, and the file system.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Demonstrates image manipulation using the Windows SDK.
The following topics demonstrate the use of the System.Drawing.Image class to perform image manipulation.
The following code example modifies the OnPaint event handler to retrieve a pointer to the Graphics object for the main form. The OnPaint function is intended for a Windows Forms application, most likely created with a Visual Studio application wizard.
The image is represented by the Image class. The image data is loaded from a .jpg file using the System.Drawing.Image.FromFile method. Before the image is drawn to the form, the form is resized to accommodate the image. The drawing of the image is performed with the System.Drawing.Graphics.DrawImage method.
The Graphics and Image classes are both in the System.Drawing namespace.
#using <system.drawing.dll>
using namespace System;
using namespace System::Drawing;
protected:
virtual Void Form1::OnPaint(PaintEventArgs^ pe) override
{
Graphics^ g = pe->Graphics;
Image^ image = Image::FromFile("SampleImage.jpg");
Form::ClientSize = image->Size;
g->DrawImage( image, 0, 0, image->Size.Width, image->Size.Height );
}
The following code example uses the Graphics class to modify the OnPaint event handler to retrieve a pointer to the Graphics object for the main form. This pointer is then used to set the background color of the form and draw a line and an arc using the System.Drawing.Graphics.DrawLine and DrawArc methods.
#using <system.drawing.dll>
using namespace System;
using namespace System::Drawing;
// ...
protected:
virtual Void Form1::OnPaint(PaintEventArgs^ pe ) override
{
Graphics^ g = pe->Graphics;
g->Clear(Color::AntiqueWhite);
Rectangle rect = Form::ClientRectangle;
Rectangle smallRect;
smallRect.X = rect.X + rect.Width / 4;
smallRect.Y = rect.Y + rect.Height / 4;
smallRect.Width = rect.Width / 2;
smallRect.Height = rect.Height / 2;
Pen^ redPen = gcnew Pen(Color::Red);
redPen->Width = 4;
g->DrawLine(redPen, 0, 0, rect.Width, rect.Height);
Pen^ bluePen = gcnew Pen(Color::Blue);
bluePen->Width = 10;
g->DrawArc( bluePen, smallRect, 90, 270 );
}
The following code example demonstrates the use of the System.Drawing.Image class to load an image from disk, rotate it 90 degrees, and save it as a new .jpg file.
#using <system.drawing.dll>
using namespace System;
using namespace System::Drawing;
int main()
{
Image^ image = Image::FromFile("SampleImage.jpg");
image->RotateFlip( RotateFlipType::Rotate90FlipNone );
image->Save("SampleImage_rotated.jpg");
return 0;
}
The following code example demonstrates the System.Drawing.Image class and the System.Drawing.Imaging.ImageFormat enumeration used to convert and save image files. The following code loads an image from a .jpg file and then saves it in both .gif and .bmp file formats.
#using <system.drawing.dll>
using namespace System;
using namespace System::Drawing;
using namespace System::Drawing::Imaging;
int main()
{
Image^ image = Image::FromFile("SampleImage.jpg");
image->Save("SampleImage.png", ImageFormat::Png);
image->Save("SampleImage.bmp", ImageFormat::Bmp);
return 0;
}
Getting Started with Graphics Programming
Training
Module
Work with files and directories in a .NET app - Training
Learn how to use .NET, C#, and System.IO to work with directories, paths, files, and the file system.