mp 的图片文件。
''Visual Basic
'' NOTE: You must import the following namespaces:
'' Imports System.IO
'' Imports System.Drawing.Imaging
'' Without these import statements at the beginning of your code,
'' the code example will not function.
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
'' Display an 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
'' Save the Image via a FileStream created by the OpenFile method.
Dim fs As FileStream = CType(saveFileDialog1.OpenFile(), FileStream)
'' Save 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, ImageFormat.Jpeg)
Case 2
Me.button2.Image.Save(fs, ImageFormat.Bmp)
Case 3
Me.button2.Image.Save(fs, ImageFormat.Gif)
End Select
fs.Close()
End If
End Sub
// C#
// NOTE: You must import the following namespaces:
// using System.IO;
// using System.Drawing.Imaging;
// Without these import statements at the beginning of your code,
// the code example will not function.
private void button2_Click(object sender, System.EventArgs e)
{
// Display an 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 != "")
{
// Save the Image via a FileStream created by the OpenFile method.
FileStream fs = (FileStream)saveFileDialog1.OpenFile();
// Save 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,ImageFormat.Jpeg);
break;
case 2 :
this.button2.Image.Save(fs,ImageFormat.Bmp);
break;
case 3 :
this.button2.Image.Save(fs,ImageFormat.Gif);
break;
}
fs.Close();
}
}
关于写入文件流的进一步信息,请参阅 FileStream.BeginWrite 方法。
ColorDialog 组件
此对话框显示颜色列表,并且返回所选的颜色。
与前两种对话框不同,ColorDialog 组件很容易实现其主要功能(挑选颜色)。选取的颜色将成为 Color 属性的设定值。因此,使用颜色就和设定属性值一样简单。在下面的例子中,按钮控制的 Click 事件将会开启一个 ColorDialog 组件。一旦用户选中某种颜色,并且单击了 OK ,按钮的背景将被设成所选的颜色。本例假设存在名为 Button1 的 Button 组件和名为 ColorDialog1 的 ColorDialog 组件。