Image.Saveとは? わかりやすく解説

Image.Save メソッド (String, ImageFormat)

この Image を、指定した形式で指定したファイルに保存します。

名前空間: System.Drawing
アセンブリ: System.Drawing (system.drawing.dll 内)
構文構文

例外例外
例外の種類条件

ArgumentNullException

filename または format が null 参照 (Visual Basic では Nothing) です。

ExternalException

このイメージは、誤ったイメージ形式で保存されています。

または

このイメージは、イメージの作成元と同じファイルに保存されています。

使用例使用例

ビットマップを型から作成する方法、および Save メソッドを使用する方法を次のコード例に示します。この例を実行するには、コードを Windows フォームに貼り付けます。フォームの Paint イベントを処理し、ConstructFromResourceSaveAsGif メソッドを呼び出し、e を 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;
        }
    }
private void ConstructFromResourceSaveAsGif(PaintEventArgs
 e)
{
    // Construct a bitmap from the button image resource.
    Bitmap bmp1 = new Bitmap(Button.class.ToType(),
 "Button.bmp");
    // Save the image as a GIF.
    bmp1.Save("c:\\button.gif", System.Drawing.Imaging.ImageFormat.get_Gif());
    // Construct a new image from the GIF file.
    Bitmap bmp2 = new Bitmap("c:\\button.gif");
    // Draw the two images.
    e.get_Graphics().DrawImage(bmp1, new Point(10, 10));
    e.get_Graphics().DrawImage(bmp2, new Point(10, 40));
    // Dispose of the image files.
    bmp1.Dispose();
    bmp2.Dispose();
} //ConstructFromResourceSaveAsGif
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Image.Save メソッド (String)

この Image を指定したファイルまたはストリームに保存します。

名前空間: System.Drawing
アセンブリ: System.Drawing (system.drawing.dll 内)
構文構文

例外例外
例外の種類条件

ArgumentNullException

filename is null.

ExternalException

このイメージは、誤ったイメージ形式で保存されています。

または

このイメージは、イメージの作成元と同じファイルに保存されています。

解説解説
使用例使用例

Save メソッドを呼び出す方法を次のコード例に示します。この例は、Windows フォームでの使用を意図してデザインされています。Button1 および Button5 という名前の 2 つのボタンを格納するフォームを作成します。コードをフォームに貼り付け、対応するボタンの Click イベントに各メソッドを関連付けます。

Dim image1 As Bitmap

Private Sub Button1_Click(ByVal
 sender As System.Object, _
    ByVal e As System.EventArgs) Handles
 Button1.Click

    Try
        ' Retrieve the image.
        image1 = New Bitmap( _
            "C:\Documents and Settings\All Users\Documents\My
 Music\music.bmp", _
            True)

        Dim x, y As Integer

        ' Loop through the images pixels to reset color.
        For x = 0 To image1.Width - 1
            For y = 0 To image1.Height - 1
                Dim pixelColor As Color = image1.GetPixel(x,
 y)
                Dim newColor As Color = _
                    Color.FromArgb(pixelColor.R, 0, 0)
                image1.SetPixel(x, y, newColor)
            Next
        Next

        ' Set the PictureBox to display the image.
        PictureBox1.Image = image1

        ' Display the pixel format in Label1.
        Label1.Text = "Pixel format: " + image1.PixelFormat.ToString()

    Catch ex As ArgumentException
        MessageBox.Show("There was an error." _
            & "Check the path to the image file.")
    End Try
End Sub
Bitmap image1;

private void Button1_Click(System.Object sender,
 System.EventArgs e)
{

    try
    {
        // Retrieve the image.
        image1 = new Bitmap(@"C:\Documents and Settings\All
 Users\" 
            + @"Documents\My Music\music.bmp", true);

        int x, y;

        // Loop through the images pixels to reset color.
        for(x=0; x<image1.Width; x++)
        {
            for(y=0; y<image1.Height; y++)
            {
                Color pixelColor = image1.GetPixel(x, y);
                Color newColor = Color.FromArgb(pixelColor.R, 0, 0);
                image1.SetPixel(x, y, newColor);
            }
        }

        // Set the PictureBox to display the image.
        PictureBox1.Image = image1;

        // Display the pixel format in Label1.
        Label1.Text = "Pixel format: "+image1.PixelFormat.ToString();

    }
    catch(ArgumentException)
    {
        MessageBox.Show("There was an error." +
            "Check the path to the image file.");
    }
}
private:
   Bitmap^ image1;
   void Button1_Click( System::Object^ /*sender*/, System::EventArgs^
 /*e*/ )
   {
      try
      {
         
         // Retrieve the image.
         image1 = gcnew Bitmap( "C:\\Documents and Settings\\All Users\\"
         "Documents\\My Music\\music.bmp",true );
         int x;
         int y;
         
         // Loop through the images pixels to reset color.
         for ( x = 0; x < image1->Width; x++ )
         {
            for ( y = 0; y < image1->Height; y++ )
            {
               Color pixelColor = image1->GetPixel( x, y );
               Color newColor = Color::FromArgb( pixelColor.R, 0, 0 );
               image1->SetPixel( x, y, newColor );

            }

         }
         
         // Set the PictureBox to display the image.
         PictureBox1->Image = image1;
         
         // Display the pixel format in Label1.
         Label1->Text = String::Format( "Pixel format: {0}", image1->PixelFormat
 );
      }
      catch ( ArgumentException^ ) 
      {
         MessageBox::Show( "There was an error."
         "Check the path to the image file." );
      }

   }
private Bitmap image1;

private void button1_Click(Object sender, System.EventArgs
 e)
{
    try {
        // Retrieve the image.
        image1 = new Bitmap("C:\\Documents and Settings\\All
 Users\\"  
            + "Documents\\My Music\\music.bmp", true);

        int x, y;

        // Loop through the images pixels to reset color.
        for (x = 0; x < image1.get_Width(); x++) {
            for (y = 0; y < image1.get_Height(); y++) {
                Color pixelColor = image1.GetPixel(x, y);
                Color newColor = Color.FromArgb(pixelColor.get_R(), 0, 0);

                image1.SetPixel(x, y, newColor);
            }
        }

        // Set the PictureBox to display the image.
        pictureBox1.set_Image(image1);

        // Display the pixel format in label1.
        label1.set_Text("Pixel format: " 
            + image1.get_PixelFormat().ToString());
    }
    catch (ArgumentException exp) {
        MessageBox.Show(("There was an error."  
            + "Check the path to the image file."));
    }
} //button1_Click
Private Sub Button5_Click(ByVal
 sender As System.Object, _
    ByVal e As System.EventArgs) Handles
 Button5.Click
    Try
        If Not (image1 Is
 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
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:
   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(Object sender, System.EventArgs
 e)
{
    try {
        if (image1 != null) {
            image1.Save("c:\\myBitmap.bmp");
            button5.set_Text("Saved file.");
        }
    }
    catch (System.Exception exp) {
        MessageBox.Show(("There was a problem saving the file."  
            + "Check the file permissions."));
    }
} //button5_Click
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Image.Save メソッド (Stream, ImageFormat)

このイメージを、指定した形式で指定したストリームに保存します。

名前空間: System.Drawing
アセンブリ: System.Drawing (system.drawing.dll 内)
構文構文

例外例外
例外の種類条件

ArgumentNullException

stream または format が null 参照 (Visual Basic では Nothing) です。

ExternalException

このイメージは、誤ったイメージ形式で保存されています。

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Image.Save メソッド (Stream, ImageCodecInfo, EncoderParameters)

指定したエンコーダ パラメータおよびイメージ エンコーダ パラメータを使用して、このイメージを指定したストリームに保存します。

名前空間: System.Drawing
アセンブリ: System.Drawing (system.drawing.dll 内)
構文構文

Public Sub Save ( _
    stream As Stream, _
    encoder As ImageCodecInfo, _
    encoderParams As EncoderParameters _
)
Dim instance As Image
Dim stream As Stream
Dim encoder As ImageCodecInfo
Dim encoderParams As EncoderParameters

instance.Save(stream, encoder, encoderParams)
public void Save (
    Stream stream,
    ImageCodecInfo encoder,
    EncoderParameters encoderParams
)
public:
void Save (
    Stream^ stream, 
    ImageCodecInfo^ encoder, 
    EncoderParameters^ encoderParams
)
public void Save (
    Stream stream, 
    ImageCodecInfo encoder, 
    EncoderParameters encoderParams
)
public function Save (
    stream : Stream, 
    encoder : ImageCodecInfo, 
    encoderParams : EncoderParameters
)

パラメータ

stream

イメージが保存される Stream。

encoder

この Image の ImageCodecInfo。

encoderParams

イメージ エンコーダによって使用されるパラメータを指定する EncoderParameters。

例外例外
例外の種類条件

ArgumentNullException

stream が null 参照 (Visual Basic では Nothing) です。

ExternalException

このイメージは、誤ったイメージ形式で保存されています。

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Image.Save メソッド


Image.Save メソッド (String, ImageCodecInfo, EncoderParameters)

指定したエンコーダ パラメータおよびイメージ エンコーダ パラメータを使用して、指定したファイルにこの Image を保存します。

名前空間: System.Drawing
アセンブリ: System.Drawing (system.drawing.dll 内)
構文構文

Public Sub Save ( _
    filename As String, _
    encoder As ImageCodecInfo, _
    encoderParams As EncoderParameters _
)
Dim instance As Image
Dim filename As String
Dim encoder As ImageCodecInfo
Dim encoderParams As EncoderParameters

instance.Save(filename, encoder, encoderParams)
public void Save (
    string filename,
    ImageCodecInfo encoder,
    EncoderParameters encoderParams
)
public:
void Save (
    String^ filename, 
    ImageCodecInfo^ encoder, 
    EncoderParameters^ encoderParams
)
public void Save (
    String filename, 
    ImageCodecInfo encoder, 
    EncoderParameters encoderParams
)
public function Save (
    filename : String, 
    encoder : ImageCodecInfo, 
    encoderParams : EncoderParameters
)

パラメータ

filename

この Image の保存先ファイルの名前を格納している文字列。

encoder

この Image の ImageCodecInfo。

encoderParams

この Image に使用する EncoderParameters。

例外例外
例外の種類条件

ArgumentNullException

filename または encoder が null 参照 (Visual Basic では Nothing) です。

ExternalException

このイメージは、誤ったイメージ形式で保存されています。

または

このイメージは、イメージの作成元と同じファイルに保存されています。

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「Image.Save」の関連用語

Image.Saveのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Image.Saveのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS