次の方法で共有


方法: SaveFileDialog コンポーネントを使用してファイルを保存する

SaveFileDialog コンポーネントを使用すると、ユーザーはファイル システムを参照し、保存するファイルを選択できます。 このダイアログ ボックスは、ユーザーがダイアログ ボックス内で選択したファイルのパスと名前を返します。 ただし、ファイルを実際にディスクに書き込むためのコードを記述する必要があります。

SaveFileDialog コンポーネントを使用してファイルを保存するには

  • [ファイルの保存] ダイアログ ボックスを表示し、ユーザーによって選択されたファイルを保存するメソッドを呼び出します。

    SaveFileDialog コンポーネントの OpenFile メソッドを使用して、ファイルを保存します。 このメソッドにより、書き込み先となる Stream オブジェクトが提供されます。

    次の例では、DialogResult プロパティを使用してファイルの名前を取得し、OpenFile メソッドを使用してファイルを保存します。 OpenFile メソッドによって、ファイルの書き込み先となるストリームが提供されます。

    次のコード例では、Button コントロールに画像が割り当てられています。 ボタンをクリックすると、ファイルの種類として .gif、.jpeg、および .bmp を許可するフィルターを使用して、SaveFileDialog コンポーネントがインスタンス化されます。 [ファイルの保存] ダイアログ ボックスでこれらの種類のファイルが選択されると、ボタンのイメージが保存されます。

    重要

    FileName プロパティを取得または設定するには、アセンブリに、System.Security.Permissions.FileIOPermission クラスによって付与される特権レベルが必要です。 部分的に信頼されたコンテキストで実行している場合、プロセスは、特権がないために例外をスローする可能性があります。 詳しくは、「コード アクセス セキュリティの基礎」をご覧ください。

    この例では、フォームに Button コントロールがあり、その Image プロパティが .gif、.jpeg、または .bmp のファイルの種類に設定されていることを想定しています。

    注意

    FileDialog クラスの FilterIndex プロパティ (継承により SaveFileDialog クラスの一部になっている) では、1 から始まるインデックスを使用します。 これは、ファイルをバイナリ形式ではなくプレーン テキストで保存する場合など、データを特定の形式で保存するコードを記述する場合に重要です。 このプロパティは、次のコード例に示されています。

    Private Sub Button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button2.Click
        ' Displays a SaveFileDialog so the user can save the Image
        ' assigned to Button2.
        Dim saveFileDialog1 As New SaveFileDialog()
        saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"
        saveFileDialog1.Title = "Save an Image File"
        saveFileDialog1.ShowDialog()
    
        ' If the file name is not an empty string open it for saving.
        If saveFileDialog1.FileName <> "" Then
          ' Saves the Image via a FileStream created by the OpenFile method.
          Dim fs As System.IO.FileStream = Ctype _
              (saveFileDialog1.OpenFile(), System.IO.FileStream)
          ' Saves the Image in the appropriate ImageFormat based upon the
          ' file type selected in the dialog box.
          ' NOTE that the FilterIndex property is one-based.
          Select Case saveFileDialog1.FilterIndex
              Case 1
                Me.button2.Image.Save(fs, _
                    System.Drawing.Imaging.ImageFormat.Jpeg)
    
              Case 2
                Me.button2.Image.Save(fs, _
                    System.Drawing.Imaging.ImageFormat.Bmp)
    
              Case 3
                Me.button2.Image.Save(fs, _
                    System.Drawing.Imaging.ImageFormat.Gif)
            End Select
    
            fs.Close()
        End If
    End Sub
    
    private void button2_Click(object sender, System.EventArgs e)
    {
        // Displays a SaveFileDialog so the user can save the Image
        // assigned to Button2.
        SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
        saveFileDialog1.Title = "Save an Image File";
        saveFileDialog1.ShowDialog();
    
        // If the file name is not an empty string open it for saving.
        if(saveFileDialog1.FileName != "")
        {
          // Saves the Image via a FileStream created by the OpenFile method.
          System.IO.FileStream fs =
              (System.IO.FileStream)saveFileDialog1.OpenFile();
          // Saves the Image in the appropriate ImageFormat based upon the
          // File type selected in the dialog box.
          // NOTE that the FilterIndex property is one-based.
          switch(saveFileDialog1.FilterIndex)
          {
              case 1 :
              this.button2.Image.Save(fs,
                System.Drawing.Imaging.ImageFormat.Jpeg);
              break;
    
              case 2 :
              this.button2.Image.Save(fs,
                System.Drawing.Imaging.ImageFormat.Bmp);
              break;
    
              case 3 :
              this.button2.Image.Save(fs,
                System.Drawing.Imaging.ImageFormat.Gif);
              break;
          }
    
        fs.Close();
        }
    }
    
    private:
        System::Void button2_Click(System::Object ^ sender,
          System::EventArgs ^ e)
        {
          // Displays a SaveFileDialog so the user can save the Image
          // assigned to Button2.
          SaveFileDialog ^ saveFileDialog1 = new SaveFileDialog();
          saveFileDialog1->Filter =
              "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
          saveFileDialog1->Title = "Save an Image File";
          saveFileDialog1->ShowDialog();
          // If the file name is not an empty string, open it for saving.
          if(saveFileDialog1->FileName != "")
          {
              // Saves the Image through a FileStream created by
              // the OpenFile method.
              System::IO::FileStream ^ fs =
                safe_cast\<System::IO::FileStream*>(
                saveFileDialog1->OpenFile());
              // Saves the Image in the appropriate ImageFormat based on
              // the file type selected in the dialog box.
              // Note that the FilterIndex property is one based.
              switch(saveFileDialog1->FilterIndex)
              {
                case 1 :
                    this->button2->Image->Save(fs,
                      System::Drawing::Imaging::ImageFormat::Jpeg);
                    break;
                case 2 :
                    this->button2->Image->Save(fs,
                      System::Drawing::Imaging::ImageFormat::Bmp);
                    break;
                case 3 :
                    this->button2->Image->Save(fs,
                      System::Drawing::Imaging::ImageFormat::Gif);
                    break;
              }
          fs->Close();
          }
        }
    

    (Visual C# および Visual C++) フォームのコンストラクターに次のコードを配置して、イベント ハンドラーを登録します。

    this.button2.Click += new System.EventHandler(this.button2_Click);
    
    this->button2->Click += gcnew
        System::EventHandler(this, &Form1::button2_Click);
    

    ファイル ストリームの書き込みの詳細については、BeginWriteWrite を参照してください。

    注意

    RichTextBox コントロールなど、一部のコントロールには、ファイルを保存する機能があります。

関連項目