2014-01-15 42 views
2

UPDATECreateDIBSection圖形假象或如何獲取正確的位圖位

感謝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完美地工作。我可能不正確訪問位指針。這裏有圖片時,它的工作原理和其他與文物:

enter image description here enter image description here

這段代碼顯示我的窗口如何繪製:

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

+1

你的圖片是我不清楚,他們似乎都爲黑色。你可以用那些**清楚地說明問題的方法來更新它們嗎?謝謝。最好的祝福。 – AlwaysLearningNewStuff

+0

夠公平的。將顏色更改爲更亮。 – FrogTheFrog

+0

現在好多了,謝謝。現在讓我們看看可以做些什麼來幫助你! – AlwaysLearningNewStuff

回答

6

這段代碼是錯誤的:

位圖的每一行必須對齊到4字節邊界的
for (int i = 0; i < Width * Height; i++) 
    { 
     pRgb[0] = RValue; 
     pRgb[1] = GValue; 
     pRgb[2] = BValue; 
     pRgb += 3; 
    } 

開始。

更新

你應該使用這樣的事情:

BytesPerLine := Width * 3; 
if BytesPerLine mod 4 <> 0 then 
    Inc(BytesPerLine, 4 - BytesPerLine mod 4); 
for y := 0 to Height - 1 do 
    begin 
    Line := pRgb; 
    for x := 0 to Width - 1 do 
     begin 
     Line[0] := RValue; 
     Line[1] := GValue; 
     Line[2] := BValue; 
     Inc(Line, 3); 
     end; 
    Inc(pRgb, BytesPerLine); 
    end; 
+0

所以猜對了,我訪問錯誤的地方,你能告訴我一些關於如何做到這一點的僞代碼? – FrogTheFrog

+1

@Helix您需要*兩個*循環,以便您可以跳過每行末尾的填充。附:非常有趣的是,C++問題在Pascal中得到了答案。 –

+1

@Mark Ransom我可以讀取C++,但我不能寫它:)另外Helix請求僞代碼,我認爲Pascal適合用作僞代碼。 –