GVillani82
GVillani82

Reputation: 17429

Exception on BitmapFrame.Create (bug in WPF framework?)

I implemented a C# application that recevies frame RGB at framerate of 30fps.

The event of frame arrive is managed with this code:

void client_ColorFrameReady(object sender, ColorFrameReadyEventArgs e)
{
    mycounter++;
    Console.WriteLine("new frame received: " + mycounter);

    if (writer != null)
    {
        count++;
        if (count % 2== 0)
        {
            using (var frame = BitmapImage2Bitmap(e.ColorFrame.BitmapImage))
            using (var thumb = ResizeBitmap(frame, 320, 240))
            {
                writer.WriteVideoFrame(thumb);
            }
        }
    }
    else
    {
        writer.Close();
    }
}

with the if condition I manage only one of two frames.

When my code call BitmapImage2Bitmap I obtain this exception:

enter image description here

The exception in english should be:

A first chance exception of type 'System.NotSupportedException' occurred in `PresentationCore.dll`
Additional information: BitmapMetadata is not available on BitmapImage.

The strange thing is that my application works "well" because the frames are correctly inserted in the output file.

I've read this, so the problem seems a bug in WPF framework.

Upvotes: 12

Views: 10321

Answers (4)

user3172292
user3172292

Reputation: 161

I hope my answer will help you.

I was using the same code, but PresetationCore.dll throws an exception when we are using BitmapFrame.Create(source).

So, I'm using another overload of the BitmapFrame.Create function:

public static BitmapFrame Create(
   BitmapSource source,
   BitmapSource thumbnail,
   BitmapMetadata metadata,
   ReadOnlyCollection<colorcontext> colorContexts
   )

We can get the same result with BitmapFrame.Create(source, null, null, null).

In your case: enc.Frames.Add(BitmapImage.Create(bitmap, null, null, null));

Upvotes: 16

jool
jool

Reputation: 2301

I've come across this issue lately when dealing with images included in projects as Resources (BuildType=Resource in file properties). It seems to be some build related issue which makes the resources corrupt and causes what seems like random issues as WPF are loading them. Simply performing a clean/rebuild makes the error(s) go away. They may reappear when adding new images though but the same fix obviously applies.

Upvotes: 0

user2359723
user2359723

Reputation: 21

Running with .NET Framework 4.5, I had to change the similar line in the Microsoft SDK WPF Photo Viewer sample from

  _image = BitmapFrame.Create(_source);

to

  _image = BitmapFrame.Create(_source, BitmapCreateOptions.None, BitmapCacheOption.None);

to avoid the ConfigurationErrorsException. Things seem to be drifting under the hood...

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941635

This is by design. A first-chance exception notification doesn't mean that there's a problem. The relevant code inside the Create() method looks like this:

try
{
    metadata = source.Metadata as BitmapMetadata;
}
catch (NotSupportedException)
{
}

In other words, the exception is expected and simply swallowed. Which is certainly very annoying since these exceptions do make the debugger stop when you have the Thrown checkbox checked in the Debug + Exception dialog. But it certainly is not a bug, this was intentionally written this way. Sometimes it is a lot cheaper to just let an exception be thrown and swallowing it instead of writing the code that prevents the exception. Especially when it gets unpractical to avoid the exception, the case with bitmaps since there are so many different kind of bitmap types. Some of which don't support metadata. Wherever this is done inside the framework code, it is almost always done to make the code faster. Speed is also an important aspect of code.

Feature, not a bug. Untick the Thrown checkbox to avoid seeing these exceptions.

Upvotes: 24

Related Questions