保存的BitmapImage到文件 [英] Save BitmapImage to File

查看:271
本文介绍了保存的BitmapImage到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,从一个URL下载图像到bitmapimageand显示它的程序。接下来,我尝试将保存的BitmapImage使用jpegbitmapen codeR的硬盘。成功创建文件,但实际的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
    FileStream filestream = new FileStream(photolocation, FileMode.Create);
    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(objImage));
    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,以便适当地处理任何错误情况。

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);
  } 
}

这篇关于保存的BitmapImage到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆