首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将BitmapImage保存到文件

将BitmapImage保存到文件
EN

Stack Overflow用户
提问于 2010-11-12 11:24:04
回答 1查看 72.6K关注 0票数 22

我正在开发一个从URL下载图像到位图图像并显示它的程序。接下来,我尝试使用jpegbitmapencoder将位图图像保存到硬盘驱动器中。文件已成功创建,但实际的jpeg图像为空或1个黑色像素。

代码语言:javascript
复制
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个黑色像素。

EN

回答 1

Stack Overflow用户

发布于 2015-04-03 09:43:00

在Chris Baxter的解决方案上扩展,此Converter使用本地版本(如果存在),否则将下载它并保存文件。

代码语言:javascript
复制
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中的转换器

代码语言:javascript
复制
<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>

并在图像上使用该转换器

代码语言:javascript
复制
<Image Source="{Binding URL, Mode=OneWay, Converter={StaticResource UrlToCachedImageConverter}, IsAsync=true}"/>
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4161359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档