CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2001
    Location
    Czech Republic
    Posts
    78

    Why does CopyImage fail?

    Hi, in my program I create several dozen of quite big monochrome bitmaps (approx. 6000 x 1500 pixels). This is done in a standard way by calling
    Code:
    CBitmap bmp;
    bmp.CreateCompatibleBitmap( &memDC, bitmapWidth, bitmapHeight);
    where memDC is obviously a memory DC. I need then to copy the bitmap and get a new handle. I do this by calling
    Code:
    HBITMAP hBitmapNew = (HBITMAP) CopyImage( (HBITMAP) bmp.GetSafeHandle(), IMAGE_BITMAP, 0, 0, LR_MONOCHROME );
    However, the CopyImage method often fails and GetLastError returns 8 which means "not enough memory". What confuses me the most is that after this unsuccessful call I can happily create other bitmaps of the same size with the CreateCompatibleBitmap method. When there is not enough memory to copy a bitmap, shouldn't there be also not enough memory to create it? Or is there any other reason why CopyImage fails?

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Why does CopyImage fail?

    Don't know what the cause of your problem is, but why not use bitblt. I never even heard of CopyImage. Always used bitblt.

  3. #3
    Join Date
    Mar 2001
    Location
    Czech Republic
    Posts
    78

    Re: Why does CopyImage fail?

    Quote Originally Posted by Skizmo View Post
    Don't know what the cause of your problem is, but why not use bitblt. I never even heard of CopyImage. Always used bitblt.
    I don't want to put the bitmap to a DC (to display it somewhere). I need to copy the bitmap data in the memory, so that I have two independent copies of it.

  4. #4
    Join Date
    Apr 2009
    Posts
    598

    Re: Why does CopyImage fail?

    I have always used BitBlt() too, but CopyImage() looks interesting.

    It is said, at http://www.codeproject.com/Messages/...mage-woes.aspx , that the color depth must be the same for the source and target image. I am not sure that your last argument, LR_MONOCHROME, is correct. Can you try with something else? e.g. LR_CREATEDIBSECTION.
    Last edited by olivthill2; January 28th, 2010 at 05:27 AM.

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Why does CopyImage fail?

    This http://support.microsoft.com/kb/935607 could be an answer to your problem. Even if the description provided by MS would indicate otherwise since the color depth is just 1 bit.

  6. #6
    Join Date
    Mar 2001
    Location
    Czech Republic
    Posts
    78

    Re: Why does CopyImage fail?

    Quote Originally Posted by olivthill2 View Post
    I have always used BitBlt() too, but CopyImage() looks interesting.

    It is said, at http://www.codeproject.com/Messages/...mage-woes.aspx , that the color depth must be the same for the source and target image. I am not sure that your last argument, LR_MONOCHROME, is correct. Can you try with something else? e.g. LR_CREATEDIBSECTION.
    I tried setting the flags to zero, with no effect. And now I'm even more confused - the program created and successfully copied 95 bitmaps. Then it created two more, the CopyImage method returned a non-NULL handle for them, but GetLastError called immediately after it returned 8! From then on, the program was able to create the bitmaps but CopyImage always returned NULL and GetLastError returned 8. What's going on?

  7. #7
    Join Date
    Mar 2001
    Location
    Czech Republic
    Posts
    78

    Re: Why does CopyImage fail?

    Quote Originally Posted by OReubens View Post
    This http://support.microsoft.com/kb/935607 could be an answer to your problem. Even if the description provided by MS would indicate otherwise since the color depth is just 1 bit.
    Thanks for an interesting link, but I am running Windows XP

  8. #8
    Join Date
    Mar 2001
    Location
    Czech Republic
    Posts
    78

    Re: Why does CopyImage fail?

    Another update: when I try using a DIB instead of a DDB (by calling CreateDIBSection instead of CreateCompatibleBitmap), CopyImage always passes but GetLastError after it returns always 8.

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Why does CopyImage fail?

    Could be you're running into a limitation of GDI. And yes, even on XP there are still limits on certain GDI/USER/Kernel resources.

    My guess is that you're hitting the limit of either bitmap handles, bitmap headers (which use memory in the global GDI space) combined with the fact that CopyImage needs a temporary buffer for the image rescaling (Even if you're not actually rescaling).

    Why do you need an actual copy of the bitmap ? As far as windows is concerned, there's no real reason to actually ever have a duplicate. If you're not painting on it, why does it even matter ?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured