1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
HBITMAP CRotate::SimpleCopie(HDC hDC, HBITMAP hSourceBitmap)
{
/*
HDC hdcSrc = CreateCompatibleDC(NULL);
HDC hdcDst = CreateCompatibleDC(NULL);
HBITMAP hbmOld, hbmOld2, hbmNew;
BITMAP bm;
GetObject(hSourceBitmap, sizeof(bm), &bm);
hbmOld = (HBITMAP) SelectObject(hdcSrc, hSourceBitmap);
hbmNew = CreateBitmap( bm.bmWidth, bm.bmHeight, bm.bmPlanes, bm.bmBitsPixel,NULL);
hbmOld2 = (HBITMAP) SelectObject(hdcDst, hbmNew);
BitBlt(hdcDst, 0, 0, bm.bmWidth, bm.bmHeight, hdcSrc, 0, 0, SRCCOPY);
SelectObject(hdcSrc, hbmOld);
DeleteDC(hdcSrc);
DeleteDC(hdcDst);
return hbmNew;
*/
//2. Getting bimap size.
BITMAP bm;
GetObject(hSourceBitmap, sizeof(BITMAP), &bm);
//3. Creating new bitmap and receive pointer to it's bits.
HBITMAP hTargetBitmap;
void *pBuffer;
//3.1 Initilize DIBINFO structure
BITMAPINFO bi = {0};
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biClrImportant = 0;
bi.bmiHeader.biClrUsed = 0;
bi.bmiHeader.biCompression = 0;
bi.bmiHeader.biHeight = bm.bmHeight;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);;
bi.bmiHeader.biSizeImage = bm.bmWidth*bm.bmHeight*3;
bi.bmiHeader.biWidth = bm.bmWidth;
bi.bmiHeader.biXPelsPerMeter = 3780;
bi.bmiHeader.biYPelsPerMeter = 3780;
bi.bmiColors[0].rgbBlue = 0;
bi.bmiColors[0].rgbGreen = 0;
bi.bmiColors[0].rgbRed = 0;
bi.bmiColors[0].rgbReserved = 0;
//3.2 Create bitmap and receive pointer to points into pBuffer
hTargetBitmap = CreateDIBSection(
hDC,
&bi,
DIB_RGB_COLORS,
(void**)&pBuffer,
NULL,
0);
//4. Copy source bitmap into the target bitmap.
//4.1 Create 2 device contexts
HDC memDc = CreateCompatibleDC(NULL);
HDC targetDc = CreateCompatibleDC(NULL);
//4.2 Select source bitmap into one DC, target into another
HBITMAP hOldBitmap1 = (HBITMAP)::SelectObject(memDc, hSourceBitmap);
HBITMAP hOldBitmap2 = (HBITMAP)::SelectObject(targetDc, hTargetBitmap);
//4.3 Copy source bitmap into the target one
BitBlt(memDc,0, 0, bm.bmWidth, bm.bmHeight, memDc, 0, 0, SRCCOPY);
//4.4 Restore device contexts
::SelectObject(memDc, hOldBitmap1);
::SelectObject(targetDc, hOldBitmap2);
DeleteDC(memDc);
DeleteDC(targetDc);
//Here we can bitmap bits: pBuffer. Note:
// 1. pBuffer contains 3 bytes per point
// 2. Lines ane from the bottom to the top!
// 3. Points in the line are from the left to the right
// 4. Bytes in one point are BGR (blue, green, red) not RGB
// 5. Don't delete pBuffer, it will be automatically deleted
// when delete hTargetBitmap
//DeleteObject(hSourceBitmap);
//DeleteObject(hTargetBitmap);
return hTargetBitmap;
} |
Partager