gpt4 book ai didi

C++从图像中获取对像素数组的引用

转载 作者:行者123 更新时间:2023-11-30 04:17:40 24 4
gpt4 key购买 nike

   void drawImage(HWND &hWnd,HBITMAP &hBitmap)
{
PAINTSTRUCT ps;
HDC hdc;
BITMAP bitmap;
HDC hdcMem;
HGDIOBJ oldBitmap;

RECT rec;
::GetClientRect(hWnd,&rec);

hdc = BeginPaint(hWnd, &ps);

hdcMem = CreateCompatibleDC(hdc);
oldBitmap = SelectObject(hdcMem, hBitmap);

GetObject(hBitmap, sizeof(bitmap), &bitmap);

int* x = (int*)bitmap.bmBits; ?? problem
x[0] = 0xff00ff;


//draw image so it fits entire window
StretchBlt(hdc, 0, 0, rec.right, rec.bottom, hdcMem, 0, 0,bitmap.bmWidth, bitmap.bmHeight, SRCCOPY);


SelectObject(hdcMem, oldBitmap);
DeleteDC(hdcMem);

EndPaint(hWnd, &ps);
}

在上面的代码中,我有一个简单的窗口,我在上面绘制了一个 BMP 图像。问题是我不知道如何获取对内部 int 数组的引用。我希望能够随意更改和操作图像中的像素。

我试过了:

int* x = (int*)bitmap.bmBits;
x[0] = 0xff00ff;

但是屏幕是空白的

最佳答案

你可以使用 GetBitmapBits或者更好,GetDIBits .

顺便说一句,您不必通过引用传递 HWNDHBITMAP,除非您想更改它们的值,我认为这不是案件。它们是数值(实际上是指针),因此您不会获得任何东西。

关于C++从图像中获取对像素数组的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16971770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com