BitMapImage 转字节和反转

BitMapImage to byte and reverse

这两天我尝试在 WPF 应用程序中管理我的图像,但我有错误和错误错误,...

图像显示在 System.Windows.Control.Image 中。 出于这个原因,我尝试使用变量 BitMapImage。

现在出现错误:"Impossible to access close stream",我找不到解决方案。

我已经为转换创建了两个函数:

public static BitmapImage ConvertToBitMapImage(byte[] bytes)
    {
        if (bytes == null || bytes.Length == 0) return null;
        var image = new BitmapImage();
        using (var mem = new MemoryStream(bytes))
        {
            mem.Position = 0;
            image.BeginInit();
            image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
            image.CacheOption = BitmapCacheOption.OnLoad;
            image.UriSource = null;
            image.StreamSource = mem;
            image.EndInit();
        }
        //image.Freeze();
        return image;
    }

    public static byte[] ImageToByte(BitmapImage imageSource)
    {
        Stream stream = imageSource.StreamSource;
        Byte[] buffer = null;
        if (stream != null && stream.Length > 0)
        {
            using (BinaryReader br = new BinaryReader(stream))
            {
                buffer = br.ReadBytes((Int32)stream.Length);
            }
        }

        return buffer;
    }

在我的对象中我有一个 属性 :

        public BitmapImage MiniatureApp
    {
        get
        {
            if (IMAGES != null)
                _MiniatureApp = Tools.BinaryImageConverter.ConvertToBitMapImage(IMAGES.IMAGE);
            return _MiniatureApp;
        }
        set
        {
            if (this.IMAGES != null)
                this.IMAGES.IMAGE = Tools.BinaryImageConverter.ImageToByte((BitmapImage)value);
            else
            {
                IMAGES img = new IMAGES();
                img.NOM = "";
                img.IMAGE = Tools.BinaryImageConverter.ImageToByte((BitmapImage)value);
                this.IMAGES = img;
            }
            NotifyPropertyChanged();
        }
    }

我主要是这样做的:

FileStream fs = new FileStream(pathImage, FileMode.Open, FileAccess.Read);
byte[] data = new byte[fs.Length];
fs.Read(data, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
VMAppareil.VMApp.CurrentAppareil.MiniatureApp = Tools.BinaryImageConverter.ConvertToBitMapImage(data);

是否存在针对我的问题的解决方案或最佳方法?

高脚杯。

您可以将 ImageToByte 方法替换为以下方法(从 借用)

public static byte[] ImageToByte(BitmapImage imageSource)
{
    var encoder = new JpegBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(imageSource));

    using (var ms = new MemoryStream())
    {
        encoder.Save(ms);
        return ms.ToArray();
    }
}

您之前的解决方案不起作用,因为一旦代码退出单元语句,ConvertToBitMapImage 方法就会关闭您分配给 image.StreamSource 的流。当您调用 ImageToByte 方法作为 MiniatureApp setter 的一部分时,StreamSource 将关闭并且您会收到错误消息。

我不太确定你用 getter-setter 做什么。

为了从文件中获取字节数组,我会这样做:

public static byte[] FileToByteArray(string fileName)
{
    byte[] fileData = null;

    using (FileStream fs = new File.OpenRead(fileName)) 
    { 
        var binaryReader = new BinaryReader(fs); 
        fileData = binaryReader.ReadBytes((int)fs.Length); 
    }
    return fileData;
}

一旦你从那个函数中获得了字节,你就可以使用 ConvertToBitMapImage() 并将其分配给 Image.Source,就像这样:

byte[] bytes = FileToByteArray(fileName);
YourImage.Source = ConvertToBitMapImage(bytes);

我不确定您为什么需要将 BitmapImage 转换为字节,但您应该能够为此直接调用您的函数:

BitmapImage bImg = new BitmapImage();
bImg.Source = ConvertToBitMapImage(bytes);
byte[] bytes = ImageToByte(bImg);  // these should be the same bytes as went in

像这样设置它并逐步执行代码以查看它在哪里遇到问题。当您在 ImageToByte().

中获取字节时,这些字节可能需要编码

而且,您不只是将 byte[] 直接设置为 IMAGESthis.IMAGES.IMAGE,因为那不是 Image 对象,它是一个byte[]。在不知道你的 IMAGES 模型构造及其 属性 类型的情况下,知道什么被设置为什么以及为什么设置有点模糊,但这似乎是一个让你的东西过于复杂的坏主意应该只根据需要进行函数调用,没有 getter-setter 和 IMAGES 模型。