Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将位图图像转换为字节并反转_C#_Arrays_Wpf_Byte_Bitmapimage - Fatal编程技术网

C# 将位图图像转换为字节并反转

C# 将位图图像转换为字节并反转,c#,arrays,wpf,byte,bitmapimage,C#,Arrays,Wpf,Byte,Bitmapimage,自从两天以来,我试图在WPF应用程序中管理我的图像,但我有错误和错误 图像显示在System.Windows.Control.image中。 因此,我尝试使用变量BitMapImage 现在我有一个错误:“不可能访问封闭流”,我找不到解决方案 我为convert创建了两个函数: public static BitmapImage ConvertToBitMapImage(byte[] bytes) { if (bytes == null || bytes.Length =

自从两天以来,我试图在WPF应用程序中管理我的图像,但我有错误和错误

图像显示在System.Windows.Control.image中。 因此,我尝试使用变量BitMapImage

现在我有一个错误:“不可能访问封闭流”,我找不到解决方案

我为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;
    }
在我的对象中,我有一个属性:

        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。

您可以修复解决方案,使用以下方法替换ImageToByte方法(借用自)


您以前的解决方案不起作用,因为一旦代码退出unit语句,ConvertToBitMapImage方法就会关闭分配给image.StreamSource的流。当您将ImageToByte方法作为MinimatureApp 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()
并将其分配给图像。源代码如下:

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()
中获取字节时,可能需要对其进行编码


而且,您不需要直接将
字节[]
设置为
图像
this.IMAGES.IMAGE
,因为这不是
图像
对象,而是
字节[]
。在不知道您的
图像
模型构造及其属性类型的情况下,知道将什么设置为什么以及为什么设置是有点模糊的,但这似乎是一个不好的主意,因为它会使您应该在需要时对函数调用做的事情过于复杂,如果没有getter setter和
IMAGES
模型,您的ImageToByte方法就不正常,因为尝试访问StreamSource时它是关闭的。这就是BitmapCacheOption.OnLoad所做的。此答案解释了如何将BitmapImage保存到字节数组:是否有任何理由将图像存储为字节数组(在images.IMAGE中)?我的图像是从数据库加载的。我没有其他选择从bytesOk加载我的图像。您的main似乎从磁盘上的文件加载字节,但这可能只是一个示例。这是我的示例,我第一次从磁盘加载,并将其转换为字节,然后尝试从字节加载,但它不起作用:用我认为是您问题的解决方案来填充我的响应。
BitmapImage bImg = new BitmapImage();
bImg.Source = ConvertToBitMapImage(bytes);
byte[] bytes = ImageToByte(bImg);  // these should be the same bytes as went in