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);
3
  • 1
    stackoverflow.com/questions/8897210/… Commented Oct 24, 2014 at 16:52
  • Does the byte array contain an encoded bitmap buffer (e.g. PNG or JPEG), or is it raw pixel data? Commented Oct 24, 2014 at 17:49
  • Its raw pixel data in the BGR24 format. I saw that post but it didn't work. I'll add more info in my post. Commented Oct 24, 2014 at 18:20

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.