我正在制作一个程序,将图像从URL下载到bitmapimage并显示它。接下来我尝试使用jpegbitmapencoder将bitmapimage保存到硬盘驱动器。文件已成功创建,但实际的jpeg图像为空或1个黑色像素。
public Guid SavePhoto(string istrImagePath)
{
ImagePath = istrImagePath;
BitmapImage objImage = new BitmapImage(
new Uri(istrImagePath, UriKind.RelativeOrAbsolute));
PictureDisplayed.Source = objImage;
savedCreationObject = objImage;
Guid photoID = System.Guid.NewGuid();
string photolocation = photoID.ToString() + ".jpg"; //file name
FileStream filestream = new FileStream(photolocation, FileMode.Create);
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(objImage));
encoder.Save(filestream);
return photoID;
}
这是保存和显示照片的功能。照片显示正确,但保存后再次显示我得到一个空的jpeg或1个黑色像素。
答案 0 :(得分:30)
从Uri创建BitmapImage时,需要时间来下载图像。
如果您检查以下属性,则该值可能为TRUE
objImage.IsDownloading
因此,您将一个监听器附加到DownloadCompleted事件处理程序并将所有处理移动到该EventHandler。
objImage.DownloadCompleted += objImage_DownloadCompleted;
该处理程序的位置如下:
private void objImage_DownloadCompleted(object sender, EventArgs e)
{
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
Guid photoID = System.Guid.NewGuid();
String photolocation = photoID.ToString() + ".jpg"; //file name
encoder.Frames.Add(BitmapFrame.Create((BitmapImage)sender));
using (var filestream = new FileStream(photolocation, FileMode.Create))
encoder.Save(filestream);
}
您可能还想为DownloadFailed添加另一个EventHandler,以便正常处理任何错误情况。
修改强>
根据Ben的评论添加了完整的示例类:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
SavePhoto("http://www.google.ca/intl/en_com/images/srpr/logo1w.png");
}
public void SavePhoto(string istrImagePath)
{
BitmapImage objImage = new BitmapImage(new Uri(istrImagePath, UriKind.RelativeOrAbsolute));
objImage.DownloadCompleted += objImage_DownloadCompleted;
}
private void objImage_DownloadCompleted(object sender, EventArgs e)
{
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
Guid photoID = System.Guid.NewGuid();
String photolocation = photoID.ToString() + ".jpg"; //file name
encoder.Frames.Add(BitmapFrame.Create((BitmapImage)sender));
using (var filestream = new FileStream(photolocation, FileMode.Create))
encoder.Save(filestream);
}
}
答案 1 :(得分:5)
扩展Chris Baxter的解决方案,此Converter使用本地版本(如果存在),否则下载并保存文件。
using System;
using System.Globalization;
using System.IO;
using System.Windows.Data;
using System.Windows.Media.Imaging;
namespace MyNamespace
{
public sealed class UriToCachedImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var url = value as string;
if (url == null)
return null;
var webUri = new Uri(url, UriKind.Absolute);
var filename = Path.GetFileName(webUri.AbsolutePath);
var localFilePath = Path.Combine("C:\\MyImagesFolder\\", filename);
if (File.Exists(localFilePath))
{
return BitmapFrame.Create(new Uri(localFilePath, UriKind.Absolute));
}
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = webUri;
image.EndInit();
SaveImage(image, localFilePath);
return image;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public void SaveImage(BitmapImage image, string localFilePath)
{
image.DownloadCompleted += (sender, args) =>
{
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapImage) sender));
using (var filestream = new FileStream(localFilePath, FileMode.Create))
{
encoder.Save(filestream);
}
};
}
}
}
并确保您可以访问xaml中的转换器
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:u="clr-namespace:MyNamespace"
d:DesignHeight="500"
d:DesignWidth="420">
<UserControl.Resources>
<ResourceDictionary>
<u:UriToCachedImageConverter x:Key="UrlToCachedImageConverter" />
</ResourceDictionary>
</UserControl.Resources>
</UserControl>
在图像上使用转换器
<Image Source="{Binding URL, Mode=OneWay, Converter={StaticResource UrlToCachedImageConverter}, IsAsync=true}"/>