Image.Save Metoda

Definice

Uloží tento obrázek do zadaného datového proudu v zadaném formátu.

Přetížení

Name Description
Save(String, ImageCodecInfo, EncoderParameters)

Uloží ho Image do zadaného souboru se zadanými parametry kodéru a kodéru obrázků.

Save(Stream, ImageCodecInfo, EncoderParameters)

Uloží tento obrázek do zadaného datového proudu se zadanými parametry kodéru a kodéru obrázků.

Save(String, ImageFormat)

Uloží ho Image do zadaného souboru v zadaném formátu.

Save(Stream, ImageFormat)

Uloží tento obrázek do zadaného datového proudu v zadaném formátu.

Save(String)

Uloží ho Image do zadaného souboru nebo datového proudu.

Save(String, ImageCodecInfo, EncoderParameters)

Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs

Uloží ho Image do zadaného souboru se zadanými parametry kodéru a kodéru obrázků.

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)

Parametry

filename
String

Řetězec, který obsahuje název souboru, do kterého chcete tento Imagesoubor uložit .

encoder
ImageCodecInfo

Pro ImageCodecInfo toto Image.

encoderParams
EncoderParameters

Použití EncoderParameters pro toto Image.

Výjimky

filename nebo encoder je null.

Obrázek byl uložen s nesprávným formátem obrázku.

nebo

Obrázek byl uložen do stejného souboru, ze kterého byl vytvořen.

Příklady

Následující příklad vytvoří Bitmap objekt ze souboru BMP. Kód uloží rastrový obrázek do tří souborů JPEG, z nichž každý má jinou úroveň kvality.

#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

Poznámky

Uložení image do stejného souboru, ze něhož byl vytvořen, není povoleno a vyvolá výjimku.

Viz také

Platí pro

Save(Stream, ImageCodecInfo, EncoderParameters)

Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs

Uloží tento obrázek do zadaného datového proudu se zadanými parametry kodéru a kodéru obrázků.

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)

Parametry

stream
Stream

Místo Stream , kam se obrázek uloží.

encoder
ImageCodecInfo

Pro ImageCodecInfo toto Image.

encoderParams
EncoderParameters

Určuje EncoderParameters parametry používané kodérem obrázků.

Výjimky

stream je null.

Obrázek byl uložen s nesprávným formátem obrázku.

Poznámky

Neukládejte obrázek do stejného datového proudu, který byl použit k vytvoření image. To může způsobit poškození datového proudu.

Obrázek musí být uložen do datového proudu na posunu nuly. Pokud se do datového proudu před uložením image zapíšou nějaká další data, data obrázků v datovém proudu budou poškozena.

Platí pro

Save(String, ImageFormat)

Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs

Uloží ho Image do zadaného souboru v zadaném formátu.

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)

Parametry

filename
String

Řetězec, který obsahuje název souboru, do kterého chcete tento Imagesoubor uložit .

format
ImageFormat

Pro ImageFormat toto Image.

Výjimky

filename nebo format je null.

Obrázek byl uložen s nesprávným formátem obrázku.

nebo

Obrázek byl uložen do stejného souboru, ze kterého byl vytvořen.

Příklady

Následující příklad kódu ukazuje, jak vytvořit bitmapu z typu a jak použít metodu Save . Pokud chcete tento příklad spustit, vložte kód do formuláře Windows. Zpracování události formuláře Paint a volání ConstructFromResourceSaveAsGif metody předání e jako 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

Platí pro

Save(Stream, ImageFormat)

Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs

Uloží tento obrázek do zadaného datového proudu v zadaném formátu.

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)

Parametry

stream
Stream

Místo Stream , kam se obrázek uloží.

format
ImageFormat

Určuje ImageFormat formát uloženého obrázku.

Výjimky

stream nebo format je null.

Obrázek byl uložen s nesprávným formátem obrázku.

Poznámky

Měli byste se vyhnout uložení image do stejného datového proudu, který byl použit k jeho vytvoření. To může způsobit poškození datového proudu.

Obrázek musí být uložen do datového proudu na posunu nuly. Pokud se do datového proudu před uložením image zapíšou nějaká další data, data obrázků v datovém proudu budou poškozena.

Platí pro

Save(String)

Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs
Zdroj:
Image.cs

Uloží ho Image do zadaného souboru nebo datového proudu.

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

Parametry

filename
String

Řetězec, který obsahuje název souboru, do kterého chcete tento Imagesoubor uložit .

Výjimky

filename je null.

Obrázek byl uložen s nesprávným formátem obrázku.

nebo

Obrázek byl uložen do stejného souboru, ze kterého byl vytvořen.

Příklady

Následující příklad kódu ukazuje, jak volat metodu Save . Tento příklad je navržený tak, aby se používal s Windows Forms. Vytvořte formulář, který obsahuje tlačítko s názvem Button5. Vložte kód do formuláře a přidružte metodu k události tlačítka 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

Poznámky

Pokud pro formát souboru obrázku neexistuje žádný kodér, použije se kodér PNG (Portable Network Graphics). Pokud použijete metodu Save k uložení grafického obrázku ve formátu WMF (Windows Metafile Format) nebo souboru EMF (Enhanced Metafile Format), výsledný soubor se uloží jako soubor PNG (Portable Network Graphics). K tomuto chování dochází, protože komponenta GDI+ rozhraní .NET Framework nemá kodér, který můžete použít k ukládání souborů jako .wmf nebo .emf souborů.

Uložení image do stejného souboru, ze něhož byl vytvořen, není povoleno a vyvolá výjimku.

Platí pro