Every time I try loadimage I get a conversion error....I've tried about 20 different thiings and can't get it... does someone have a sample laying around that uses LoadImage()?
This is the code I wrote for a dialog(testing purpose) in a control button clicked function.
Later, I'm going to do a SDI to make use of the CFileDialog to load the bitmap.
Code:
//The code below, loads the bitmap into a mem DC
//then from the memory DC blit to the dialog window.
//** Create a paint DC from the dialog window
CClientDC clientDC(this);
//Win32 code below
//char Path[]="E:\\svbitmaps\\sv24bit.bmp";
//Change the Path and file to the one u want
char Path[]="E:\\svbitmaps\\sv8bitbig.bmp";
HANDLE hImage=LoadImage(NULL,Path,IMAGE_BITMAP,
0,0,LR_LOADFROMFILE);
if (hImage==NULL){
CHAR szBuf[80];
DWORD dw = GetLastError();
sprintf(szBuf, "%s failed: GetLastError returned %u\n",
"LoadImage", dw);
//Don't be mistaken; This is MFC version of MessageBox
MessageBox(szBuf, "Error", MB_OK);
OnOK();
}
//Structure to hold information about the Bitmap
BITMAP Bitmap;
//Holds the dimensions of the Bitmap
SIZE ImageSize;
//Get info about the bitmap
GetObject(hImage, sizeof(Bitmap), &Bitmap);
ImageSize.cx=Bitmap.bmWidth;
ImageSize.cy=Bitmap.bmHeight;
//MFC code starts here
//Create compatible memory device context
CDC memDC;
memDC.CreateCompatibleDC(&clientDC);
// ** Select it into our memory device context
memDC.SelectObject(CBitmap::FromHandle((HBITMAP) hImage));
//do blitting here.
clientDC.BitBlt(0,0,ImageSize.cx,ImageSize.cy,&memDC,0,0,SRCCOPY);
//Delete DC for the Bitmap
DeleteObject(hImage);
I CAN'T do a OnPaint(WM_PAINT). If I tried to call the above routine, it hung my Win98.
Nvm, here's the solution.
Code:
void CLoadBitmapsvDlg::OnDraw()
{
// TODO: Add your control notification handler code here
//The code below, loads the bitmap into a mem DC
//then from the memory DC blit to the dialog window.
//** Create a paint DC from the dialog window
CPaintDC paintDC(this);
//Win32 code below
//char Path[]="E:\\svbitmaps\\sv24bit.bmp";
char Path[]="E:\\svbitmaps\\sv8bitbig.bmp";
HANDLE hImage=LoadImage(NULL,Path,IMAGE_BITMAP,
0,0,LR_LOADFROMFILE);
if (hImage==NULL){
CHAR szBuf[80];
DWORD dw = GetLastError();
sprintf(szBuf, "%s failed: GetLastError returned %u\n",
"LoadImage", dw);
//Don't be mistaken; This is MFC version of MessageBox
MessageBox(szBuf, "Error", MB_OK);
OnOK();
}
//Structure to hold information about the Bitmap
BITMAP Bitmap;
//Holds the dimensions of the Bitmap
SIZE ImageSize;
//Get info about the bitmap
GetObject(hImage, sizeof(Bitmap), &Bitmap);
ImageSize.cx=Bitmap.bmWidth;
ImageSize.cy=Bitmap.bmHeight;
//MFC code starts here
//Create compatible memory device context
CDC memDC;
memDC.CreateCompatibleDC(&paintDC);
// ** Select it into our memory device context
memDC.SelectObject(CBitmap::FromHandle((HBITMAP) hImage));
//do blitting here.
paintDC.BitBlt(0,0,ImageSize.cx,ImageSize.cy,&memDC,0,0,SRCCOPY);
//Delete DC for the Bitmap
DeleteObject(hImage);
}
Code in blue is what I had changed in order to call it in OnPaint function without crashing
WM_PAINT will be fired when everything you drew is cleared and it needs repaint. BeingPaint(....) is used to get the handle to the device context when WM_PAINT is fired. It doesn't clear anything because it's already done. Make sure you aren't drawing your second bitmap on top of the other one (like the x any coordinates are same).
Are you doing exactly the same thing as what transcendental posted? It seems like he's only drawing one bitmap so I don't know if there's any other bitmap or anything else to erase. Can you post your code?
Hmm odd, it seems that if you call BeginPaint() twice, it effects the visibility of your contents. A quick solution would be to call BeginPaint(...) once when you get WM_PAINT message and then call graphics.paint() with the hdc you got your BeginPaint(...). Here's an example of working code:
PHP Code:
//Steve Mack
#include <windows.h>
HWND ghWnd_Main;
HINSTANCE ghInst;
HINSTANCE ghInst2;
class graphic
{
public:
int x;
int y;
char filename[50];
// Use LoadImage() to get the image loaded into a DIBSection
*phBitmap = (HBITMAP)LoadImage( NULL, szFileName, IMAGE_BITMAP, 0, 0,
LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE );
if( *phBitmap == NULL )
return FALSE;
// Get the color depth of the DIBSection
GetObject(*phBitmap, sizeof(BITMAP), &bm );
// If the DIBSection is 256 color or less, it has a color table
if( ( bm.bmBitsPixel * bm.bmPlanes ) <= 8 )
{
HDC hMemDC;
HBITMAP hOldBitmap;
RGBQUAD rgb[256];
LPLOGPALETTE pLogPal;
WORD i;
// Create a memory DC and select the DIBSection into it
hMemDC = CreateCompatibleDC( NULL );
hOldBitmap = (HBITMAP)SelectObject( hMemDC, *phBitmap );
// Get the DIBSection's color table
GetDIBColorTable( hMemDC, 0, 256, rgb );
// Create a palette from the color tabl
pLogPal = (LOGPALETTE *)malloc( sizeof(LOGPALETTE) + (256*sizeof(PALETTEENTRY)) );
pLogPal->palVersion = 0x300;
pLogPal->palNumEntries = 256;
for(i=0;i<256;i++)
{
pLogPal->palPalEntry[i].peRed = rgb[i].rgbRed;
pLogPal->palPalEntry[i].peGreen = rgb[i].rgbGreen;
pLogPal->palPalEntry[i].peBlue = rgb[i].rgbBlue;
pLogPal->palPalEntry[i].peFlags = 0;
}
*phPalette = CreatePalette( pLogPal );
// Clean up
free( pLogPal );
SelectObject( hMemDC, hOldBitmap );
DeleteDC( hMemDC );
}
else // It has no color table, so use a halftone palette
{
HDC hRefDC;
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam); // Not handled - let the system deal with it
}
//==================WINDOW CODE=========================================================
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
ghInst = hInstance;
ghInst2 = hInstance;
WNDCLASSEX wcxMyClass;
wcxMyClass.cbSize = sizeof(WNDCLASSEX); // Size of structure in bytes
wcxMyClass.style = 0; // Extra style information
wcxMyClass.lpfnWndProc = WndProc; // Pointer to window procedure
wcxMyClass.cbClsExtra = 0; // Extra bytes in class definition
wcxMyClass.cbWndExtra = 0; // Extra bytes for each window
wcxMyClass.hInstance = hInstance; // Instance handle for the class
wcxMyClass.hIcon = NULL; // Default icon - use system
wcxMyClass.hCursor = LoadCursor(NULL, IDC_ARROW); // Load the system arrow
wcxMyClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); // Background brush
wcxMyClass.lpszMenuName = NULL; // Menu name [MAKEINTRESOURCE(IDM_MAINMENU)] - none here
wcxMyClass.lpszClassName = "TESTCLASS"; // Class name
wcxMyClass.hIconSm = NULL; // Small icon
RegisterClassEx(&wcxMyClass); // Register the class
He uses DIB sections, which are strange hybrids between DDBs and DIBs. StretchDIBits and SetDIBitsToDevice are slow, especially on WinNT.
In MFC7 there is the new CImage class which is capable of loading BMP, GIF, JPEG and PCX natively and can be extended by extending GDI+ images to other formats.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.