Problem creating an BitmapSource from an HBitmap in threaded code

  • Thread starter Shaun Bedingfield
  • Start date
S

Shaun Bedingfield

I am building a high-performance multi-threaded application.

I am making the following call in the code to convert a rasterized bitmap to
a bitmap source.
BitmapSource pageImageSource = Imaging.CreateBitmapSourceFromHBitmap(
bitmapHandle, IntPtr.Zero, Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());

Based on the documentation and lack of comments, I was assuming the method
was thread safe, however, I start getting exceptions after about 48 hours of
operation at full load in my development area.

The following exception is caught:
Exception occurred: System.ComponentModel.Win32Exception: The system cannot
find the file specified
at MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d)
at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32
exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr
parent, HwndWrapperHook[] hooks)
at System.Windows.Threading.Dispatcher..ctor()
at System.Windows.Freezable..ctor()
at System.Windows.Media.Imaging.BitmapSource..ctor(Boolean useVirtuals)
at System.Windows.Interop.InteropBitmap..ctor(IntPtr hbitmap, IntPtr
hpalette, Int32Rect sourceRect, BitmapSizeOptions sizeOptions,
WICBitmapAlphaChannelOption alphaOptions)
at
System.Windows.Interop.Imaging.CriticalCreateBitmapSourceFromHBitmap(IntPtr
bitmap, IntPtr palette, Int32Rect sourceRect, BitmapSizeOptions sizeOptions,
WICBitmapAlphaChannelOption alphaOptions)
at System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(IntPtr
bitmap, IntPtr palette, Int32Rect sourceRect, BitmapSizeOptions sizeOptions)
at
.....

Is my initial assumption about the thread-safety of the method correct? I
would hate to put a mutex around the code but I can if need be.

Is there another way to cause this exception to go away?

Thanks for any assistance
 
C

Chris Boarman

Hello Shaun, I am having the same problem in a multi-threaded WPF application. Did you ever get this resolved?

Here is the exception from my code:


08/02/2010 08:01:10.921 FATAL System.ComponentModel.Win32Exception: The system cannot find the file specified
at MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d)
at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
at MS.Win32.MessageOnlyHwndWrapper..ctor()
at System.Windows.Threading.Dispatcher..ctor()
at System.Windows.Threading.Dispatcher.get_CurrentDispatcher()
at System.Windows.Freezable..ctor()
at System.Windows.Media.Imaging.BitmapSource..ctor(Boolean useVirtuals)
at System.Windows.Media.Imaging.BitmapImage..ctor()
at CM.Imager.ViewModel.ImageViewModel.GetBitmapImageFromBitmap(Bitmap bitmap) in C:\Software\Products\CrystalMation\Source\Application\ImagerViewModel\ImageViewModel.cs:line 2854

The above code is running on the UI thread.

This happens after running our application continuously for a few days. I think the system is running out of some needed resource. There was plenty of memory available as reported by task manager.




ShaunBedingfiel wrote:

Problem creating an BitmapSource from an HBitmap in threaded code
05-Mar-08

I am building a high-performance multi-threaded application

I am making the following call in the code to convert a rasterized bitmap to
a bitmap source.
BitmapSource pageImageSource = Imaging.CreateBitmapSourceFromHBitmap
bitmapHandle, IntPtr.Zero, Int32Rect.Empty
BitmapSizeOptions.FromEmptyOptions())

Based on the documentation and lack of comments, I was assuming the method
was thread safe, however, I start getting exceptions after about 48 hours of
operation at full load in my development area

The following exception is caught
Exception occurred: System.ComponentModel.Win32Exception: The system cannot
find the file specifie
at MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d
at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32
exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr
parent, HwndWrapperHook[] hooks
at System.Windows.Threading.Dispatcher..ctor(
at System.Windows.Freezable..ctor(
at System.Windows.Media.Imaging.BitmapSource..ctor(Boolean useVirtuals
at System.Windows.Interop.InteropBitmap..ctor(IntPtr hbitmap, IntPtr
hpalette, Int32Rect sourceRect, BitmapSizeOptions sizeOptions,
WICBitmapAlphaChannelOption alphaOptions
at
System.Windows.Interop.Imaging.CriticalCreateBitmapSourceFromHBitmap(IntPtr
bitmap, IntPtr palette, Int32Rect sourceRect, BitmapSizeOptions sizeOptions,
WICBitmapAlphaChannelOption alphaOptions
at System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(IntPtr
bitmap, IntPtr palette, Int32Rect sourceRect, BitmapSizeOptions sizeOptions
at
....

Is my initial assumption about the thread-safety of the method correct? I
would hate to put a mutex around the code but I can if need be.

Is there another way to cause this exception to go away?

Thanks for any assistance

Previous Posts In This Thread:

Problem creating an BitmapSource from an HBitmap in threaded code
I am building a high-performance multi-threaded application

I am making the following call in the code to convert a rasterized bitmap to
a bitmap source.
BitmapSource pageImageSource = Imaging.CreateBitmapSourceFromHBitmap
bitmapHandle, IntPtr.Zero, Int32Rect.Empty
BitmapSizeOptions.FromEmptyOptions())

Based on the documentation and lack of comments, I was assuming the method
was thread safe, however, I start getting exceptions after about 48 hours of
operation at full load in my development area

The following exception is caught
Exception occurred: System.ComponentModel.Win32Exception: The system cannot
find the file specified
at MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d)
at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32
exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr
parent, HwndWrapperHook[] hooks)
at System.Windows.Threading.Dispatcher..ctor()
at System.Windows.Freezable..ctor()
at System.Windows.Media.Imaging.BitmapSource..ctor(Boolean useVirtuals)
at System.Windows.Interop.InteropBitmap..ctor(IntPtr hbitmap, IntPtr
hpalette, Int32Rect sourceRect, BitmapSizeOptions sizeOptions,
WICBitmapAlphaChannelOption alphaOptions)
at
System.Windows.Interop.Imaging.CriticalCreateBitmapSourceFromHBitmap(IntPtr
bitmap, IntPtr palette, Int32Rect sourceRect, BitmapSizeOptions sizeOptions,
WICBitmapAlphaChannelOption alphaOptions)
at System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(IntPtr
bitmap, IntPtr palette, Int32Rect sourceRect, BitmapSizeOptions sizeOptions)
at
.....

Is my initial assumption about the thread-safety of the method correct? I
would hate to put a mutex around the code but I can if need be.

Is there another way to cause this exception to go away?

Thanks for any assistance


Submitted via EggHeadCafe - Software Developer Portal of Choice
ASP.NET Providerless Custom Forms Authentication, Roles and Profile with MongoDb
http://www.eggheadcafe.com/tutorial...ntication-roles-and-profile-with-mongodb.aspx
 
Top