c - 单击按钮打开图像文件到矩形

标签 c image winapi jpeg rectangles

如何在单击按钮时打开图像文件并将其放入矩形中?我使用winapi和c编程语言,请帮助并提前致谢

全局变量:

HDC                     hdc, hdcMem;
HBITMAP                 hLogo, hGambar;
OPENFILENAME            ofn = {0};
char szNamaFile[MAX_PATH]   = {0}

WM_CREATE:

HWND   btnLoad;

btnLoad     = CreateWindow("Button", "&Load", WS_CHILD | WS_TABSTOP | WS_VISIBLE | BS_FLAT, 149, 421, 70, 25, hWnd, (HMENU)IDB_LOAD, GetModuleHandle(NULL), NULL);
if(btnLoad == NULL)
{
   MessageBox(NULL, "Gagal Membuat Button!", "Error Uy!", MB_ICONEXCLAMATION);
   exit(EXIT_FAILURE);
}
hGambar = (HBITMAP)LoadImage(hInst, szNamaFile, IMAGE_BITMAP, 5, 5, LR_LOADFROMFILE);
SendMessage(btnLoad, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hGambar);

WM_命令:

if(HIWORD(wParam) != BN_CLICKED)
   break;
switch(LOWORD(wParam))
{
   case IDB_LOAD:
      BukaFormDialog(hWnd);
   break;
}
break;

WM_PAINT:

PAINTSTRUCT     ps ;

InvalidateRect (hWnd, NULL, TRUE);

RECT rctWindow;
GetClientRect(hWnd, &rctWindow);

RECT  rctPct;

rctPct.left         = 3;
rctPct.top          = rctKiri.top + 3;
rctPct.right        = rctKiri.right - 3;
rctPct.bottom       = rctKiri.bottom- 75;

hdc = BeginPaint(hWnd, &ps);

/*------------Picture Box--------------*/
Rectangle(hdc, rctPct.left, rctPct.top, rctPct.right, rctPct.bottom);
/*-------------------------------------*/

EndPaint(hWnd, &ps);
break;

程序:

char BukaFormDialog(HWND hWnd)
{  
    ofn.lStructSize     = sizeof(ofn);
    ofn.hwndOwner       = hWnd;
    ofn.lpstrFilter     = "File JPG (*.JPG)\0*.JPG\0File BMP (*.BMP)\0*.BMP\0File PNG(*.PNG)\0*.PNG\0All Files (*.*)\0*.*\0";
    ofn.lpstrFile       = szNamaFile;
    ofn.lpstrTitle      = "Pilih Gambar ...";
    ofn.nMaxFile        = MAX_PATH;
    ofn.Flags           = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;

    if(GetOpenFileName(&ofn))
    {
        hMem        = CreateCompatibleDC(hdc);
        SelectObject(hMem, hGambar);
        BitBlt(hdc, 4, 4, 50,50, hMem, 0, 0, SRCCOPY);
        DeleteDC(hMem);
        DeleteObject(hGambar);
    }
    else
        MessageBox(hWnd, "Gagal Membuka File", "Error", MB_ICONERROR | MB_OK);
    return (0);
}

最佳答案

这里有四个问题

1st) LoadImage(..,IMAGE_BITMAP,...) 仅支持 BMP 文件。

第二)您删除了错误的句柄 (hGambar)

if(GetOpenFileName(&ofn))
    {
        hMem        = CreateCompatibleDC(hdc);
        hGambar=SelectObject(hMem, hGambar); //save the original hBmp
        BitBlt(hdc, 4, 4, 50,50, hMem, 0, 0, SRCCOPY);
        hGambar=SelectObject(hMem, hGambar); // Select Back the original hBmp
        DeleteDC(hMem);
        DeleteObject(hGambar); // now hGambar is back delete it
    }

第三)你上面的绘图不是持久的,它会被任何重叠的窗口删除。将其移至 WM_PAINT

4th) 不要在 WM_PAINT 中调用 InvalidateRectInvalidateRgn

关于c - 单击按钮打开图像文件到矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33664773/

相关文章:

c# - 使用 ldap 照片的 ASN.1 包装器创建 G3 传真图像

javascript - 在 ReactJS 中显示来自 url 的图像

c++ - 使用 C++ CryptAPI :CNG Create CSR

c++ - 了解从 TranslateMessage() 发送的字符消息顺序

c++ - Windows 如何进行定时 GUI 动画?

C - 当负数字符(-128)始终被编码为其二进制的 2 补码时,其符号位在哪里?

c++ - 使用 CUDA 查找数组中未知大小区域的最大值

部分代码的CPU运行时间

c - jpg 格式不正确,混合了多个图像。谁能告诉我怎么了 ?“RECOVER”

html - 如何让多个图像自动调整大小并在 div 中保持居中