问题描述
我正在尝试使用它作为资源加载.bmp图像。但它没有正确加载。放心bmp图像,它们位于正确的位置。现在,当我调试程序时,MessageBox("图像未正确加载")不会出现
但是弹出MessageBox("SelectObject()失败")。使用它作为资源的图像加载有什么问题。使用fuLoad作为LR_LOADFROMFILE的bitmap1正确加载并显示图像。但是我认为IDB_BITMAP1正在构成装载问题
。
I am trying to load a .bmp image using it as a resource. But it is not loading properly. Rest assured about the bmp images, they are at the correct location. Now when I debug the program the MessageBox("Image not loaded properly") does not come but the MessageBox("SelectObject() failed") pops up. What's the problem with the image loading using it as a resource. The bitmap1 using fuLoad as LR_LOADFROMFILE is loading properly and displays the image. But the IDB_BITMAP1 is posing loading problems I suppose.
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#include "resource.h"
LRESULT CALLBACK WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
HBITMAP bitmap,bitmap1;
HINSTANCE hInst;
HWND hwnd;
HDC hdc;
int WINAPI wWinMain (HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
hInst=wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
RegisterClass (&wc);
// Create the window.
hwnd = CreateWindowEx (
0, // Optional window styles.
CLASS_NAME, // Window class
L"Learn to Program Windows", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if ( hwnd == NULL )
{
return 0;
}
ShowWindow (hwnd, nCmdShow);
// Run the message loop.
MSG msg = {};
while ( GetMessage (&msg, NULL, 0, 0) )
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return 0;
}
HDC image;
/*
void OpenImage ()
{
int x, y, xs, ys;
h = (HBITMAP)LoadImage (NULL, pszFilePath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
BITMAP q;
GetObject (reinterpret_cast<HGDIOBJ>(h), sizeof (BITMAP),
reinterpret_cast<LPVOID>(&q));
if ( gx > q.bmWidth ) { x = q.bmWidth; xs = (gx - x) / 2; }
else { x = 0.95*gx; xs = (gx - x) / 2; }
if ( gy > q.bmHeight ) { y = q.bmHeight; ys = (gy - y) / 2; }
else { y = 0.95*gy; ys = (gy - y) / 2; }
h = (HBITMAP)LoadImage (NULL, pszFilePath, IMAGE_BITMAP, x, y, LR_LOADFROMFILE);
if ( h == NULL )
{
MessageBox (hwnd, L"File not loaded properly", L"Error", MB_OK); DeleteObject (h); value = FALSE;
return;
}
hdcMem = CreateCompatibleDC (hdc);
if ( hdcMem == NULL )
{
MessageBox (hwnd, L"CreateCompatibleDC failed", L"Error", MB_OK); DeleteObject (hdcMem); value = FALSE;
return;
}
HBITMAP old = (HBITMAP)SelectObject (hdcMem, h);
if ( old == NULL )
{
MessageBox (hwnd, L"SelectObject failed", L"Error", MB_OK); DeleteObject (old); value = FALSE;
return;
}
BOOL j = BitBlt (hdc, xs, ys, x, y, hdcMem, 0, 0, SRCCOPY);
if ( !j )
{
MessageBox (hwnd, L"BitBlt failed", L"Error", MB_OK); value = FALSE;
return;
}
SelectObject (hdcMem, old);
DeleteObject (h);
DeleteObject (hdcMem);
value = FALSE;
}
*/
void draw ()
{
bitmap1 = (HBITMAP)LoadImage (NULL, L"Alok.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if ( bitmap1 == NULL )
{
MessageBox (hwnd, L"bitmap1 dosen't load properly", L"Faliure", MB_OK);
}
//bitmap = bitmap1;
BITMAP q;
GetObject (reinterpret_cast<HGDIOBJ>(bitmap), sizeof (BITMAP),
reinterpret_cast<LPVOID>(&q));
HDC hdcMem = CreateCompatibleDC (hdc);
if ( hdcMem == NULL )
{
MessageBox (hwnd, L"CreateCompatibleDC() failed", L"Faliure", MB_OK);
}
HBITMAP bold = (HBITMAP)SelectObject (hdcMem, bitmap);
if ( bold == NULL )
{
MessageBox (hwnd, L"SelectObject() failed", L"Faliure", MB_OK);
}
BOOL g = BitBlt (hdc, 0, 0, q.bmWidth,q.bmHeight, hdcMem, 0, 0, SRCCOPY);
if ( !g )
{
MessageBox (hwnd, L"BitBlt() failed", L"Faliure", MB_OK);
}
}
LRESULT CALLBACK WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch ( uMsg )
{
case WM_DESTROY:
PostQuitMessage (0);
return 0;
case WM_CREATE:
{
bitmap = (HBITMAP)LoadImage (hInst, MAKEINTRESOURCE (IDB_BITMAP1), IMAGE_BITMAP, 400, 400, LR_DEFAULTCOLOR);
//bitmap = (HBITMAP)LoadImage (hInstance, MAKEINTRESOURCE (IDB_BITMAP1), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
if ( bitmap == NULL )
{
MessageBox (hwnd, L"Failed to load image", L"Error", MB_OK);
}
wchar_t ch[10];
BITMAP q;
GetObject (reinterpret_cast<HGDIOBJ>(bitmap), sizeof (BITMAP),
reinterpret_cast<LPVOID>(&q));
_itow (q.bmHeight, ch, 10);
MessageBox (hwnd, ch, L"Height", MB_OK);
_itow (q.bmWidth, ch, 10);
MessageBox (hwnd, ch, L"Width", MB_OK);
}
case WM_PAINT:
{
PAINTSTRUCT ps;
hdc = BeginPaint (hwnd, &ps);
FillRect (hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
draw ();
EndPaint (hwnd, &ps);
}
return 0;
}
return DefWindowProc (hwnd, uMsg, wParam, lParam);
}
请告诉我如何使用gdi加载.jpg图像。我在网上搜索都是徒劳的,但却找不到解决办法。如果你知道的话请提示。
Please also tell me how to load a .jpg image using gdi. I had searched all over the web in vain but come up with no solution. Please drop a hint if you know.
Saurav yadav
Saurav yadav
推荐答案
这篇关于图像无法加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!