Camera raw data directly to image using CxImage

http://social.msdn.microsoft.com/forums/en-US/windowssdk/thread/2408d395-5c57-494c-bb3c-ba4c15512c05

Hello!

I'm having a problem with BitBlt in my application.

I
 capture the frames of a webcam and display them with BitBlt directly on the main window. But after a few minutes the drawing stops, although the capturing is still running (i'm saving each 10th image in order to check that). 
So the images are not drawn anymore after some time, and even the  Windows " picture and fax viewer" does not work anymore when this problem occurs. 

The capturing runs in an own thread and each time a new frame is taken it is directly painted on the screen to get a live stream of the camera:

if(VI.isFrameNew(device1))
{
  hdc1 = GetDC(hWnd);
  imageBuffer = VI.getPixels(device1, false, false);
  cxImage.CreateFromArray((BYTE*)imageBuffer, VI.getWidth(device1), VI.getHeight(device1), 24, VI.getWidth(device1)*3, false);

  bmp = cxImage.MakeBitmap(hdc1);
  hdcMem = CreateCompatibleDC(hdc1);
  hBitmapPrev = (HBITMAP)SelectObject(hdcMem, bmp);

  BitBlt(hdc1, 20, 40, VI.getWidth(device1), VI.getHeight(device1), hdcMem, 0, 0, SRCCOPY);
  SelectObject(hdcMem, hBitmapPrev);
            
  DeleteDC(hdcMem); 
  DeleteDC(hdc1);
  imageBuffer = NULL;
}

Could you please tell me if I'm making any mistake in this code or if the problem is somewhere else?

Thanks in advance,
Daniel
-------------------------------------
I don't know if this is the source of your error, but you should "ReleaseDC(hdc1)"

See the documentation of DeleteDC at the msdn.

Hope this helps.
------------------------
Thanks for your reply.

But actually I was using ReleaseDC,i forgot to post this part of the code.

Here is the whole Thread-Method:

DWORD WINAPI ThreadProc( LPVOID pvoid ) 
{ 
    HDC hdc1, hdcMem;
    HBITMAP    bmp, hBitmapPrev;
    CxImage    cxImage;
    unsigned char* imageBuffer;    //Here is the camera image stored

    while(true)
    {
        hdc1 = GetDC(hWnd);
        if(VI.isFrameNew(device1))
        {
            imageBuffer = VI.getPixels(device1, false, false);
            cxImage.CreateFromArray((BYTE*)imageBuffer, VI.getWidth(device1), VI.getHeight(device1), 24, VI.getWidth(device1)*3, false);

            bmp = cxImage.MakeBitmap(hdc1);
            hdcMem = CreateCompatibleDC(hdc1);
            hBitmapPrev = (HBITMAP)SelectObject(hdcMem, bmp);

            BitBlt(hdc1, 20, 40, VI.getWidth(device1), VI.getHeight(device1), hdcMem, 0, 0, SRCCOPY);
            SelectObject(hdcMem, hBitmapPrev);
            
            DeleteDC(hdcMem);
            DeleteDC(hdc1);
            imageBuffer = NULL;
        }
        ReleaseDC(hWnd, hdcMem);
        ReleaseDC(hWnd, hdc1);
    }
    return 0;
}

Any other ideas about a possible reason for my problem?

Thanks a lot,
Daniel

EDIT: I could solve it now. My mistake was i didn't delete the Bitmaps. By adding

DeleteObject(bmp);
DeleteObject(hBitmapPrev);

everything works fine now.

Thanks a lot, alex! Your idea with releasing resources made me remember that I have to delete these objects too :-)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值