Loading WPF Image Control from Byte Array

Hey, I'm trying to load a control Imagefrom an array of bytes, I tried several solutions found on the Internet (especially on this site), but nothing works. My main goal was to get ImageSourcefrom an array of bytes and return it from the converter.

I tried:

BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = new MemoryStream(lBytes);
bi.EndInit();

But this fails:

NotSupportedException

No image component suitable for completing this operation was found.

I also tried to load a first Bitmapand try to get from there ImageSource.

using (MemoryStream lMem = new MemoryStream(lBytes))
{
        TypeConverter tc = TypeDescriptor.GetConverter(typeof(System.Drawing.Bitmap));
        System.Drawing.Bitmap b = (System.Drawing.Bitmap)tc.ConvertFrom(lBytes);
        lResult = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
            b.GetHbitmap(),
            IntPtr.Zero,
            System.Windows.Int32Rect.Empty,
            BitmapSizeOptions.FromEmptyOptions());

        b.Dispose();
}

But this does not work on ConvertFromwith "The parameter is invalid."

All this when loading a valid PNG file in my file system.

Am I running out of ideas, some kind of clue?

Thank.

Edit:

, ...

using (FileStream lFileStream = new FileStream(pFilePath, FileMode.Open))
{
    using (StreamReader lReader = new StreamReader(lFileStream))
    {
        BinaryFormatter bf = new BinaryFormatter();
        using (MemoryStream ms = new MemoryStream())
        {
            string lString = lReader.ReadToEnd();
            bf.Serialize(ms, lString);
            ms.Seek(0, 0);
            lImage = ms.ToArray();
        } 
        lResult = new Graphic(lImage);
    }
}

, :

lImage = File.ReadAllBytes(pFilePath);

.

.

+3
1

, , , , .
. , .

:

System.IO.File.ReadAllBytes(filepath)
0

All Articles