C#WPF上载图像并将其保存到文件
我有一个C#WPF应用程序,我希望能够上传图像并将其保存到文件夹中。我有下面的代码,但不知道如何保存后,我上传和显示的图像C#WPF上载图像并将其保存到文件,c#,wpf,C#,Wpf,我有一个C#WPF应用程序,我希望能够上传图像并将其保存到文件夹中。我有下面的代码,但不知道如何保存后,我上传和显示的图像 <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Margin="12,12,16,71" Name="imgPhoto" Stretch="Fill" Grid.Column="1"/>
<Button Height="23" HorizontalAlignment="Left" Margin="12,0,0,34" Name="btnLoad" VerticalAlignment="Bottom" Width="75" Grid.Column="1" Click="btnLoad_Click">
_Load
</Button>
<Button Height="23" HorizontalAlignment="Left" Margin="12,0,0,34" Name="btnSave" VerticalAlignment="Bottom" Width="75" Grid.Column="2" Click="btnSave_Click">
_Save
</Button>
</Grid>
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
if (fd.ShowDialog() == true)
{
imgPhoto.Source = new BitmapImage(new Uri(fd.FileName));
Stream stream = File.OpenRead(fd.FileName);
stream = File.OpenRead(fd.FileName);
byte[] binaryImage = new byte[stream.Length];
stream.Read(binaryImage, 0, (int)stream.Length);
}
}
_装载
_拯救
私有无效btnLoad_单击(对象发送方,路由目标)
{
OpenFileDialog fd=新建OpenFileDialog();
如果(fd.ShowDialog()==true)
{
imgPhoto.Source=新的位图图像(新的Uri(fd.FileName));
Stream=File.OpenRead(fd.FileName);
stream=File.OpenRead(fd.FileName);
byte[]binaryImage=新字节[stream.Length];
读取(二进制图像,0,(int)stream.Length);
}
}
显示加载文件中的图像
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
if (fd.ShowDialog() == true)
{
imgPhoto.Source = new BitmapImage(new Uri(fd.FileName));
}
}
要保存加载的文件,只需复制它
var fileNameToSave = DateTime.Now.ToFileNameFormat() + Path.GetExtension(fd.FileName);
var imagePath = Path.Combine("C:\" + fileNameToSave);
File.Copy(fd.FileName, imagePath);
请参阅与之相反的操作,writealBytes
。