使用icon01.Source
(ImageSource
)經由PngBitmapEncoder
使用SaveFileDialog
private void icon_MouseDown(object sender, MouseButtonEventArgs e)
{
try
{
var saveFileDialog = new SaveFileDialog()
{
Filter = "Image Files (*.bmp, *.png, *.jpg)|*.bmp;*.png;*.jpg"
};
if (saveFileDialog.ShowDialog() == true)
{
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapSource)icon01.Source));
using (FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create))
encoder.Save(stream);
}
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}
創建FileStream
,這裏的示例和XAML是相同的:
<Grid>
<Image x:Name="icon01" MouseDown="icon_MouseDown" Cursor="Hand" Source="FavIcon\01.png" Height="48" Width="48" Margin="10"/>
</Grid>