WPF convert byte array from database to Image control source

0

I have a byte array stored in an entity framework database. The list of objects is set to a ListView source. The list view has this template as its item template:

<DataTemplate x:Key="ItemContentTemplate">
    <Grid Height="auto" Margin="0,0,0,10">
        <Image Width="auto" Height="auto" Source="{Binding InitialImage, Converter={StaticResource ResourceKey=ImageSourceConverter}}" Margin="10"/>
    </Grid>
</DataTemplate>

I'm using this as a converter but I get this exception on EndInit: "No imaging component suitable to complete this operation was found." inner exception:{"Exception from HRESULT: 0x88982F50"} System.Runtime.InteropServices.COMException

public class ImageSourceConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo cultureInfo)
    {
        if (value == null)
        {
            return new BitmapImage();
        }

        try
        {
            MemoryStream strmImg = new MemoryStream((byte[])value);
            BitmapImage myBitmapImage = new BitmapImage();
            myBitmapImage.BeginInit();
            myBitmapImage.StreamSource = strmImg;
            myBitmapImage.EndInit();
            return myBitmapImage;
        }
        catch (Exception ex)
        {
            string message = ex.Message;
        }

        return new BitmapImage();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

I'm not really sure what this means...

EDIT: Ok here some more info. So I receive the raw data from a usb camera via directshow in the form of an Intptr from managed code. Then I copy it from a the pointer to a write able bitmap:

public static void CopyTo(this IBitmapImage image, WriteableBitmap writeableBitmap)
{
   Kernel32.MoveMemory(
            writeableBitmap.BackBuffer,
            image.Data,
            Convert.ToUInt32(
                writeableBitmap.PixelWidth *
                writeableBitmap.PixelHeight *
                (PixelFormats.Bgr24.BitsPerPixel / 8)));
    }

after that I convert the backbuffer of the writeable bitmap to a byte array and save it to the database. later on when the picture needs to be viewed I grabs it from the database.

var width = cameraOneBitmap.PixelWidth;
var height = cameraOneBitmap.PixelHeight;
var stride = width * ((cameraOneBitmap.Format.BitsPerPixel + 7) / 8);

var bitmapData = new byte[height * stride];

cameraOneBitmap.CopyPixels(bitmapData, stride, 0);
c#
wpf
entity-framework
asked on Stack Overflow Oct 24, 2014 by shady • edited Oct 24, 2014 by shady

1 Answer

2

When you initialize an Image with a StreamSource, it expects an encoded image (e.g., PNG, JPG, etc.). There's nothing you can do with just the raw bitmap data; you also need, at minimum, the dimensions and stride (or dimensions and bit depth, from which you can calculate the stride).

Once you have those, you can use BitmapSource.Create:

var bitmapSource = BitmapSource.Create(
    width,
    height,
    horizontalDpi, // typically 96
    verticalDpi,   // typically 96
    PixelFormats.Bgr24,
    /* palette: */ null,
    /* (byte[]) */ bitmapData,
    stride);
answered on Stack Overflow Oct 24, 2014 by Mike Strobel

User contributions licensed under CC BY-SA 3.0