Windows Vista Forums

BitmapSource Disposal/cleanup

  1. #1


    King Adrock Guest

    BitmapSource Disposal/cleanup

    I have a dispatchtimer pulling System.Drawing.Bitmap Images from my
    webcam and I am displaying them on my WPF UI. I am using a
    BitmapSource created by CreateBitmapSourceFromHBitmap using the
    ..GetHbitmap() method from my Bitmap object.

    I am getting some serious memory leaks - besides setting my ImageSource
    to null before assinging a new BitmapSource to it, is there anyway I
    can dispose of the object?


      My System SpecsSystem Spec

  2. #2


    robertwl@nospam.microsoft.com Guest

    re: BitmapSource Disposal/cleanup

    You are likely leaking HBITMAPs. Are you doing something like this:

    BitmapSource bmpSrc = null;

    using(System.Drawing.Bitmap bmp = ...)
    {
    bmpSrc = System.Windows.Interop.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(),...);
    }

    ?

    If not, then you're leaking HBITMAPs from the System.Drawing.Bitmap. Those need to be disposed after using them.

    -----Original Message-----
    From: King Adrock
    Posted At: Monday, July 24, 2006 4:31 PM
    Posted To: microsoft.public.windows.developer.winfx.avalon
    Conversation: BitmapSource Disposal/cleanup
    Subject: BitmapSource Disposal/cleanup


    I have a dispatchtimer pulling System.Drawing.Bitmap Images from my
    webcam and I am displaying them on my WPF UI. I am using a
    BitmapSource created by CreateBitmapSourceFromHBitmap using the
    ..GetHbitmap() method from my Bitmap object.

    I am getting some serious memory leaks - besides setting my ImageSource
    to null before assinging a new BitmapSource to it, is there anyway I
    can dispose of the object?

      My System SpecsSystem Spec

  3. #3


    King Adrock Guest

    Re: BitmapSource Disposal/cleanup

    I've made the changes you've shown me but I'm still leaking memory.
    Here is a snippit of code form my app - this is running at 30fps and I
    update whenever I receive a new frame from my Video Camera

    using( System.Drawing.Bitmap bmp = IMGManager.MainBitmap )
    {
    if (vidMain.Fill == null)
    {
    vidMain.Fill = new
    ImageBrush(System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(),
    IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()));
    }
    else
    {
    ImageBrush bmpSrc = vidMain.Fill as ImageBrush;
    BitmapSource bsrc = bmpSrc.ImageSource as BitmapSource;
    bsrc = null; //no dispose method.. how do i clean up?
    bmpSrc.ImageSource =
    System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(),
    IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
    }
    }

    I see a post on the MSDN forums quoting a simmilar issue - Our issue is
    there is no visible way of manually disposing of the interopbitmapsouce

    http://forums.microsoft.com/MSDN/Sho...23409&SiteID=1


    robertwl@nospam.microsoft.com wrote:
    > You are likely leaking HBITMAPs. Are you doing something like this:
    >
    > BitmapSource bmpSrc = null;
    >
    > using(System.Drawing.Bitmap bmp = ...)
    > {
    > bmpSrc = System.Windows.Interop.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(),...);
    > }
    >
    > ?
    >
    > If not, then you're leaking HBITMAPs from the System.Drawing.Bitmap. Those need to be disposed after using them.
    >
    > -----Original Message-----
    > From: King Adrock
    > Posted At: Monday, July 24, 2006 4:31 PM
    > Posted To: microsoft.public.windows.developer.winfx.avalon
    > Conversation: BitmapSource Disposal/cleanup
    > Subject: BitmapSource Disposal/cleanup
    >
    >
    > I have a dispatchtimer pulling System.Drawing.Bitmap Images from my
    > webcam and I am displaying them on my WPF UI. I am using a
    > BitmapSource created by CreateBitmapSourceFromHBitmap using the
    > .GetHbitmap() method from my Bitmap object.
    >
    > I am getting some serious memory leaks - besides setting my ImageSource
    > to null before assinging a new BitmapSource to it, is there anyway I
    > can dispose of the object?



      My System SpecsSystem Spec

BitmapSource Disposal/cleanup

Similar Threads
Thread Forum
Disk Cleanup
Hi, I there anyway to add Clear Recent Items In the Disk Cleanup Files In Vista? As I need to do it manually.. I do not want CCleaner
General Discussion
Trojan cleanup.
Scanned Vista HDD on XP set. found Trojan in netlogon.dll. Deleted it. Do I have to Restore that file from a cab or SFC? or start in Safe? ...
Vista General
Disk Cleanup
Hi, Whenever I do a disk cleanup, when it is scanning It gets stuck on the system error memory dump files, and takes forever to finish the scan??...
Crashes and Debugging
Disc Cleanup
Hello, Could you please advise is it okay to delete Per User Queued Windows Error Reports and System Queued Windows Error Reports? They are...
Vista performance & maintenance
Cleanup after SP1 install
During install of SP1 a backup file will be created so that SP1 can be uninstalled ata later date if required. Does anybody know what this...
Windows Updates
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...
.NET General
How to put unmanaged CBitmap*->GetBitmapBits() into an BitmapSource-derivedclass
Hi all, I know there are some discussions about getting byte from BitmapSource and how to put byte into an BitmapSource through MemoryStream. I...
Avalon