BitMapImage字节反转 [英] BitMapImage to byte and reverse

查看:82
本文介绍了BitMapImage字节反转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从两天以来,我尝试在WPF应用程序中管理图像,但是出现错误和错误错误,...

Since two days I try to manage my images in a WPF application but I have errors and errors errors, ...

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

The image is show in a System.Windows.Control.Image. For this reason I try to work with variable BitMapImage.

现在我遇到了错误:无法访问关闭流,我找不到解决方案

Now I have the error : "Impossible to access close stream" and I can not find a solution.

我创建了两个转换函数:

I have created two function for convert :

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

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

In my object I have a property :

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

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

Exist a solution for my problem or exist a best way to do this ?

Gobelet。

推荐答案

我不太确定您正在用吸气剂做什么-setter。

I'm not really sure what you're doing with the getter-setter.

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

For getting a byte array from a file, I would have this:

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,如下所示:

And once you have the bytes from that function, you can use ConvertToBitMapImage() and assign that to the Image.Source, like this:

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

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

I'm not sure why you would need to convert a BitmapImage into bytes, but you should be able to call your function directly for that:

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

设置的字节相同,并逐步执行代码看看它在哪里当您在 ImageToByte()中获取字节时,可能需要对字节进行编码。

Set it up like this and step through the code to see where it snags. It's possible the bytes might need encoding when you grab it in ImageToByte().

但是,您也不需要然后直接将 byte [] 设置为 IMAGES this.IMAGES.IMAGE ,因为它不是 Image 对象,而是一个 byte [] 。不知道您的 IMAGES 模型构造的属性类型是什么样,要知道将什么设置为什么以及为什么设置有点模糊,但这似乎是个坏主意。过度复杂化了您只需要使用函数调用就应该做的事情,而没有使用getter-setter和 IMAGES 模型的方式。

But also, you don't just then set the byte[] directly to the IMAGES or this.IMAGES.IMAGE, because that isn't an Image object, it's a byte[]. Without knowing what your IMAGES model construct looks like with its property types, it's a little vague to know what is being set to what and why, but this seems like a bad idea to over-complicate what you should just do with function calls as it is needed, without a getter-setter and an IMAGES model in the way.

这篇关于BitMapImage字节反转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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