Image.Save Image.Save Image.Save Image.Save Method

定义

将此图像以指定的格式保存到指定的流中。Saves this image to the specified stream in the specified format.

重载

Save(String, ImageCodecInfo, EncoderParameters) Save(String, ImageCodecInfo, EncoderParameters) Save(String, ImageCodecInfo, EncoderParameters) Save(String, ImageCodecInfo, EncoderParameters)

使用指定的编码器和图像编码器参数,将该 Image 保存到指定的文件。Saves this Image to the specified file, with the specified encoder and image-encoder parameters.

Save(Stream, ImageCodecInfo, EncoderParameters) Save(Stream, ImageCodecInfo, EncoderParameters) Save(Stream, ImageCodecInfo, EncoderParameters)

使用指定的编码器和图像编码器参数,将该图像保存到指定的流。Saves this image to the specified stream, with the specified encoder and image encoder parameters.

Save(String, ImageFormat) Save(String, ImageFormat) Save(String, ImageFormat) Save(String, ImageFormat)

将此 Image 以指定格式保存到指定文件。Saves this Image to the specified file in the specified format.

Save(Stream, ImageFormat) Save(Stream, ImageFormat) Save(Stream, ImageFormat)

将此图像以指定的格式保存到指定的流中。Saves this image to the specified stream in the specified format.

Save(String) Save(String) Save(String) Save(String)

将该 Image 保存到指定的文件或流。Saves this Image to the specified file or stream.

Save(String, ImageCodecInfo, EncoderParameters) Save(String, ImageCodecInfo, EncoderParameters) Save(String, ImageCodecInfo, EncoderParameters) Save(String, ImageCodecInfo, EncoderParameters)

使用指定的编码器和图像编码器参数,将该 Image 保存到指定的文件。Saves this Image to the specified file, with the specified encoder and image-encoder parameters.

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);
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 String String String

字符串,包含要将此 Image 保存到的文件的名称。A string that contains the name of the file to which to save this Image.

异常

filenameencodernull.filename or encoder is null.

该图像以错误的图像格式保存。The image was saved with the wrong image format.

-or-

该图像被保存到创建该图像的文件。The image was saved to the same file it was created from.

示例

下面的示例从 BMP Bitmap文件创建对象。The following example creates a Bitmap object from a BMP file. 该代码将位图保存为三个 JPEG 文件,每个文件都有不同的质量级别。The code saves the bitmap to three JPEG files, each with a different quality level.

#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

注解

不允许将图像保存到从中构建它的文件,并且会引发异常。Saving the image to the same file it was constructed from is not allowed and throws an exception.

另请参阅

Save(Stream, ImageCodecInfo, EncoderParameters) Save(Stream, ImageCodecInfo, EncoderParameters) Save(Stream, ImageCodecInfo, EncoderParameters)

使用指定的编码器和图像编码器参数,将该图像保存到指定的流。Saves this image to the specified stream, with the specified encoder and image encoder parameters.

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

参数

stream
Stream Stream Stream Stream

将在其中保存图像的 StreamThe Stream where the image will be saved.

encoderParams
EncoderParameters EncoderParameters EncoderParameters EncoderParameters

一个 EncoderParameters,它指定图像编码器使用的参数。An EncoderParameters that specifies parameters used by the image encoder.

异常

该图像以错误的图像格式保存。The image was saved with the wrong image format.

注解

不要将图像保存到用于构造映像的相同流中。Do not save an image to the same stream that was used to construct the image. 这样做可能会损坏流。Doing so might damage the stream.

必须将该图像保存到流中偏移量为零的位置。The image must be saved to the stream at an offset of zero. 如果在保存映像之前已向流中写入任何其他数据,则流中的图像数据将损坏。If any additional data has been written to the stream before saving the image, the image data in the stream will be corrupted.

Save(String, ImageFormat) Save(String, ImageFormat) Save(String, ImageFormat) Save(String, ImageFormat)

将此 Image 以指定格式保存到指定文件。Saves this Image to the specified file in the specified format.

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 String String String

字符串,包含要将此 Image 保存到的文件的名称。A string that contains the name of the file to which to save this Image.

异常

filenameformatnull.filename or format is null.

该图像以错误的图像格式保存。The image was saved with the wrong image format.

-or-

该图像被保存到创建该图像的文件。The image was saved to the same file it was created from.

示例

下面的代码示例演示如何从类型构造位图,以及如何使用Save方法。The following code example demonstrates how to construct a bitmap from a type, and how to use the Save method. 若要运行此示例,请将代码粘贴到 Windows 窗体中。To run this example, paste the code into a Windows Form. 处理窗体的Paint事件,并调用方法ConstructFromResourceSaveAsGif ,将传递ePaintEventArgsHandle the form's Paint event, and call the ConstructFromResourceSaveAsGif method, passing e as 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) Save(Stream, ImageFormat) Save(Stream, ImageFormat)

将此图像以指定的格式保存到指定的流中。Saves this image to the specified stream in the specified format.

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

参数

stream
Stream Stream Stream Stream

将在其中保存图像的 StreamThe Stream where the image will be saved.

format
ImageFormat ImageFormat ImageFormat ImageFormat

ImageFormat,指定保存的图像的格式。An ImageFormat that specifies the format of the saved image.

异常

streamformatnullstream or format is null.

该图像以错误的图像格式保存The image was saved with the wrong image format

注解

应避免将图像保存到用于构造它的同一个流中。You should avoid saving an image to the same stream that was used to construct it. 这样做可能会损坏流。Doing so might damage the stream.

必须将该图像保存到流中偏移量为零的位置。The image must be saved to the stream at an offset of zero. 如果在保存映像之前已向流中写入任何其他数据,则流中的图像数据将损坏。If any additional data has been written to the stream before saving the image, the image data in the stream will be corrupted.

Save(String) Save(String) Save(String) Save(String)

将该 Image 保存到指定的文件或流。Saves this Image to the specified file or stream.

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

参数

filename
String String String String

字符串,包含要将此 Image 保存到的文件的名称。A string that contains the name of the file to which to save this Image.

异常

该图像以错误的图像格式保存。The image was saved with the wrong image format.

- 或 --or-

该图像被保存到创建该图像的文件。The image was saved to the same file it was created from.

示例

下面的代码示例演示如何调用Save方法。The following code example demonstrates how to call the Save method. 此示例旨在与 Windows 窗体一起使用。This example is designed to be used with Windows Forms. 创建一个包含名为Button5的按钮的窗体。Create a form that contains a button named Button5. 将代码粘贴到窗体中,并将该方法与按钮的Click事件关联。Paste the code into the form, and associate the method with the button's Click event.

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)编码器。If no encoder exists for the file format of the image, the Portable Network Graphics (PNG) encoder is used. 当你使用Save方法将图形图像保存为 Windows 图元文件格式(WMF)或增强型图元文件格式(EMF)文件时,生成的文件将另存为可移植网络图形(PNG)文件。When you use the Save method to save a graphic image as a Windows Metafile Format (WMF) or Enhanced Metafile Format (EMF) file, the resulting file is saved as a Portable Network Graphics (PNG) file. 之所以发生此行为, GDI+GDI+是因为的.NET Framework.NET Framework组件没有可用于将文件另存为 .wmf 或 .emf 文件的编码器。This behavior occurs because the GDI+GDI+ component of the .NET Framework.NET Framework does not have an encoder that you can use to save files as .wmf or .emf files.

不允许将图像保存到从中构建它的文件,并且会引发异常。Saving the image to the same file it was constructed from is not allowed and throws an exception.

适用于