BitMapImage为字节和反向

时间:2016-03-06 20:06:18

标签: c# arrays wpf byte bitmapimage

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

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

现在我有错误:"无法访问近距离"我无法找到解决方案。

我创建了两个转换函数:

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

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

Gobelet。

2 个答案:

答案 0 :(得分:1)

您可以使用以下内容(借鉴https://stackoverflow.com/a/6597746/5574010)来修复您的解决方案替换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将关闭,您将收到错误。

答案 1 :(得分:0)

我不确定你对吸气装置的做法是什么。

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

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模型。