CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Threaded View

  1. #1
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880

    [RESOLVED] Why does my bitmap become all black when saved to file?

    I am trying to create a bitmap. This works well as I can obtain the correct result on the screen. But when I am trying to save the bitmap to a file, it become just a black rectangle.

    I have tried a lot of things, multiple functions to save bitmap taken from different places, and always obtain the same result.

    Here is a simplified version of the code, which produces the same result. It can simply be called in the simplest project that uses MFC. If I put a break point just before calling the write function, I can see my picture on the screen, as expected. But the bitmap is black in the file.

    Code:
    void CreateDrawing()
    {
    	CWnd wnd;
    	CRect rect (0, 0, 100, 100);
    	wnd.CreateEx(NULL, NULL, NULL,WS_POPUP |  WS_CHILD | WS_CLIPSIBLINGS, rect, NULL, NULL, NULL);
    
    	CClientDC paintdc(&wnd);
    	paintdc.SetBkMode(TRANSPARENT);
    
    	CRect rectWnd = rect;
    	CPoint ptToolTipLeft = rectWnd.TopLeft();
    	SetWindowPos(wnd.m_hWnd, HWND_TOPMOST,ptToolTipLeft.x+1, ptToolTipLeft.y+1, rectWnd.Width(), rectWnd.Height(),SWP_SHOWWINDOW|SWP_NOOWNERZORDER|SWP_NOACTIVATE);
    
    	CBrush m_brush(RGB(255,255,255));
    	CPen m_pen(PS_SOLID, 0, COLORREF(RGB(0, 0, 0)));
    	CBrush* pOldBrush = paintdc.SelectObject(&m_brush);
    	CPen* pOldPen = paintdc.SelectObject(&m_pen);
    	paintdc.Rectangle(0,0,rect.Width(),rect.Height());
    	paintdc.MoveTo(0, 0);
    	paintdc.LineTo(100, 100);
    
    
    	HBITMAP hbmp = CreateCompatibleBitmap(paintdc.m_hDC, rect.Width(),rect.Height());
    	CBitmap bmp;
    	bmp.Attach(hbmp);
    
    	WriteToFile(paintdc, hbmp);
    
    	paintdc.SelectObject(pOldBrush);
    	paintdc.SelectObject(pOldPen);
    }
    
    
    bool WriteToFile(CDC& hMemDC, HBITMAP hBitmap)
    {
    	CString strFileName = "c:\\test\\test.bmp";
    	int   wBitCount = 32;
    
    	BITMAP   Bitmap;  
    
    	CPalette pal;
    	pal.Attach(::GetStockObject(DEFAULT_PALETTE));	
    	hMemDC.SelectPalette(&pal, TRUE);
    
    	GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap); 
    
    	BITMAPINFOHEADER   bi;
    	bi.biSize = sizeof(BITMAPINFOHEADER);    
    	bi.biWidth = Bitmap.bmWidth;    
    	bi.biHeight = Bitmap.bmHeight;  
    	bi.biPlanes = 1;    
    	bi.biBitCount = wBitCount;    
    	bi.biCompression = BI_RGB;    
    	bi.biSizeImage = 0;  
    	bi.biXPelsPerMeter = 0;    
    	bi.biYPelsPerMeter = 0;    
    	bi.biClrUsed = 0;    
    	bi.biClrImportant = 0; 
    
    	DWORD dwBmBitsSize = ((Bitmap.bmWidth * wBitCount + 31) / 32) * 4 * Bitmap.bmHeight; 
    
    	HANDLE hDib = GlobalAlloc(GHND, dwBmBitsSize+sizeof(BITMAPINFOHEADER)); 
    	LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);    
    	*lpbi = bi;
    
    	GetDIBits(hMemDC.m_hDC, hBitmap, 0, (UINT)Bitmap.bmHeight,  
    		(LPSTR)lpbi + sizeof(BITMAPINFOHEADER), (BITMAPINFO *)lpbi, DIB_RGB_COLORS);    
    
    
    	DWORD dwDIBSize   =   sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwBmBitsSize;
    
    	BITMAPFILEHEADER   bmfHdr;    
    	bmfHdr.bfType = 'B'+('M' << 8);
    	bmfHdr.bfSize = dwDIBSize;
    	bmfHdr.bfReserved1 = 0;
    	bmfHdr.bfReserved2 = 0;
    	bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);    
    
    	DWORD dwWritten;
    
    	HANDLE fh = CreateFile(strFileName,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,
    			FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);   
    	ASSERT(fh != INVALID_HANDLE_VALUE);
    	WriteFile(fh,   (LPSTR)&bmfHdr,   sizeof(BITMAPFILEHEADER),   &dwWritten,   NULL);    
    	WriteFile(fh, (LPSTR)lpbi, dwDIBSize, &dwWritten, NULL);    
    	GlobalUnlock(hDib);    
    	GlobalFree(hDib);    
    	CloseHandle(fh);
    	return true;
    }
    I am probably missing something simple, but could not figure what.
    Last edited by Elrond; June 28th, 2010 at 06:23 AM. Reason: Added resolved tag
    Elrond
    A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
    -- George Steiner

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