views:

5173

answers:

4

I'm trying to bind a Byte array from my databse to a WPF Image.

My XAML:

<Window.Resources>
    <local:BinaryImageConverter x:Key="imgConverter" />
</Window.Resources>
...
<Image Source="{Binding Path=ImageData, Converter={StaticResource imgConverter}}" />

I've modified code published by Ryan Cromwell for a value converter:

Class BinaryImageConverter
    Implements IValueConverter
    Private Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
        If value IsNot Nothing AndAlso TypeOf value Is Byte() Then
            Dim bytes As Byte() = TryCast(value, Byte())
            Dim stream As New MemoryStream(bytes)
            Dim image As New BitmapImage()
            image.BeginInit()
            image.StreamSource = stream
            image.EndInit()
            Return image
        End If
        Return Nothing
    End Function
    Private Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
        Throw New Exception("The method or operation is not implemented.")
    End Function
End Class

The image.EndInit() line of the BinaryImageConverter's Convert() function throws this NotSupportedException:

"No imaging component suitable to complete this operation was found."

InnerException: "Exception from HRESULT: 0x88982F50"

I don't understand what I'm doing wrong. How can I get this working?


Update

It seems the problem was the bytes coming out of the database. There must have been a problem with the way I was putting them in.

See my working code below.

+1  A: 

I believe this is actually a security permission issue. Try running with administrator privileges, and see if that works, and go from there.

EDIT: I disagree with the downvote and comment. Take a look at this link:

http://social.expression.microsoft.com/Forums/en-US/wpf/thread/617f6711-0373-44cc-b72c-aeae20f0f7a8/

This user had the exact same error, and it was caused by security settings. Therefore, I stand by my answer (which may not be the cause, but it is certainly worth a try)

Razzie
@Razzie: More than likely not, given the error code.
casperOne
+1  A: 

My guess would be that the bytes are not a legitimate image format. I believe that error code corresponds to WINCODEC_ERR_COMPONENTNOTFOUND, which would be consistent with invalid bytes.

What format is the byte array supposed to be in? Can you save it to disk, and try to open it with another imaging program?

casperOne
This creates a 24,425 byte file that cannot be read by Windows Picture and Fax Viewer.
Zack Peterson
@Zack: Then that means that my answer was correct, you have an invalid format for the image file (either that, or the way you are getting/processing the bytes is incorrect and corrupting it).
casperOne
+1  A: 

Try using this

Dim imageSource as ImageSource
Dim bitmapDecoder = new PngBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
imageSource = bitmapDecoder.Frames[0];
imageSource.Freeze();
Return imageSource
bendewey
This throws the same exception on the "Dim bitmapDecoder = " line.
Zack Peterson
Is your image in PNG format?
bendewey
No. It's JPEG. But, JpegBitmapDecoder also fails.
Zack Peterson
If you save the image directly to a FileStream does it open successfully?
bendewey
Additionally, in your update you're reading the image from a a stream and then putting that image object's data into the database. why not just load the FileOpenStream directly into the database?
bendewey
bendewey, I changed my ButtonUpload_Click() method to "Using br As New BinaryReader(FileOpenStream)" and re-added the image to the database with that method. I still get the same exception.
Zack Peterson
what about saving it to disk directly from the database?
bendewey
This creates a 24,425 byte file that cannot be read by Windows Picture and Fax Viewer.
Zack Peterson
+2  A: 
Zack Peterson