将位图图像保存到文件

Save BitmapImage to File(将位图图像保存到文件)
本文介绍了将位图图像保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个程序,该程序将图像从 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);
  } 
}

这篇关于将位图图像保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)