Ben*_*rth 22 c# save bitmapimage
我正在开发一个程序,将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
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(objImage));
using (FileStream filestream = new FileStream(photolocation, FileMode.Create))
{
encoder.Save(filestream);
}
return photoID;
}
Run Code Online (Sandbox Code Playgroud)
这是保存和显示照片的功能.照片显示正确,但保存时再次显示我得到一个空的jpeg或1个黑色像素.
Chr*_*ter 32
从Uri创建BitmapImage时,需要时间来下载图像.
如果检查以下属性,则该值可能为TRUE
objImage.IsDownloading
Run Code Online (Sandbox Code Playgroud)
因此,您可以将一个侦听器附加到DownloadCompleted事件处理程序,并将所有处理移动到该EventHandler.
objImage.DownloadCompleted += objImage_DownloadCompleted;
Run Code Online (Sandbox Code Playgroud)
该处理程序的位置如下:
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);
}
Run Code Online (Sandbox Code Playgroud)
您可能还想为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);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 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);
}
};
}
}
}
Run Code Online (Sandbox Code Playgroud)
并确保您可以访问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>
Run Code Online (Sandbox Code Playgroud)
并在图像上使用转换器
<Image Source="{Binding URL, Mode=OneWay, Converter={StaticResource UrlToCachedImageConverter}, IsAsync=true}"/>
Run Code Online (Sandbox Code Playgroud)