Saves this Image to the specified file in the specified format.
Namespace:
System.Drawing
Assembly:
System.Drawing (in System.Drawing.dll)
Visual Basic (Declaration)
Public Sub Save ( _
filename As String, _
format As ImageFormat _
)
Dim instance As Image
Dim filename As String
Dim format As ImageFormat
instance.Save(filename, format)
public void Save(
string filename,
ImageFormat format
)
public:
void Save(
String^ filename,
ImageFormat^ format
)
public function Save(
filename : String,
format : ImageFormat
)
| Exception | Condition |
|---|
| ArgumentNullException |
filename or format is null. |
| ExternalException | The image was saved with the wrong image format. -or- The image was saved to the same file it was created from. |
The following code example demonstrates how to construct a bitmap from a type, and how to use the Save method. To run this example, paste the code into a Windows Form. Handle the form’s Paint event, and call the ConstructFromResourceSaveAsGif method, passing e as PaintEventArgs
Private Sub ConstructFromResourceSaveAsGif(ByVal e As PaintEventArgs)
' Construct a bitmap from the button image resource.
Dim bmp1 As New Bitmap(GetType(Button), "Button.bmp")
' Save the image as a GIF.
bmp1.Save("c:\button.gif", System.Drawing.Imaging.ImageFormat.Gif)
' Construct a new image from the GIF file.
Dim bmp2 As New Bitmap("c:\button.gif")
' Draw the two images.
e.Graphics.DrawImage(bmp1, New Point(10, 10))
e.Graphics.DrawImage(bmp2, New Point(10, 40))
' Dispose of the image files.
bmp1.Dispose()
bmp2.Dispose()
End Sub
private void ConstructFromResourceSaveAsGif(PaintEventArgs e)
{
// Construct a bitmap from the button image resource.
Bitmap bmp1 = new Bitmap(typeof(Button), "Button.bmp");
// Save the image as a GIF.
bmp1.Save("c:\\button.gif", System.Drawing.Imaging.ImageFormat.Gif);
// Construct a new image from the GIF file.
Bitmap bmp2 = new Bitmap("c:\\button.gif");
// Draw the two images.
e.Graphics.DrawImage(bmp1, new Point(10, 10));
e.Graphics.DrawImage(bmp2, new Point(10, 40));
// Dispose of the image files.
bmp1.Dispose();
bmp2.Dispose();
}
private:
void ConstructFromResourceSaveAsGif(PaintEventArgs^ e)
{
// Construct a bitmap from the button image resource.
Bitmap^ bmp1 = gcnew Bitmap(Button::typeid, "Button.bmp");
String^ savePath =
Environment::GetEnvironmentVariable("TEMP") + "\\Button.bmp";
try
{
// Save the image as a GIF.
bmp1->Save(savePath, System::Drawing::Imaging::ImageFormat::Gif);
}
catch (IOException^)
{
// Carry on regardless
}
// Construct a new image from the GIF file.
Bitmap^ bmp2 = nullptr;
if (File::Exists(savePath))
{
bmp2 = gcnew Bitmap(savePath);
}
// Draw the two images.
e->Graphics->DrawImage(bmp1, Point(10, 10));
// If bmp1 did not save to disk, bmp2 may be null
if (bmp2 != nullptr)
{
e->Graphics->DrawImage(bmp2, Point(10, 40));
}
// Dispose of the image files.
delete bmp1;
if (bmp2 != nullptr)
{
delete bmp2;
}
}
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.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
.NET Compact Framework
Supported in: 3.5, 2.0
Reference