Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6733015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:43:07+00:00 2026-05-26T10:43:07+00:00

I capture images from my web camera to Image control in WPF with WebCam_Capture.dll

  • 0

I capture images from my web camera to Image control in WPF with WebCam_Capture.dll. For every captured frame i have event called webCam_imageCaptured. It calls every time when i receive new image from web camera. In this event I call method LoadBitmap to get hBitmap and receive BitmapSource to put this captured picture to the Image control.
Here is code of this method:

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr handle);
public BitmapSource bs;
public IntPtr ip;
public BitmapSource LoadBitmap(System.Drawing.Bitmap source)
{
   ip = source.GetHbitmap();
   bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, System.Windows.Int32Rect.Empty,
   System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
   DeleteObject(ip);
   return bs;
}

And here is the code of the event:

private void webcam_ImageCaptured(object source, WebcamEventArgs e)
{
    //_frameImage is Image WPF control.
     _frameImage.Source = LoadBitmap((System.Drawing.Bitmap)e.WebCamImage);   
}

And I have a leak here! RAM loads up to 1 GB and sometimes more! DeleteObject(ip) works not every time when it called. When RAM is 1 GB or more, it releases or show me Out of memory error. How to solve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:43:08+00:00Added an answer on May 26, 2026 at 10:43 am

    Without the aid of a profiler or even seeing the rest of your code, I can almost guarantee that the “leak” is here:

    _frameImage.Source = LoadBitmap((System.Drawing.Bitmap)e.WebCamImage);
    

    That method is certainly being called very quickly and you are not Dispose()ing of your Bitmaps. The GC won’t have time to come clean up after you and call the finalizer on each Bitmap, so you run out of memory.

    You need to call Dispose() on that Bitmap after you’re done with it. The BitmapSource makes a copy of the image data anyway, so it shouldn’t be needed. I would try this:

    private void webcam_ImageCaptured(object source, WebcamEventArgs e)
    {
        using( Bitmap b = e.WebCamImage )
        {
             _frameImage.Source = LoadBitmap((System.Drawing.Bitmap)e.WebCamImage);   
        }
    }
    

    This of course assumes that disposing of the bitmap inside this event handler is a valid thing to do (i.e., does anyone else handle this event?) You may want to expose a BitmapSource in the WebCamEventArgs class instead so that you can make sure no other clients are relying on that Bitmap remaining valid.

    That is of course if you wrote the API to being with. If not you could write a wrapper to make sure your code never actually sees a Bitmap and deals only with WPF image types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed an application to upload image captured from camera to server. I
I have a 3rd party web page screen capture DLL from http://websitesscreenshot.com/ that lets
I am working on a project where I have to capture images from webcam.I
I've built a little camera capture daemon which captures a sequence of images from
I want to be able to capture images from a webcam on Linux. This
In my C/C++ program, I'm using OpenCV to capture images from my webcam. The
What library can you recommend to capture image from a webcam in .Net?
Basically just looking to see if you can capture an image from the webcam
I am using the iPhone camera to capture an image and them resizing and
I am evaluating VintaSoft .net control and Atalasoft DotTwain Image Capture. And I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.