UPDATE:CreateDIBSection圖形假象或如何獲取正確的位圖位
感謝Denis安尼西莫夫我通過更換解決我的問題:
for (int i = 0; i < Width * Height; i++)
{
pRgb[0] = RValue;
pRgb[1] = GValue;
pRgb[2] = BValue;
pRgb += 3;
}
有:
int BytesPerLine = Width * 3;
if (BytesPerLine % 4 != 0)
BytesPerLine += 4 - BytesPerLine % 4;
PBYTE Line = NULL;
for (int y = 0; y < Height; y++)
{
Line = pRgb;
for (int x = 0; x < Width; x++)
{
Line[0] = RValue;
Line[1] = GValue;
Line[2] = BValue;
Line += 3;
}
pRgb += BytesPerLine;
}
當創建設備無關位圖時,我有時會得到stra nge行文物。它只發生在當我改變窗口,我畫,寬度。在調整大小時,我刪除舊的位圖並使用自定義函數創建新的。因爲我可以直接訪問位圖的位,所以我直接改變它們的顏色,但這是問題發生的地方(我認爲),因爲FillRect完美地工作。我可能不正確訪問位指針。這裏有圖片時,它的工作原理和其他與文物:
這段代碼顯示我的窗口如何繪製:
case WM_PAINT:
{
PAINTSTRUCT ps;
BeginPaint(hWnd, &ps);
Painter Paint;
if (temp != NULL)
{
DeleteObject(temp);
temp = NULL;
}
temp = Paint.Create24BitBitmap(NULL, ps.rcPaint.right - ps.rcPaint.left - 5, ps.rcPaint.bottom - ps.rcPaint.top - 5, 20, 70, 0);
HDC MemoryDC = CreateCompatibleDC(ps.hdc);
SelectObject(MemoryDC, temp);
BitBlt(ps.hdc, ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right - ps.rcPaint.left - 5, ps.rcPaint.bottom - ps.rcPaint.top - 5, MemoryDC, 0, 0, SRCCOPY);
DeleteDC(MemoryDC);
EndPaint(hWnd, &ps);
}
break;
case WM_NCPAINT:
{
RedrawWindow(hWnd, NULL, NULL, RDW_ERASENOW | RDW_INVALIDATE | RDW_INTERNALPAINT);
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
break;
而這其中顯示瞭如何創建位圖:
HBITMAP Create24BitBitmap(HDC *hDC, int Width, int Height, BYTE RValue, BYTE GValue, BYTE BValue)
{
Width = ((Width < 1)?1:Width);
Height = ((Height < 1)?1:Height);
RValue = ((RValue > 254)?255:((RValue < 0)?0:RValue));
GValue = ((GValue > 254)?255:((GValue < 0)?0:GValue));
BValue = ((BValue > 254)?255:((BValue < 0)?0:BValue));
HDC MemoryDC = NULL;
if (hDC != NULL)
MemoryDC = CreateCompatibleDC(*hDC);
else
MemoryDC = CreateCompatibleDC(NULL);
BITMAPINFO BitmapInfo;
ZeroMemory(&BitmapInfo,sizeof(BITMAPINFO));
BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BitmapInfo.bmiHeader.biWidth = Width;
BitmapInfo.bmiHeader.biHeight = Height;
BitmapInfo.bmiHeader.biPlanes = 1;
BitmapInfo.bmiHeader.biBitCount = 24;
BitmapInfo.bmiHeader.biCompression = BI_RGB;
PBYTE pRgb = NULL;
HBITMAP hBitmap = CreateDIBSection(MemoryDC, &BitmapInfo, DIB_RGB_COLORS, (void**)&pRgb, NULL, NULL);
DeleteDC(MemoryDC);
if (hBitmap == NULL)
{
MessageBox(NULL, TEXT("\"Create24BitBitmap\" function failed."), TEXT("ERROR!"), MB_ICONERROR);
exit(EXIT_FAILURE);
}
for (int i = 0; i < Width * Height; i++)
{
pRgb[0] = RValue;
pRgb[1] = GValue;
pRgb[2] = BValue;
pRgb += 3;
}
return hBitmap;
}
所以問題是如何正確訪問位圖位?或者還有其他的東西可能會導致這個神器?
在這裏你可以找到編譯的程序: http://www.putlocker.com/file/D555A03E41032615
你的圖片是我不清楚,他們似乎都爲黑色。你可以用那些**清楚地說明問題的方法來更新它們嗎?謝謝。最好的祝福。 – AlwaysLearningNewStuff
夠公平的。將顏色更改爲更亮。 – FrogTheFrog
現在好多了,謝謝。現在讓我們看看可以做些什麼來幫助你! – AlwaysLearningNewStuff