Solved

how to Get Rid GetObject Error !!

Posted on 2000-03-07
4
324 Views
Last Modified: 2010-04-10
hi !!

I am using the below function described
BOOL CDib::CreateDibIndirect( int width, int height, int planes, int bytesPerPixel , CDC& dc, RGBQUAD* colors, int numCols, void** frameData )
{
      CDC memdc;
      memdc.CreateCompatibleDC(&dc);
            
      char* bmi = new char[sizeof(BITMAPINFOHEADER) + numCols*sizeof(RGBQUAD)];
      BITMAPINFOHEADER& bmih = *(BITMAPINFOHEADER*)bmi;
      
      RGBQUAD* newcolors = (RGBQUAD*)(&bmih+1);
      
      bmih.biSize = sizeof( BITMAPINFOHEADER );
      bmih.biWidth = width;
      bmih.biHeight = height;
      bmih.biPlanes = planes;
      bmih.biBitCount = bytesPerPixel * 8;
      bmih.biCompression = BI_RGB;
      bmih.biSizeImage = 0;
      bmih.biXPelsPerMeter = 4000;
      bmih.biYPelsPerMeter = 4000;
      bmih.biClrUsed = numCols;
      bmih.biClrImportant = numCols;
      memcpy(newcolors, colors, numCols*sizeof(RGBQUAD));
      
      
      // Create a DIB section for our image
      HBITMAP ds = ::CreateDIBSection( memdc, (BITMAPINFO*)bmi, DIB_RGB_COLORS, frameData, NULL, NULL );
      delete[] bmi;
      return Attach( ds );
}

In which i am using one parameter of CreateDIBSection hOffset as NULL , due to which i get error when i latter use in as it gives (GetObject) NULL value , How should i use the value by using CreateFileMapping function to get rid of the error ??

Please answer !!
Thanks in advance
Govil
0
Comment
Question by:Govil
  • 3
4 Comments
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2594102
What does "to which i get error when i latter use in as it gives (GetObject) NULL value" mean?

What does this have to do with CreateFileMapping?

Perhaps if you show us the code in which you call the CDib::CreateDibIndirect that may help us understand.
0
 

Author Comment

by:Govil
ID: 2595012
thanks to look at my problem !! yeah I am sending u the code where i get the error !! When i call my save fuction from CView class it gives error at
CView::OnFileSave()
{

      CFileDialog *pSave;
      char szFilter[]="BMP Files(*.BMP)|*.BMP||";
      pSave = new CFileDialog(FALSE,"BMP","",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szFilter,this);

      if(pSave->DoModal () == IDOK)
      {
                        CFile file;
                  CFileException fe;
            
            if(!file.Open(pSave->GetPathName(),CFile::modeCreate | CFile::modeWrite,&fe))
            {
                  #ifdef _DEBUG
                        afxDump << "File could not be opened " << fe.m_cause << "\n";
                  #endif
                  return;
            }
      
            LPBYTE lpDib;
            bSave=TRUE;
            fImageConvert2->Save(file,hDib,lpDib);
            }
                  /*
                  AfxGetMainWnd()->SetWindowText (pSave->GetPathName () );
                  update=true;
                  */
            //}
            
                  //seq.Release ();
      

}

my save function is in CDib class code ia given below ,Sorry for lot of comments as just for testing !!

DWORD CDib::Save(CFile& file,HANDLE hDib,LPBYTE lpDib) //,HDC* hdc)//CDC* dc)
{
      /*
      //handle of the CGdiObject
      if (m_hObject != NULL)
      {
            //AfxMessageBox("First selct the frame");
            return FALSE;
      }
      */
      //CString str;
      //CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
      
      
      BOOL bSave = TRUE;
//      HDIB hDib;
      LPBITMAPINFOHEADER lpBI;   // Pointer to DIB info structure
      BITMAPFILEHEADER hdr;
      DWORD dwDIBSize;

      DIBSECTION ds;
      //Attach( ds );
       VERIFY(GetObject(sizeof(ds), &ds)==sizeof(ds));
            
      char* buf = new char[sizeof(BITMAPINFOHEADER) + MAXPALCOLORS*sizeof(RGBQUAD)];
               
      BITMAPINFOHEADER& bmih = *(BITMAPINFOHEADER*)buf;
      RGBQUAD* bmicolors = (RGBQUAD*)(&bmih+1);
      
      GetColorTable(bmicolors,MAXPALCOLORS);
      memcpy(&bmih, &ds.dsBmih, sizeof(bmih));

      //memcpy(bmicolors,bmicolors,MAXPALCOLORS*sizeof(RGBQUAD));
      //Get a pointer to the DIB memory,first of which contains the Bitmapinfo structure
      //lpBI = (LPBITMAPINFOHEADER)GlobalLock(hDib);
      //if (!m_hObject)
      //      return FALSE;

      if(hDib == NULL)
            return 0;

      if(&bmih == NULL)
            return 0;

//      if (lpBI == NULL)
//            return 0;
      
      if(bmicolors == NULL)
            return 0;
            
      //fill the fields of file header
      // fill the file type( first two will be "BM")

            hdr.bfType = BFT_BITMAP;//0x4d42;//DIB_HEADER_MARKER;

      //Calculating the size of DIB

      // First find the size of header plus size of color table.
      //the first DWORD in BITMAPINFOHEADER and BITMAPCOREHEADER contains
      //the size of the structure
      //Now Calculate the size of the Palette
      
      //dwDIBSize = *(LPDWORD)&lpBI + PaletteSize();

      if((bmih.biCompression == BI_RLE8) || (bmih.biCompression  == BI_RLE4))
      {

            dwDIBSize +=bmih.biSizeImage;
            
      }
      else
      {
            DWORD dwBmBitsSize;                  //Size of the Bitmap Bits only

            //It's not RLE,so size is width (DWORD aligned) * Height

            dwBmBitsSize = WIDTHBYTES((bmih.biWidth) * ((DWORD)bmih.biBitCount)) * bmih.biHeight;
            dwDIBSize +=dwBmBitsSize;
            bmih.biSizeImage  = dwBmBitsSize;
            

      }

      //Calculating the file size by adding the DIB size to sizeof(BITMAPFILEHEADER)
      hdr.bfSize = dwDIBSize + sizeof(BITMAPFILEHEADER);
      hdr.bfReserved1 = 0;
      hdr.bfReserved2 = 0;

       //now calculate the offset
      
      hdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + bmih.biSize  + PaletteSize();


      //Write the file header

      file.Write ((LPSTR)&hdr,sizeof(BITMAPFILEHEADER));
      DWORD dwBytesSaved = sizeof(BITMAPFILEHEADER);

      // Write the DIB header
      
      UINT nCount = sizeof(BITMAPINFO) +(GetColorTable(bmicolors,MAXPALCOLORS)-1)*sizeof(RGBQUAD);
      dwBytesSaved += nCount;
      
      file.Write (&bmih,nCount);
      
      // Write the DIB bits

      DWORD dwBytes = bmih.biBitCount * bmih.biWidth ;      //Width();
      
      // Calculate the number of bytes per line

      if (dwBytes%32 == 0)
            dwBytes /= 8;
      else
      dwBytes = dwBytes/8 + (32-dwBytes%32)/8 + (((32-dwBytes%32)%8 > 0) ? 1 : 0);
      nCount = dwBytes * bmih.biHeight;      
      
      dwBytesSaved += nCount;
      
      file.WriteHuge (m_bm.bmBits ,nCount);
      
      return dwBytesSaved;

}

so i get degub error at line
VERIFY(GetObject(sizeof(ds), &ds)==sizeof(ds));

If u require any other information on code do let me know ASAP !!

By CreatFileMapping(...) i mean when i read the about CreateDIBSection in (msdn)help !!there it is mentioned like that !!

hope u will get me out of this error soon !! thanks in advance
Piyush Govil
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2598229
Try using (or looking at) the CSimpleDIB from the MSDN article "OpenGL VI: Rendering on DIBs with PFD_DRAW_TO_BITMAP".

Here is the class

CSimpleDIB.h:

#ifndef __CSimpleDIB_H__
#define __CSimpleDIB_H__

//
// CSimpleDIB -
//
// Implements a simple encapsulation of a DIB section and a DC.
//
class CSimpleDIB
{
      public:
            // Construction
            CSimpleDIB() ;
            virtual ~CSimpleDIB() ;

      // Create a CSimpleDIB object. Create can be called multiple times.
            void Create(int cx, int cy, int ibitcount ) ;

      // Draw the DIB onto the destination DC.
            void Draw(CDC* pdcDest, int x, int y) ;

      // Set the palette used in the DIB.
            void SetPalette(CPalette* pPal) ;

            CDC* GetDC() {return m_pdc;}
            HBITMAP GetHandle() {return m_hbmp;}


      protected:
            HBITMAP m_hbmp ;
            HBITMAP m_hbmOld ;
            void* m_pBits ;            // Pointer to the bits.
            CSize m_sizeDIB ;
            CDC* m_pdc ;
      
            void destroy() ;       // Destruction helper.
      
};
#endif


CSimpleDib.cpp:

#include "stdafx.h"
#include "CSimpleDIB.h"

//
// Construction
//
CSimpleDIB::CSimpleDIB()
      : m_sizeDIB(0,0)
{
      m_hbmp = NULL ;
      m_pBits = NULL ;
      m_pdc = NULL ;
}

//
// Destruction.
//
CSimpleDIB::~CSimpleDIB()
{
      destroy() ;
}


//
//      Destruction helper.
//
void CSimpleDIB::destroy()
{
    // Select the old bitmap back into the buffer DC.
    if (m_hbmOld)
    {
        ::SelectObject(m_pdc->GetSafeHdc(), m_hbmOld);
    }

      // Delete bitmap.
      if (m_hbmp) DeleteObject(m_hbmp);

      // Delete DC.
      if (m_pdc) delete m_pdc ;
}

//
// Create a DIB section cx X cy X ibitcount.
//
void CSimpleDIB::Create(int cx, int cy, int ibitcount)
{
   ASSERT((ibitcount == 8) || (ibitcount == 16) || (ibitcount = 24))      ;
   ASSERT(cx > 0);
   ASSERT(cy > 0);

   // Destroy parts of objects if we are recreating it.
   if ((m_pdc != NULL) || (m_hbmp != NULL)) destroy() ;

   // Save size for drawing later.
   m_sizeDIB.cx = cx ;
   m_sizeDIB.cy = cy ;

   // Create a BITMAPINFOHEADER structure to describe the DIB
   BITMAPINFOHEADER BIH ;
   int iSize = sizeof(BITMAPINFOHEADER) ;
   memset(&BIH, 0, iSize);

   // Fill in the header info.
   BIH.biSize = iSize;
   BIH.biWidth = cx;
   BIH.biHeight = cy;
   BIH.biPlanes = 1;
   BIH.biBitCount = ibitcount;
   BIH.biCompression = BI_RGB;

   // Create a new DC.
   m_pdc = new CDC ;
   m_pdc->CreateCompatibleDC(NULL);

   // Create the DIB section.
   m_hbmp = CreateDIBSection( m_pdc->GetSafeHdc(),
                                 (BITMAPINFO*)&BIH,
                                 DIB_PAL_COLORS,
                                 &m_pBits,
                                 NULL,
                                 0);

    ASSERT(m_hbmp);
    ASSERT(m_pBits);

    // Select the new bitmap into the buffer DC
    if (m_hbmp)
    {
        m_hbmOld = (HBITMAP)::SelectObject(m_pdc->GetSafeHdc(),
                                           m_hbmp);
    }
     
}

//
// BitBlt DIB section to the screen. Does not mess with the palette.
//
void CSimpleDIB::Draw(CDC* pdcDest, int x, int y)
{
    // Note: you do not need to select the palette into
    // the memory DC because the DIB section is using palette
    // index values not colors
    pdcDest->BitBlt( x, y,
                     m_sizeDIB.cx, m_sizeDIB.cy,
                     m_pdc,
                     0, 0,
                     SRCCOPY);

}

//
// Set the color table in the DIB section.
//
void CSimpleDIB::SetPalette(CPalette* pPal)
{
    ASSERT(pPal);

    // get the colors from the palette
    int iColors = 0;
    pPal->GetObject(sizeof(iColors), &iColors);
    ASSERT(iColors > 0);
    PALETTEENTRY* pPE = new PALETTEENTRY[iColors];
    pPal->GetPaletteEntries(0, iColors, pPE);

    // Build a table of RGBQUADS
    RGBQUAD* pRGB = new RGBQUAD[iColors];
    ASSERT(pRGB);
    for (int i = 0; i < iColors; i++) {
        pRGB[i].rgbRed = pPE[i].peRed;
        pRGB[i].rgbGreen = pPE[i].peGreen;
        pRGB[i].rgbBlue = pPE[i].peBlue;
        pRGB[i].rgbReserved = 0;
    }
   
    ::SetDIBColorTable(m_pdc->GetSafeHdc(),
                       0, iColors,
                       pRGB);

    delete [] pRGB;
    delete [] pPE;
}

0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 50 total points
ID: 2775681
i have code for saving a DIB to file that does work.

Your code seems to be doing strange things.

For example, you are doing a GetObject on a DIBSECTION that hasn't been created yet .. that's why it asserts

Here is the code I use to write out a 24-bit dib...

bool Q24bitDIB::WriteToFile(LPCTSTR filename) const {
    bool ok = false;
    TRY {
        CFile file(filename,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
        ok = WriteToFile(file);
    } CATCH_ALL(e) {
    } END_CATCH_ALL
    return ok;
}

bool Q24bitDIB::WriteToFile(CFile& file) const {
    QReentrancyReader lock(&m_cs,"Q24bitDIB::WriteToFile");

    if (! m_pBits) return false;

    // setup file header
    BITMAPFILEHEADER hdr;
    hdr.bfType = 0x4d42;    // 0x42 = "B", 0x4d = "M"
    hdr.bfReserved1 = 0;
    hdr.bfReserved2 = 0;

    BITMAPINFOHEADER bitmapinfoheader;
    GetBITMAPINFOHEADER(bitmapinfoheader);

    // Compute the offset to the array of color indices.
    hdr.bfOffBits = (DWORD) (
        sizeof(BITMAPFILEHEADER) +
        bitmapinfoheader.biSize +
        bitmapinfoheader.biClrUsed * sizeof (RGBQUAD)
    );

    // Compute the size of the entire file.
    hdr.bfSize = hdr.bfOffBits + bitmapinfoheader.biSizeImage;

    bool ok = false;
    TRY {
        // Copy the BITMAPFILEHEADER into the .BMP file.
        file.WriteHuge(&hdr,sizeof(hdr));
        // Copy the BITMAPINFOHEADER and RGBQUAD array into the file.
        file.WriteHuge(&bitmapinfoheader,sizeof(bitmapinfoheader) + bitmapinfoheader.biClrUsed * sizeof (RGBQUAD));
        // Copy the array of color indices into the .BMP file.
        file.WriteHuge(m_pBits,bitmapinfoheader.biSizeImage);

        ok = true;
    } CATCH_ALL (e) {
    } END_CATCH_ALL
    return ok;
}
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now