问题描述
我正在开发一个程序,该程序将图像从 URL 下载到位图图像并显示它.接下来我尝试使用 jpegbitmapencoder 将位图图像保存到硬盘驱动器.文件创建成功,但实际 jpeg 图像为空或 1 个黑色像素.
I am working on a program that downloads images from a URL to a bitmapimageand displays it. Next I try to save the bitmapimage to the harddrive using jpegbitmapencoder. The file is successfully created but the actual jpeg image is empty or 1 black pixel.
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;
}
这是保存和显示照片的功能.照片显示正确,但再次保存时,我得到一个空的 jpeg 或 1 个黑色像素.
This is the function that saves and displays the photo. The photo is displayed correctly but again when it is saved I get an empty jpeg or 1 black pixel.
推荐答案
从 Uri 创建 BitmapImage 时,下载图像需要时间.
When you create your BitmapImage from a Uri, time is required to download the image.
如果您检查以下属性,则该值可能为 TRUE
If you check the following property, the value will likely be TRUE
objImage.IsDownloading
因此,您可以将侦听器附加到 DownloadCompleted 事件处理程序,并将所有处理移至该事件处理程序.
As such, you much attach a listener to the DownloadCompleted event handler and move all processing to that EventHandler.
objImage.DownloadCompleted += objImage_DownloadCompleted;
该处理程序的外观类似于:
Where that handler will look something like:
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,以便优雅地处理任何错误情况.
You will likely also want to add another EventHandler for DownloadFailed in order to gracefully handle any error cases.
编辑
根据 Ben 的评论添加了完整的示例类:
Added full sample class based on Ben's comment:
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);
}
}
这篇关于将位图图像保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!