Image.Save 方法

定义

将此图像以指定的格式保存到指定的流中。

重载

Save(String, ImageCodecInfo, EncoderParameters)

使用指定的编码器和图像编码器参数,将该 Image 保存到指定的文件。

Save(Stream, ImageCodecInfo, EncoderParameters)

使用指定的编码器和图像编码器参数,将该图像保存到指定的流。

Save(String, ImageFormat)

将此 Image 以指定格式保存到指定文件。

Save(Stream, ImageFormat)

将此图像以指定的格式保存到指定的流中。

Save(String)

将该 Image 保存到指定的文件或流。

Save(String, ImageCodecInfo, EncoderParameters)

Source:
Image.cs
Source:
Image.cs
Source:
Image.cs

使用指定的编码器和图像编码器参数,将该 Image 保存到指定的文件。

public:
 void Save(System::String ^ filename, System::Drawing::Imaging::ImageCodecInfo ^ encoder, System::Drawing::Imaging::EncoderParameters ^ encoderParams);
public void Save (string filename, System.Drawing.Imaging.ImageCodecInfo encoder, System.Drawing.Imaging.EncoderParameters encoderParams);
public void Save (string filename, System.Drawing.Imaging.ImageCodecInfo encoder, System.Drawing.Imaging.EncoderParameters? encoderParams);
member this.Save : string * System.Drawing.Imaging.ImageCodecInfo * System.Drawing.Imaging.EncoderParameters -> unit
Public Sub Save (filename As String, encoder As ImageCodecInfo, encoderParams As EncoderParameters)

参数

filename
String

字符串,包含要将此 Image 保存到的文件的名称。

encoder
ImageCodecInfo

用于此 ImageCodecInfoImage

encoderParams
EncoderParameters

用于该 EncoderParametersImage

例外

filenameencodernull

该图像以错误的图像格式保存。

- 或 -

该图像被保存到创建该图像的文件。

示例

以下示例从 BMP 文件创建 Bitmap 对象。 代码将位图保存到三个 JPEG 文件中,每个文件具有不同的质量级别。

#using <System.Drawing.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Drawing::Imaging;
static ImageCodecInfo^ GetEncoderInfo( ImageFormat^ format );
int main()
{
   Bitmap^ myBitmap;
   ImageCodecInfo^ myImageCodecInfo;
   Encoder^ myEncoder;
   EncoderParameter^ myEncoderParameter;
   EncoderParameters^ myEncoderParameters;
   
   // Create a Bitmap object based on a BMP file.
   myBitmap = gcnew Bitmap( "Shapes.bmp" );
   
   // Get an ImageCodecInfo object that represents the JPEG codec.
   myImageCodecInfo = GetEncoderInfo( ImageFormat->Jpeg );
   
   // Create an Encoder object based on the GUID
   // for the Quality parameter category.
   myEncoder = Encoder::Quality;
   
   // Create an EncoderParameters object.
   // An EncoderParameters object has an array of EncoderParameter
   // objects. In this case, there is only one
   // EncoderParameter object in the array.
   myEncoderParameters = gcnew EncoderParameters( 1 );
   
   // Save the bitmap as a JPEG file with quality level 25.
   myEncoderParameter = gcnew EncoderParameter( myEncoder,__int64(25) );
   myEncoderParameters->Param[ 0 ] = myEncoderParameter;
   myBitmap->Save( "Shapes025.jpg", myImageCodecInfo, myEncoderParameters );
   
   // Save the bitmap as a JPEG file with quality level 50.
   myEncoderParameter = gcnew EncoderParameter( myEncoder,__int64(50) );
   myEncoderParameters->Param[ 0 ] = myEncoderParameter;
   myBitmap->Save( "Shapes050.jpg", myImageCodecInfo, myEncoderParameters );
   
   // Save the bitmap as a JPEG file with quality level 75.
   myEncoderParameter = gcnew EncoderParameter( myEncoder,__int64(75) );
   myEncoderParameters->Param[ 0 ] = myEncoderParameter;
   myBitmap->Save( "Shapes075.jpg", myImageCodecInfo, myEncoderParameters );
}

static ImageCodecInfo^ GetEncoderInfo( ImageFormat^ format )
{
   int j;
   array<ImageCodecInfo^>^encoders;
   encoders = ImageCodecInfo::GetImageEncoders();
   for ( j = 0; j < encoders->Length; ++j )
   {
      if ( encoders[ j ]->FormatID == format->Guid)
            return encoders[ j ];

   }
   return nullptr;
}
using System;
using System.Drawing;
using System.Drawing.Imaging;
class Example_SetJPEGQuality
{
    public static void Main()
    {
        Bitmap myBitmap;
        ImageCodecInfo myImageCodecInfo;
        Encoder myEncoder;
        EncoderParameter myEncoderParameter;
        EncoderParameters myEncoderParameters;
                     
        // Create a Bitmap object based on a BMP file.
        myBitmap = new Bitmap("Shapes.bmp");
                     
        // Get an ImageCodecInfo object that represents the JPEG codec.
        myImageCodecInfo = GetEncoderInfo("image/jpeg");
                     
        // Create an Encoder object based on the GUID
                     
        // for the Quality parameter category.
        myEncoder = Encoder.Quality;
                     
        // Create an EncoderParameters object.
                     
        // An EncoderParameters object has an array of EncoderParameter
                     
        // objects. In this case, there is only one
                     
        // EncoderParameter object in the array.
        myEncoderParameters = new EncoderParameters(1);
                     
        // Save the bitmap as a JPEG file with quality level 25.
        myEncoderParameter = new EncoderParameter(myEncoder, 25L);
        myEncoderParameters.Param[0] = myEncoderParameter;
        myBitmap.Save("Shapes025.jpg", myImageCodecInfo, myEncoderParameters);
                     
        // Save the bitmap as a JPEG file with quality level 50.
        myEncoderParameter = new EncoderParameter(myEncoder, 50L);
        myEncoderParameters.Param[0] = myEncoderParameter;
        myBitmap.Save("Shapes050.jpg", myImageCodecInfo, myEncoderParameters);
                     
        // Save the bitmap as a JPEG file with quality level 75.
        myEncoderParameter = new EncoderParameter(myEncoder, 75L);
        myEncoderParameters.Param[0] = myEncoderParameter;
        myBitmap.Save("Shapes075.jpg", myImageCodecInfo, myEncoderParameters);
    }
    private static ImageCodecInfo GetEncoderInfo(String mimeType)
    {
        int j;
        ImageCodecInfo[] encoders;
        encoders = ImageCodecInfo.GetImageEncoders();
        for(j = 0; j < encoders.Length; ++j)
        {
            if(encoders[j].MimeType == mimeType)
                return encoders[j];
        }
        return null;
    }
}
Imports System.Drawing
Imports System.Drawing.Imaging


Class Example_SetJPEGQuality

    Public Shared Sub Main()
        Dim myBitmap As Bitmap
        Dim myImageCodecInfo As ImageCodecInfo
        Dim myEncoder As Encoder
        Dim myEncoderParameter As EncoderParameter
        Dim myEncoderParameters As EncoderParameters

        ' Create a Bitmap object based on a BMP file.
        myBitmap = New Bitmap("Shapes.bmp")

        ' Get an ImageCodecInfo object that represents the JPEG codec.
        myImageCodecInfo = GetEncoderInfo(ImageFormat.Jpeg)

        ' Create an Encoder object based on the GUID
        ' for the Quality parameter category.
        myEncoder = Encoder.Quality

        ' Create an EncoderParameters object.
        ' An EncoderParameters object has an array of EncoderParameter
        ' objects. In this case, there is only one
        ' EncoderParameter object in the array.
        myEncoderParameters = New EncoderParameters(1)

        ' Save the bitmap as a JPEG file with quality level 25.
        myEncoderParameter = New EncoderParameter(myEncoder, CType(25L, Int32))
        myEncoderParameters.Param(0) = myEncoderParameter
        myBitmap.Save("Shapes025.jpg", myImageCodecInfo, myEncoderParameters)

        ' Save the bitmap as a JPEG file with quality level 50.
        myEncoderParameter = New EncoderParameter(myEncoder, CType(50L, Int32))
        myEncoderParameters.Param(0) = myEncoderParameter
        myBitmap.Save("Shapes050.jpg", myImageCodecInfo, myEncoderParameters)

        ' Save the bitmap as a JPEG file with quality level 75.
        myEncoderParameter = New EncoderParameter(myEncoder, CType(75L, Int32))
        myEncoderParameters.Param(0) = myEncoderParameter
        myBitmap.Save("Shapes075.jpg", myImageCodecInfo, myEncoderParameters)

    End Sub

    Private Shared Function GetEncoderInfo(ByVal format As ImageFormat) As ImageCodecInfo
        Dim j As Integer
        Dim encoders() As ImageCodecInfo
        encoders = ImageCodecInfo.GetImageEncoders()

        j = 0
        While j < encoders.Length
            If encoders(j).FormatID = format.Guid Then
                Return encoders(j)
            End If
            j += 1
        End While
        Return Nothing

    End Function 'GetEncoderInfo
End Class

注解

不允许将映像保存到从中构造的同一个文件,并引发异常。

另请参阅

适用于

Save(Stream, ImageCodecInfo, EncoderParameters)

Source:
Image.cs
Source:
Image.cs
Source:
Image.cs

使用指定的编码器和图像编码器参数,将该图像保存到指定的流。

public:
 void Save(System::IO::Stream ^ stream, System::Drawing::Imaging::ImageCodecInfo ^ encoder, System::Drawing::Imaging::EncoderParameters ^ encoderParams);
public void Save (System.IO.Stream stream, System.Drawing.Imaging.ImageCodecInfo encoder, System.Drawing.Imaging.EncoderParameters encoderParams);
public void Save (System.IO.Stream stream, System.Drawing.Imaging.ImageCodecInfo encoder, System.Drawing.Imaging.EncoderParameters? encoderParams);
member this.Save : System.IO.Stream * System.Drawing.Imaging.ImageCodecInfo * System.Drawing.Imaging.EncoderParameters -> unit
Public Sub Save (stream As Stream, encoder As ImageCodecInfo, encoderParams As EncoderParameters)

参数

stream
Stream

将在其中保存图像的 Stream

encoder
ImageCodecInfo

用于此 ImageCodecInfoImage

encoderParams
EncoderParameters

一个 EncoderParameters,它指定图像编码器使用的参数。

例外

streamnull

该图像以错误的图像格式保存。

注解

不要将图像保存到用于构造映像的同一流中。 这样做可能会损坏流。

图像必须以零的偏移量保存到流中。 如果在保存图像之前已将任何其他数据写入流,则流中的图像数据将损坏。

适用于

Save(String, ImageFormat)

Source:
Image.cs
Source:
Image.cs
Source:
Image.cs

将此 Image 以指定格式保存到指定文件。

public:
 void Save(System::String ^ filename, System::Drawing::Imaging::ImageFormat ^ format);
public void Save (string filename, System.Drawing.Imaging.ImageFormat format);
member this.Save : string * System.Drawing.Imaging.ImageFormat -> unit
Public Sub Save (filename As String, format As ImageFormat)

参数

filename
String

字符串,包含要将此 Image 保存到的文件的名称。

format
ImageFormat

用于此 ImageFormatImage

例外

filenameformatnull

该图像以错误的图像格式保存。

- 或 -

该图像被保存到创建该图像的文件。

示例

下面的代码示例演示如何从类型构造位图,以及如何使用 Save 方法。 若要运行此示例,请将代码粘贴到 Windows 窗体中。 处理窗体的 Paint 事件,并调用 ConstructFromResourceSaveAsGif 方法,作为 e 传递 PaintEventArgs

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;
        }
    }
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 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

适用于

Save(Stream, ImageFormat)

Source:
Image.cs
Source:
Image.cs
Source:
Image.cs

将此图像以指定的格式保存到指定的流中。

public:
 void Save(System::IO::Stream ^ stream, System::Drawing::Imaging::ImageFormat ^ format);
public void Save (System.IO.Stream stream, System.Drawing.Imaging.ImageFormat format);
member this.Save : System.IO.Stream * System.Drawing.Imaging.ImageFormat -> unit
Public Sub Save (stream As Stream, format As ImageFormat)

参数

stream
Stream

将在其中保存图像的 Stream

format
ImageFormat

ImageFormat,指定保存的图像的格式。

例外

streamformatnull

该图像以错误的图像格式保存。

注解

应避免将图像保存到用于构造它的同一流。 这样做可能会损坏流。

图像必须以零的偏移量保存到流中。 如果在保存图像之前已将任何其他数据写入流,则流中的图像数据将损坏。

适用于

Save(String)

Source:
Image.cs
Source:
Image.cs
Source:
Image.cs

将该 Image 保存到指定的文件或流。

public:
 void Save(System::String ^ filename);
public void Save (string filename);
member this.Save : string -> unit
Public Sub Save (filename As String)

参数

filename
String

字符串,包含要将此 Image 保存到的文件的名称。

例外

filenamenull

该图像以错误的图像格式保存。

- 或 -

该图像被保存到创建该图像的文件。

示例

下面的代码示例演示如何调用 Save 方法。 此示例旨在与 Windows 窗体 一起使用。 Create包含名为 的按钮的Button5窗体。 将代码粘贴到窗体中,并将 方法与按钮的事件 Click 相关联。

private:
   void Button5_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      try
      {
         if ( image1 != nullptr )
         {
            image1->Save( "c:\\myBitmap.bmp" );
            Button5->Text = "Saved file.";
         }
      }
      catch ( Exception^ ) 
      {
         MessageBox::Show( "There was a problem saving the file."
         "Check the file permissions." );
      }
   }
private void Button5_Click(System.Object sender, System.EventArgs e)
{
    try
    {
        if (image1 != null)
        {
            image1.Save("c:\\myBitmap.bmp");
            Button5.Text = "Saved file.";
        }
    }
    catch(Exception)
    {
        MessageBox.Show("There was a problem saving the file." +
            "Check the file permissions.");
    }
}
Private Sub Button5_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button5.Click
    Try
        If (image1 IsNot Nothing) Then
            image1.Save("c:\myBitmap.bmp")
            Button5.Text = "Saved file."
        End If
    Catch ex As Exception
        MessageBox.Show("There was a problem saving the file." _
        & "Check the file permissions.")
    End Try

End Sub

注解

如果图像的文件格式不存在编码器,则使用可移植网络图形 (PNG) 编码器。 使用 Save 方法将图形图像另存为 WINDOWS 图元文件格式 (WMF) 或增强型图元文件格式 (EMF) 文件时,生成的文件将另存为可移植网络图形 (PNG) 文件。 发生此行为的原因是,.NET Framework的 GDI+ 组件没有可用于将文件另存为 .wmf 或.emf文件的编码器。

不允许将映像保存到从中构造的同一个文件,并引发异常。

适用于