【问题标题】:windows cannot load bitmap imagewindows无法加载位图图像
【发布时间】:2013-04-03 19:38:23
【问题描述】:

这个程序只是显示一个空白窗口。

我想将位图图像随机加载到屏幕上。我无法弄清楚这一点。即使我在这个程序中为图像指定不同的名称,程序仍然可以正常执行并显示空白窗口。

请帮帮我!

#ifndef UNICODE
#define UNICODE
#endif

/*have used unicode */

#include<Windows.h>
#include<iostream>
#include<time.h>
using namespace std;

bool gameover =FALSE;
const wchar_t classname[] = L"My first procedure and main";
HWND window;
HDC device;
LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam);


ATOM MyRegisterClass(HINSTANCE hInstance)
{
    //set the new windows properties 
    WNDCLASS wc = {};

    wc.lpfnWndProc=(WNDPROC)WindowProc;
    wc.hInstance=hInstance;
    wc.style=CS_HREDRAW|CS_VREDRAW;
    wc.hbrBackground=(HBRUSH)GetStockObject(COLOR_WINDOW +1);
    wc.lpszClassName=classname;

    return RegisterClass(&wc);
}

bool InitInstance(HINSTANCE hInstance,int nCmdShow)
{ 
    //Create a new window
    window=CreateWindowEx(0,
                          classname,
                          L"My Program",
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
                          NULL,
                          NULL,
                          hInstance,
                          NULL );

    //Any error in creating the window ?
    if(window==0)
        return 0;

    //Display the window 
    ShowWindow(window,nCmdShow);
    UpdateWindow(window);
    device=GetDC(window);
    return 1;
}

bool Game_Init()
{
    srand(time(NULL));
    return 1;
}

// This function handles the loading of bitmap image into the window
void DrawBitmap( char *filename,int x,int y)
{
    //Load the bitmap image
    HBITMAP image=(HBITMAP)LoadImage(0,L"image.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
    BITMAP bm;
    GetObject(image,sizeof(BITMAP),&bm);
    HDC hdcdevice=CreateCompatibleDC(device);
    SelectObject(hdcdevice,image);
    BitBlt(device,x,y,bm.bmWidth,bm.bmHeight,hdcdevice,0,0,SRCCOPY);

    //delete the device context and bitmap
    DeleteDC(hdcdevice);
    DeleteObject((HBITMAP)image);
}

void Game_Run()
{
    if(gameover==true)
        return;
    RECT rect;
    GetClientRect(window,&rect);

    //draw bitmap at random location
    int x=rand() % (rect.right-rect.left);
    int y=rand() %(rect.bottom-rect.top);

    DrawBitmap("image.bmp",x,y);
}

void Game_End()
{
    //Free the device
    ReleaseDC(window,device);
}

int WINAPI wWinMain(HINSTANCE hInstance,HINSTANCE,PWSTR PCmdLine,int nCmdShow)
{   
    //Register the class    
    MyRegisterClass(hInstance);

    //Initialize application
    if(!InitInstance(hInstance,nCmdShow))
        return 0;

    //Declare variables
    MSG msg ={ };

    //Initialize the game
    if(!Game_Init())
        return 0;

    //main message loop
    while(!gameover)
    {
        if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        Game_Run();
    }
    Game_End();

    return msg.wParam;
}

//This is window procedure function for handling closing of windows.
LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{   
    switch(uMsg) 
    { 
        case WM_CLOSE: 
            if(MessageBox(hwnd,L"Do you want to close this window",NULL,MB_OKCANCEL)==IDOK)           
                gameover=true;
            DestroyWindow(hwnd);


    }
    return DefWindowProc(hwnd,uMsg,wParam,lParam);
}

【问题讨论】:

  • 我也尝试将图像添加到调试文件夹,但它没有发生
  • 如果程序显示一个空白窗口,它正在执行。您的问题的标题令人困惑。
  • 要获得任何错误,您首先应该检查错误。或者,使用调试器并通过它检查所有返回值。

标签: c++ windows visual-studio-2010


【解决方案1】:

将 bmp 文件的完整路径传递给 LoadImage 函数。还要检查 LoadImage 函数的返回值。

例如。

HBITMAP image=(HBITMAP)LoadImage(0,L"c:\\wherever\\whatever\\image.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
if(image == NULL)
    MessageBox(0, "Couldn't load the image", "Error", MB_OK);
else
    .... Whatever else ......

几乎可以肯定,您的 LoadImage 无法找到“image.bmp”,因此返回 NULL。

【讨论】:

  • 弹出消息框显示无法加载图像。我也尝试包含路径但它不起作用。
  • @NaveenGabriel - 你能复制粘贴你的确切 LoadImage 调用和路径吗?
  • @NaveenGabriel - 你能在这里复制/粘贴代码行吗 - 你用图像的路径调用 LoadImage 吗?为了回答您的问题,MSVC 通常使用 0xcccccccc 初始化未初始化的指针
  • @user93353-HBITMAP image=(HBITMAP)LoadImage(0,L"A:\output\c++\GameLoop\Debug\image.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);跨度>
  • @NaveenGabriel - 将路径更改为 L"A:\\output\\c++\\GameLoop\\Debug\\image.bmp" - 这样就可以了。反斜杠必须被另一个反斜杠转义。也可以在任何答案中查看 - 我的路径中到处都有 2 个反斜杠。
【解决方案2】:

检查句柄的值,例如“设备”和“图像”。

尝试单步调试代码,看看是否一切正常。

您还可以选择使用 LoadBitmap()。

【讨论】:

  • 我使用的是 64 位 windows 8 操作系统。
  • 怎么知道是不是8位位图?
  • 在 Windows 中右键单击文件,然后单击属性,然后单击摘要。
  • 尝试不同的位图,看看你是否仍然得到 0xcccccccc
  • 我使用不同的位图仍然图像的值为0xcccccccc。
猜你喜欢
  • 2021-04-08
  • 2016-09-01
  • 2018-02-01
  • 1970-01-01
  • 2013-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-12
相关资源
最近更新 更多