Link to home
Start Free TrialLog in
Avatar of antelope
antelope

asked on

Bitmap problems.....

We have a program that takes an image from a CCD camera and analyzes it as well as displays it. This program works fine on our initial computer and on our server with archived data (no camera).

We have just expanded new computers and camera, however this code refuses to display the images on the new computers. It does get the data though, as the analysis are still correct. Perhaps someone can see a flaw that we are missing....

All computers are running NT (3 workstation and 1 Server) the two that will not display are running Workstation.

Here is the peice of code that should display. It generates no errors and there are no problems when stepped through in debug mode.

//bitbuffer - 262144 bytes of data
Bitmap = new CBitmap;
Bitmap->CreateBitmap( 512, 512, 1, 8, bit_buffer);
Bitmap->SetBitmapBits(8*512*512, bit_buffer);
      
if(Bitmap != NULL)
{
   CDC memoryDC;
   memoryDC.CreateCompatibleDC(&dc);
   memoryDC.SelectObject(Bitmap);
      
   dc.SetStretchBltMode(COLORONCOLOR);
   if (Palette)
   {
       dc.SelectPalette(Palette, false);
       dc.RealizePalette();
       memoryDC.SelectPalette(Palette, false);
   }
   dc.BitBlt(10,10,512,512, &memoryDC, 0,0, SRCCOPY);
   ReleaseDC(&memoryDC);
}

Thanks for your help!
Avatar of antelope
antelope

ASKER

Edited text of question
Avatar of jkr
Hmm - you didn't detect any problems because you didn't check whether tthe relevant operations failed ... the code looks good at first glance. You might want to try

   if (!dc.BitBlt(10,10,512,512, &memoryDC, 0,0, SRCCOPY))
      {
        char acError[ ERROR_MAX];

        wsprintf ( acError, "BitBltFailed(), reason: %d", GetLastError());
        MessageBox ( NULL, acError, "Error!", MB_OK | MB_ICONSTOP);
      }

I see some problems, not necessarily THE problem.

I think the ReleaseDC(&memoryDC); is wrong.  I beleive that MFC releases the DC when the DC is destroyed.  So this may cause it to be released a 2nd tim.e

The SelectObject and SelectPalette functions are not saving the previous bitmap or palette.  That is a serious error.  The previous bitmap and pallette must be saved AND must be selected back in before the DC is destroyed and before the bitmap or palette is destroyed.

Is the DC compatible with an 8 bits per pixel bitmap?  If the DC is for the screen and the screen is not in 256 color mode, that could be the problem.  If that is the case, consider using a DIB instead.
'ReleaseDC(&memoryDC);' definitely is wrong and will return an error code, you should better use 'memoryDC.ReleaseOutputDC()' if you want to explicitly release it...
But won't it be released automatically when the CDC is destroyed (it is a local)?  The docs don't mention a destructor and I don't use it, but I would HOPE that it would be.
Actually, CDC::~CDC() even calls ::DeleteDC() ;-)
nietod:

Thanks!! It was the 256 color display that was the problem, we were setup for true color....

If you want to submit it as an answer I can give you the points...

Thanks again
Antelope
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks nietod...may take you up on that offer in the future...