win32 CreateCompatibleBitmap ja pildi skaleerimine

Win32 Createcompatiblebitmap



Funktsioon CreateCompatibleBitmap
Looge bittkaart, mis ühildub seadmega, mis vastab määratud seadme kontekstile.
loob seadmega ühilduva bittkaardi, mis on seotud määratud seadme kontekstiga.

HBITMAP CreateCompatibleBitmap( HDC hdc, int cx, int cy )

parameeter:

hdc
seadme kontekst.
cx
laius
cy
kõrgus



tagastusväärtus

Funktsiooni õnnestumisel on tagastusväärtuseks ühilduv bitikaardi käepide
tagastusväärtus on ühilduva bitikaardi (DDB) käepide.
Kui funktsioon ebaõnnestub, on tagastusväärtus NULL



Märkused

Funktsiooni CreateCompatibleBitmap loodud bitikaardi värvivorming on kooskõlas seadme värviformaadiga, mis vastab parameetrile hdc.
Bittkaardi saab valida mäluseadme alalisvoolu sisenemiseks, mis peab olema ühilduv HDD parameetrile vastava seadmega.
Kuna mäluseade DC toetab nii värvilist kui ühevärvilist bitkaarti,
Kui funktsiooni CreateCompatibleBitmap parameeter hdc määrab a Mäluseade DC millal,
Funktsiooni CreateCompatibleBitmap tagastatud bitikaardi värvivorming võib olla värviline või ühevärviline bitikaart.
Kui funktsiooni CreateCompatibleBitmap parameeter hdc määrab a Mäluseade DC millal,
Funktsiooni CreateCompatibleBitmap tagastatud bittkaardi värvivorming, värvipalett ja määratud eraldamise kontekst on ühtsed



Märge : Mäluseadme konteksti loomisel lähtestatakse see ja valitakse 1 x 1 ühevärviline bitikaart.
Kui seda mäluseadme konteksti kasutatakse rakenduses CreateCompatibleBitmap, luuakse tagastatud bittkaart mustvalge bittkaardina.
Kui soovite luua värvilist bitikaarti, kasutage HDC-d, mis loob mäluseadme konteksti. Vaadake allolevat koodi:

HDC memDC = CreateCompatibleDC ( hDC ) HBITMAP memBM = CreateCompatibleBitmap ( hDC, nWidth, nHeight ) SelectObject ( memDC, memBM )

Kui rakendus määrab nWidth või nHeight väärtuseks 0, tagastab CreateCompatibleBitmap 1x1 piksli mustvalge bitikaardi käepideme.

Kui rakenduse CreateCompatibleBitmap parameetrile hdc vastav deivce-kontekst, on bittkaart valitud juba varem ja bittkaart on funktsiooni CreateDIBSection loodud DIB-sektsioon.
Seejärel loob CreateCompatibleBitmap DIB jaotise.



Kui te ei vaja bitikaarti, kasutage selle kustutamiseks DeleteObject'i.

Näide: pildi skaleerimine

https://docs.microsoft.com/zh-cn/windows/desktop/gdi/scaling-an-image

Mõni rakendus suurendab või vähendab piltide kuvamist proportsionaalselt.
Näiteks pakub joonistamisrakendus suurendusfunktsiooni, mis võimaldab kasutajatel pilte pikslites vaadata ja muuta.

Rakendus kasutab pildi skaleerimiseks funktsiooni StretchBlt. See funktsioon on sarnane BitBlt-ga,
StretchBlt kopeerib bittkaardi andmed lähte-DC bittkaardilt siht-DC bittkaardile.
Erinevalt BitBltist suudab StretchBlt pilte suumida. Skaalamine põhineb sihtmärgi ja allika ristkülikute suurusel.
Kui lähte ristkülik on suurem kui sihtkoha ristkülik, väheneb saadud pilt. Kui lähte ristkülik on sihtkoha ristkülikust väiksem, suurendatakse saadud pilti.

Kui sihtkoha ristkülik on lähte ristkülikust väiksem, eemaldab StretchBlt pildilt mõned värviandmed.
Eemaldamismeetod on seotud määratud venitusrežiimiga. Vaadake allolevat tabelit:

Venitusrežiim Meetod
Mustvalge Teostab loogilise JA-toimingu kõrvaldatud pikslite värviandmetega ja ülejäänud pikslite värviandmetega.
VALGE must Teostab loogilise VÕI toimingu kõrvaldatud pikslite värviandmetega ja ülejäänud pikslite värviandmetega.
VÄRVIVÄRV Kõrvaldab kustutatud pikslite värviandmed täielikult.
HALFTONE Lähendab sihtkoha algseid (allika) värviandmeid.

Valige venitusrežiim funktsiooni SetStretchBltMode kaudu
Allpool toodud näidiskood pärineb rakendusest, mis demonstreerib kõiki nelja StretchBlt-funktsiooni toetavat venitusrežiimi.
Selle koodi kasutamine on järgmine:

vs2010 Looge Win32 rakendus: nimi on GDIBitmapScaling, ärge valige tühja projekti.
Pärast projekti loomist luuakse GDIBitmapScaling.cpp fail,

Asendage faili GDIBitmapScaling.cpp sisu järgmise koodiga:

// GDIBitmapScaling.cpp: Define the entry point of the application. // #include 'stdafx.h' #include 'GDIBitmapScaling.h' #include #include #define MAX_LOADSTRING 100 // Global Variables: HINSTANCE hInst // current instance TCHAR szTitle[MAX_LOADSTRING] // The title bar text TCHAR szWindowClass[MAX_LOADSTRING] // the main window class name // Forward declarations of functions included in this code module: ATOM MyRegisterClass(HINSTANCE hInstance) BOOL InitInstance(HINSTANCE, int) LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM) int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance) UNREFERENCED_PARAMETER(lpCmdLine) // TODO: Place code here. MSG msg HACCEL hAccelTable // Initialize global strings LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING) LoadString(hInstance, IDC_GDIBITMAPSCALING, szWindowClass, MAX_LOADSTRING) MyRegisterClass(hInstance) // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE } hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_GDIBITMAPSCALING)) // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg) DispatchMessage(&msg) } } return (int) msg.wParam } // // FUNCTION: MyRegisterClass() // // PURPOSE: Registers the window class. // // COMMENTS: // // This function and its usage are only necessary if you want this code // to be compatible with Win32 systems prior to the 'RegisterClassEx' // function that was added to Windows 95. It is important to call this function // so that the application will get 'well formed' small icons associated // with it. // ATOM MyRegisterClass(HINSTANCE hInstance) CS_VREDRAW wcex.lpfnWndProc = WndProc wcex.cbClsExtra = 0 wcex.cbWndExtra = 0 wcex.hInstance = hInstance wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_GDIBITMAPSCALING)) wcex.hCursor = LoadCursor(NULL, IDC_ARROW) wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1) wcex.lpszMenuName = MAKEINTRESOURCE(IDC_GDIBITMAPSCALING) wcex.lpszClassName = szWindowClass wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)) return RegisterClassEx(&wcex) // // FUNCTION: InitInstance(HINSTANCE, int) // // PURPOSE: Saves instance handle and creates main window // // COMMENTS: // // In this function, we save the instance handle in a global variable and // create and display the main program window. // #define NEW_DIB_FORMAT(lpbih) (lpbih->biSize != sizeof(BITMAPCOREHEADER)) BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd hInst = hInstance // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_SYSMENU, CW_USEDEFAULT, 0, 1024, 768, NULL, NULL, hInstance, NULL) if (!hWnd) { return FALSE } ShowWindow(hWnd, nCmdShow) UpdateWindow(hWnd) return TRUE } static HCURSOR hcurSave WORD DIBNumColors (LPVOID lpv) { INT bits LPBITMAPINFOHEADER lpbih = (LPBITMAPINFOHEADER)lpv LPBITMAPCOREHEADER lpbch = (LPBITMAPCOREHEADER)lpv /* With the BITMAPINFO format headers, the size of the palette * is in biClrUsed, whereas in the BITMAPCORE - style headers, it * is dependent on the bits per pixel ( = 2 raised to the power of * bits/pixel). */ if (NEW_DIB_FORMAT(lpbih)) { if (lpbih->biClrUsed != 0) return (WORD)lpbih->biClrUsed bits = lpbih->biBitCount } else bits = lpbch->bcBitCount if (bits > 8) return 0 /* Since biClrUsed is 0, we dont have a an optimal palette */ else return (1 << bits) } /* Macro to determine to round off the given value to the closest byte */ #define WIDTHBYTES(i) ((((i)+31) >> 5) << 2) /****************************************************************************** * * * FUNCTION : DIBInfo(HANDLE hbi, LPBITMAPINFOHEADER lpbih) * * * * PURPOSE : Retrieves the DIB info associated with a CF_DIB * * format memory block. * * * * RETURNS : TRUE - if successful. * * FALSE - otherwise * * * *****************************************************************************/ BOOL DIBInfo (HANDLE hbi, LPBITMAPINFOHEADER lpbih) { if (hbi){ *lpbih = *(LPBITMAPINFOHEADER)hbi /* fill in the default fields */ if (NEW_DIB_FORMAT(lpbih)) { if (lpbih->biSizeImage == 0L) lpbih->biSizeImage = WIDTHBYTES(lpbih->biWidth*lpbih->biBitCount) * lpbih->biHeight if (lpbih->biClrUsed == 0L) lpbih->biClrUsed = DIBNumColors (lpbih) } return TRUE } return FALSE } /* flags for mmioSeek() */ #ifndef SEEK_SET #define SEEK_SET 0 /* seek to an absolute position */ #define SEEK_CUR 1 /* seek relative to current position */ #define SEEK_END 2 /* seek relative to end of file */ #endif /* ifndef SEEK_SET */ VOID ReadPackedFileHeader(HFILE hFile, LPBITMAPFILEHEADER lpbmfhdr, LPDWORD lpdwOffset) { *lpdwOffset = _llseek(hFile, 0L, (UINT) SEEK_CUR) _hread(hFile, (LPSTR) &lpbmfhdr->bfType, sizeof(WORD)) /* read in bfType*/ _hread(hFile, (LPSTR) &lpbmfhdr->bfSize, sizeof(DWORD) * 3) /* read in last 3 dwords*/ } /* macro to determine if resource is a DIB */ #define ISDIB(bft) ((bft) == BFT_BITMAP) /* Header signatutes for various resources */ #define BFT_ICON 0x4349 /* 'IC' */ #define BFT_BITMAP 0x4d42 /* 'BM' */ #define BFT_CURSOR 0x5450 /* 'PT' */ HANDLE ReadDIBBitmapInfo (INT hFile) { DWORD dwOffset HANDLE hbi = NULL INT size INT i WORD nNumColors LPRGBQUAD lprgbq BITMAPINFOHEADER bih BITMAPCOREHEADER bch LPBITMAPINFOHEADER lpbih BITMAPFILEHEADER bf DWORD dwDWMasks= 0 DWORD dwWidth = 0 DWORD dwHeight = 0 WORD wPlanes, wBitCount if (hFile == HFILE_ERROR) return NULL /* Read the bitmap file header */ ReadPackedFileHeader(hFile, &bf, &dwOffset) /* Do we have a RC HEADER? */ if (!ISDIB (bf.bfType)) { bf.bfOffBits = 0L _llseek(hFile, dwOffset, (UINT)SEEK_SET) /* seek back to beginning of file */ } if (sizeof(bih) != _hread(hFile, (LPSTR)&bih, (UINT)sizeof(bih))) return FALSE nNumColors = DIBNumColors (&bih) /* Check the nature (BITMAPINFO or BITMAPCORE) of the info. block * and extract the field information accordingly. If a BITMAPCOREHEADER, * transfer it's field information to a BITMAPINFOHEADER-style block */ switch (size = (INT)bih.biSize){ case sizeof (BITMAPINFOHEADER): break case sizeof (BITMAPCOREHEADER): bch = *(LPBITMAPCOREHEADER)&bih dwWidth = (DWORD)bch.bcWidth dwHeight = (DWORD)bch.bcHeight wPlanes = bch.bcPlanes wBitCount = bch.bcBitCount bih.biSize = sizeof(BITMAPINFOHEADER) bih.biWidth = dwWidth bih.biHeight = dwHeight bih.biPlanes = wPlanes bih.biBitCount = wBitCount bih.biCompression = BI_RGB bih.biSizeImage = 0 bih.biXPelsPerMeter = 0 bih.biYPelsPerMeter = 0 bih.biClrUsed = nNumColors bih.biClrImportant = nNumColors _llseek(hFile, (LONG)sizeof (BITMAPCOREHEADER) - sizeof (BITMAPINFOHEADER), (UINT)SEEK_CUR) break default: /* Not a DIB! */ return NULL } /* Fill in some default values if they are zero */ if (bih.biSizeImage == 0){ bih.biSizeImage = WIDTHBYTES((DWORD)bih.biWidth * bih.biBitCount) * bih.biHeight } if (bih.biClrUsed == 0) bih.biClrUsed = DIBNumColors(&bih) /* Allocate for the BITMAPINFO structure and the color table. */ if ((bih.biBitCount == 16) || (bih.biBitCount == 32)) dwDWMasks = sizeof(DWORD) * 3 hbi = GlobalAlloc (GPTR, (LONG)bih.biSize + nNumColors * sizeof(RGBQUAD) + dwDWMasks) if (!hbi) return NULL lpbih = (LPBITMAPINFOHEADER)hbi *lpbih = bih /* Get a pointer to the color table */ lprgbq = (LPRGBQUAD)((LPSTR)lpbih + bih.biSize) if (nNumColors){ if (size == sizeof(BITMAPCOREHEADER)){ /* Convert a old color table (3 byte RGBTRIPLEs) to a new * color table (4 byte RGBQUADs) */ _hread(hFile, (LPSTR)lprgbq, (UINT)nNumColors * sizeof(RGBTRIPLE)) for (i = nNumColors - 1 i >= 0 i--){ RGBQUAD rgbq rgbq.rgbRed = ((RGBTRIPLE*)lprgbq)[i].rgbtRed rgbq.rgbBlue = ((RGBTRIPLE*)lprgbq)[i].rgbtBlue rgbq.rgbGreen = ((RGBTRIPLE*)lprgbq)[i].rgbtGreen rgbq.rgbReserved = (BYTE)0 lprgbq[i] = rgbq } } else _hread(hFile, (LPSTR)lprgbq, (UINT)nNumColors * sizeof(RGBQUAD)) } else if (dwDWMasks) _hread(hFile, (LPSTR)lprgbq, dwDWMasks) if (bf.bfOffBits != 0L){ _llseek(hFile, dwOffset + bf.bfOffBits, (UINT)SEEK_SET) } return hbi } HGLOBAL GlobalFreeDIB(HGLOBAL hDIB) { LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)hDIB if (!lpbi->biClrImportant) return GlobalFree(hDIB) if (GlobalFlags((HGLOBAL)lpbi->biClrImportant) == GMEM_INVALID_HANDLE) { SetLastError(0) return GlobalFree(hDIB) } else return GlobalFree((HANDLE)lpbi->biClrImportant) } /****************************************************************************** * * * FUNCTION : ColorTableSize(LPVOID lpv) * * * * PURPOSE : Calculates the palette size in bytes. If the info. block * * is of the BITMAPCOREHEADER type, the number of colors is * * multiplied by 3 to give the palette size, otherwise the * * number of colors is multiplied by 4. * * * * RETURNS : Color table size in number of bytes. * * * *****************************************************************************/ WORD ColorTableSize (LPVOID lpv) { LPBITMAPINFOHEADER lpbih = (LPBITMAPINFOHEADER)lpv if (NEW_DIB_FORMAT(lpbih)) { if (((LPBITMAPINFOHEADER)(lpbih))->biCompression == BI_BITFIELDS) /* Remember that 16/32bpp dibs can still have a color table */ return (sizeof(DWORD) * 3) + (DIBNumColors (lpbih) * sizeof (RGBQUAD)) else return (DIBNumColors (lpbih) * sizeof (RGBQUAD)) } else return (DIBNumColors (lpbih) * sizeof (RGBTRIPLE)) } /****************************************************************************** * * * FUNCTION :OpenDIB(LPSTR szFilename) * * * * PURPOSE :Open a DIB file and create a memory DIB -- a memory handle * * containing BITMAPINFO, palette data and the bits. * * * * RETURNS :A handle to the DIB. * * * *****************************************************************************/ HANDLE OpenDIB (LPSTR szFilename) { HFILE hFile BITMAPINFOHEADER bih LPBITMAPINFOHEADER lpbih DWORD dwLen = 0 DWORD dwBits HANDLE hDIB HANDLE hMem OFSTRUCT of /* Open the file and read the DIB information */ hFile = OpenFile(szFilename, &of, (UINT)OF_READ) if (hFile == HFILE_ERROR) return NULL hDIB = ReadDIBBitmapInfo(hFile) if (!hDIB) return NULL DIBInfo(hDIB, &bih) /* Calculate the memory needed to hold the DIB */ dwBits = bih.biSizeImage dwLen = bih.biSize + (DWORD)ColorTableSize (&bih) + dwBits /* Try to increase the size of the bitmap info. buffer to hold the DIB */ hMem = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE) if (!hMem){ GlobalFreeDIB(hDIB) hDIB = NULL } else hDIB = hMem /* Read in the bits */ if (hDIB){ lpbih = (LPBITMAPINFOHEADER)hDIB _hread(hFile, (LPSTR)lpbih + (WORD)lpbih->biSize + ColorTableSize(lpbih), dwBits) } _lclose(hFile) return hDIB }/* Macros to display/remove hourglass cursor for lengthy operations */ #define StartWait() hcurSave = SetCursor(LoadCursor(NULL, IDC_WAIT)) #define EndWait() SetCursor(hcurSave) /****************************************************************************** * * * FUNCTION : BitmapFromDIB(HANDLE hDIB, HPALETTE hPal) * * * * PURPOSE : Will create a DDB (Device Dependent Bitmap) given a global * * handle to a memory block in CF_DIB format * * * * RETURNS : A handle to the DDB. * * * *****************************************************************************/ HBITMAP BitmapFromDIB (HANDLE hDIB, HPALETTE hPal) { LPBITMAPINFOHEADER lpbih HPALETTE hPalOld HDC hDC HBITMAP hBitmap StartWait() if (!hDIB) return NULL lpbih = (LPBITMAPINFOHEADER)hDIB if (!lpbih) return NULL hDC = GetDC(NULL) if (hPal){ hPalOld = SelectPalette(hDC, hPal, FALSE) RealizePalette(hDC) } hBitmap = CreateDIBitmap(hDC, lpbih, CBM_INIT, (LPSTR)lpbih + lpbih->biSize + ColorTableSize(lpbih), (LPBITMAPINFO)lpbih, DIB_RGB_COLORS ) if (hPal) SelectPalette(hDC, hPalOld, FALSE) ReleaseDC(NULL, hDC) EndWait() return hBitmap } /****************************************************************************** * * * FUNCTION : DrawBitmap(HDC hDC, int x, int y, * * HBITMAP hBitmap, DWORD dwROP) * * * * PURPOSE : Draws bitmap at the specified position in DC * * * * RETURNS : Return value of BitBlt() * * * *****************************************************************************/ BOOL DrawBitmap (HDC hDC, INT x, INT y, HBITMAP hBitmap, DWORD dwROP) !hBitmap) return FALSE hDCBits = CreateCompatibleDC(hDC) GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap) SelectObject(hDCBits, hBitmap) bResult = BitBlt(hDC, x, y, Bitmap.bmWidth, Bitmap.bmHeight, hDCBits, 0, 0, dwROP) DeleteDC(hDCBits) return bResult /*FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) PURPOSE: Processes messages for the main window. WM_COMMAND - process the application menu WM_PAINT - Paint the main window WM_DESTROY - post a quit message and return*/ #define ID_LOADBITMAP 1 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent PAINTSTRUCT ps //Handle to a GDI device context HDC hDC //Handle to a DDB(device-dependent bitmap) HBITMAP hBitmap HFONT hFont NONCLIENTMETRICS ncm={0} ncm.cbSize= sizeof(NONCLIENTMETRICS) static HWND hwndButton static HWND hwndButtonExit static HANDLE hDIB = NULL char szDirName[MAX_PATH] char szFilename[MAX_PATH]='' char szBitmapName[MAX_PATH]='\Waterfall.bmp' //char szBitmapName[MAX_PATH]='\tulips256.bmp' OPENFILENAMEA ofn switch (message) { case WM_CREATE: //Creates a font from the current theme's caption font SystemParametersInfo(SPI_GETNONCLIENTMETRICS, NULL, &ncm, NULL) hFont = CreateFontIndirect(&ncm.lfCaptionFont) //Gets the device context for the current window hDC = GetDC(hWnd) //Gets the directory of the current project and loads Waterfall.bmp GetCurrentDirectoryA(MAX_PATH, szDirName) strcat_s(szDirName, szBitmapName) strcat_s(szFilename,szDirName) hDIB = OpenDIB(szFilename) hBitmap = BitmapFromDIB(hDIB, NULL) //Draws Waterfall.bmp as a device dependent bitmap DrawBitmap(hDC,0,0,hBitmap,SRCCOPY) InvalidateRect(hWnd, NULL, FALSE) ReleaseDC(hWnd,hDC) //Draws the 'Load Bitmap' button hwndButton = CreateWindowW(TEXT('button'),TEXT('Load Bitmap'), WS_CHILD | WS_VISIBLE | WS_BORDER, 600, 200, 150,50, hWnd, (HMENU)ID_LOADBITMAP, ((LPCREATESTRUCT) lParam)-> hInstance, NULL) //Set the font of the button to the theme's caption font SendMessage(hwndButton, WM_SETFONT, (WPARAM)hFont, TRUE ) return 0 case WM_COMMAND: wmId = LOWORD(wParam) wmEvent = HIWORD(wParam) //if Load Bitmap button is pressed if (wmId == ID_LOADBITMAP && wmEvent == BN_CLICKED) { //Get the current directory name, and store in szDirName GetCurrentDirectoryA(MAX_PATH, szDirName) //Set all structure members to zero. ZeroMemory(&ofn, sizeof(OPENFILENAMEA)) //Initializing the OPENFILENAMEA structure ofn.lStructSize = sizeof(OPENFILENAMEA) ofn.hwndOwner = hWnd ofn.lpstrFilter ='BMP Files (*.BMP) *.BMP' ofn.lpstrFile = szFilename ofn.nMaxFile = sizeof(szFilename) ofn.lpstrDefExt = 'BMP' ofn.lpstrInitialDir = szDirName ofn.Flags = OFN_EXPLORER | OFN_SHOWHELP | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST if (GetOpenFileNameA(&ofn)) { hDIB = OpenDIB((LPSTR)szFilename) if (!hDIB) MessageBox(hWnd, TEXT('Unable to load file!'), TEXT('Oops'), MB_ICONSTOP) } else { if (strlen((const char *)szFilename) != 0) MessageBox(hWnd, TEXT('Unable to load file!'), TEXT('Oops'), MB_ICONSTOP) return 0 } InvalidateRect(hWnd, NULL, FALSE) } break case WM_PAINT: { //Initializing arrays for boxes POINT pRect1[5] = {{0,0},{400,0},{400,400},{0,400},{0,0}} POINT pRect2[5] = {{0,500}, {200, 500}, {200, 700}, {0,700},{0,500}} POINT pRect3[5] = {{210,500}, {410, 500}, {410, 700}, {210,700},{210,500}} POINT pRect4[5] = {{420,500}, {620, 500}, {620, 700}, {420,700},{420,500}} POINT pRect5[5] = {{630,500}, {830, 500}, {830, 700}, {630,700},{630,500}} //For the white background RECT clientRect HRGN hRegion1 HRGN hRegion2 HRGN hRegion3 HBRUSH hBGBrush //Handle to a logical font HFONT hFont //Get the caption font that is currently in use SystemParametersInfo(SPI_GETNONCLIENTMETRICS, NULL, &ncm, NULL) hFont = CreateFontIndirect(&ncm.lfCaptionFont) //Begin drawing hDC = BeginPaint(hWnd, &ps) //Draw and fill rectangles for the background GetClientRect(hWnd, &clientRect) hRegion1 = CreateRectRgn(clientRect.left,clientRect.top,clientRect.right,clientRect.bottom) hBGBrush = CreateSolidBrush(RGB(255,255,255)) FillRgn(hDC, hRegion1, hBGBrush) //Create an HBITMAP(device dependent bitmap) to be drawn hBitmap = BitmapFromDIB(hDIB,NULL) //Draw the DDB DrawBitmap(hDC,0,0,hBitmap,SRCCOPY) if(hDIB) { hRegion2 = CreateRectRgn(401,0,clientRect.right,401) hRegion3 = CreateRectRgn(0,401,clientRect.right,clientRect.bottom) FillRgn(hDC,hRegion2,hBGBrush) FillRgn(hDC,hRegion3,hBGBrush) //Set stretch mode as BLACKONWHITE and then copy from the //source rectangle into the smaller rectangle SetStretchBltMode(hDC,BLACKONWHITE) StretchBlt(hDC,0,500,200,200,hDC, 0,0,400,400,SRCCOPY) //Set stretch mode as WHITEONBLACK and then copy from the //source rectangle into the smaller rectangle SetStretchBltMode(hDC,WHITEONBLACK) StretchBlt(hDC,210,500,200,200, hDC, 0,0,400,400, SRCCOPY) //Set stretch mode as COLORONCOLOR and then copy from the //source rectangle into the smaller rectangle SetStretchBltMode(hDC,COLORONCOLOR) StretchBlt(hDC,420,500,200,200, hDC, 0,0,400,400, SRCCOPY) //Set stretch mode as HALFTONE and then copy from the //source rectangle into the smaller rectangle SetStretchBltMode(hDC,HALFTONE) StretchBlt(hDC,630,500,200,200, hDC, 0,0,400,400, SRCCOPY) } //Select the caption font created earlier SelectObject(hDC,hFont) //Create captions for each demonstration of color loss modes TextOut(hDC,50,480,TEXT('BLACKONWHITE'),12) TextOut(hDC,250,480,TEXT('WHITEONBLACK'),12) TextOut(hDC,460,480,TEXT('COLORONCOLOR'),12) TextOut(hDC,680,480,TEXT('HALFTONE'),8) DeleteObject(hFont) //Selecting the stock object pen to draw with SelectObject(hDC, GetStockObject(DC_PEN)) //The pen is gray SetDCPenColor(hDC, RGB(80,80,80)) //Polylines are drawn from arrays of POINTs Polyline(hDC, pRect1, 5) Polyline(hDC, pRect2, 5) Polyline(hDC, pRect3, 5) Polyline(hDC, pRect4, 5) Polyline(hDC, pRect5, 5) FillRgn(hDC,hRegion2,hBGBrush) EndPaint(hWnd, &ps) break } case WM_DESTROY: PostQuitMessage(0) break default: return DefWindowProc(hWnd, message, wParam, lParam) } return 0 }

Ülaltoodud kood, töötav efekt on järgmine:
pilt

Kogu insenerikood on järgmine:
https://download.csdn.net/download/wowocpp/10503082

Ülaltoodud koodil on MFC-d selle rakendamiseks tasuta kasutada. HFILE OpenFile pole samuti soovitatav. Soovitatav on CreateFile

Bitmap funktsioonid
https://docs.microsoft.com/zh-cn/windows/desktop/gdi/bitmap-functions
Bitmapsi kasutamine
https://docs.microsoft.com/zh-cn/windows/desktop/gdi/using-bitmaps
Pildi jäädvustamine
Pildi skaleerimine
Pildi salvestamine
https://docs.microsoft.com/zh-cn/windows/desktop/gdi/storing-an-image

Alfa bittkaardi segamine
Varjutatud ristküliku joonistamine
Varjutatud kolmnurga joonistamine
Printeri testimine JPEG- või PNG-toe jaoks
JPEG- või PNG-pildi suuruse muutmine

https://msdn.microsoft.com/zh-cn/ff0a5ae3-ae2e-4417-b5e5-0f9871c03964
Bitmaps
Bittkaart on graafiline objekt, mida kasutatakse piltide loomiseks, käsitsemiseks (skaleerimiseks, kerimiseks, pööramiseks ja värvimiseks) ning failide salvestamiseks kettale. Selles ülevaates kirjeldatakse bitikaardiklassid ja bitikaardi toimingud.

Bitmapsi kohta
Bitmapsi kasutamine
Bittkaardi viide

Loo CreateCompatibleDC ja CreateCompatibleBitmap kokkuvõte

https://www.cnblogs.com/open-fu/p/3337810.html
CreateCompatibleDC kasutatakse tavaliselt siis, kui kasutatakse CreateCompatibleBitmap. Pigem ei ole CreateCompatibleDC eesmärk luua CreateCompatibleBitmapile, pigem tuleb luua mäluseadmete keskkond puhvrina joonistustoimingute ja kuvamisseadmete vahel ning CreateCompatibleBitmap on laiendada mäluseadmete keskkonna pildiruumi

CreateComptibleDC parameetrid mfc ja sdk programmeerimises on erinevad, esimene on CDC *, teine ​​hdc (tavaliselt on enamik jooniseid sdk-s hdc ja mfc on cdc *), kuid funktsioonid on samad, kõik seadme loomiseks ja seade Mäluseadme keskkond, mis ühildub keskkonna kontekstiga (võib mõista sama seadet, mis seade), kuid tasub teada, et sel viisil loodud mäluseadme keskkonna pildiruumi suurus on väga väike, tavaliselt 1 * 1 piksli suurus ja ikkagi ühevärviline (ma ei tea, mida teha, võib-olla eelmiste seadmete või programmidega ühildumiseks), seega peate bittkaardi bittkaardi laadimiseks helistama funktsioonile SelectObject, nii et laaditud bittkaardi suurus on samaväärne mäluseadme keskkonna suurusega. Alles seejärel saab teha rea ​​joonistamistoiminguid.

Funktsioon CreateCompatibleBitmap on seadmega ühilduva bittkaardi loomine, mis on seotud määratud seadmekeskkonnaga. Mõnikord tunnen, et pean lihtsalt bitikaardi ressursi otse ressursist laadima, miks ma selle otse loon? Kuid mõnikord on see vajalik näiteks siis, kui soovite ekraani otse juhtida ja soovite vältida värelust. Sel ajal saate luua DC-mälu, kasutada bittikaardi loomiseks funktsiooni CreateCompatibleBitmap ja seejärel kasutab DC-mälu bittikaardi laadimiseks SelectObjecti, nii et mälus on kindla suurusega pildimaht, ja seejärel kopeerimiseks kasutada bitblt ekraani mällu DC, nii et mälu alalisvoolul saab teha erinevaid joonistamistoiminguid. Muidugi saate seda kasutada ka mälupuhvris pildi toimingu lõpuleviimiseks (näiteks suumimine, läbipaistvus jne), samuti on vaja luua bitikaart

Lisaks näete:

http://blog.csdn.net/hhygcy/article/details/4073975 See on väga üksikasjalik ~

http://fengqing888.blog.163.com/blog/static/3301141620091019104353119/ Võite lihtsalt vaadata esimest poolaega, kuid teine ​​pool pole nii atraktiivne ~

:

1. For the initial color of CreateCompatibleBitmap, let me practice, the screen shows black 2. (There is such a sentence in the second link, no modification) The color format of the bitmap created by the CreateCompatibleBitmap function matches the color format of the device identified by the parameter hdc. The bitmap can be selected into any memory device environment compatible with the original device. The memory device environment allows both color and monochrome bitmaps. Therefore, when the specified device environment is a memory device environment, the bitmap format returned by the CreateCompatibleBitmap function may not be the same. However, compatible bitmaps created for non-memory device environments usually have the same color format, and using the same color palette as the specified device environment is actually not too clear. The hdc that can be selected in the CreateCompatilbeBitmap parameter can be the memory device environment Or the physical device environment, and the selected hdc will determine the color format of the bitmap you create. If you choose the memory environment, if it is monochrome, the bitmap created is also monochrome if it is color, it is also color. Select the physical device environment, it is colored. In fact, the first link is to discuss it. I think if you don’t understand it, you can look at msdn directly: http://msdn.microsoft.com/en-us/library/windows/desktop/dd183488(v=vs. 85).aspx is very detailed.

Konkreetsed näited on järgmised: Saame kasutada

HDC hDC = GetDC (NULL)
HDC hDC2 = CreateCompatibleDC (hDC)
HBITMAP hBM = CreateCompatibleBitmap (hDC, 400, 400)
Valige objekt (hDC2, hBM)
saab kasutada ka järgmiselt:

HDC hDC = GetDC (NULL)
HDC hDC2 = CreateCompatibleDC (hDC)
HBITMAP hBM = CreateCompatibleBitmap (hDC2, 400, 400)
Valige objekt (hDC2, hBM)
Esimese koodi hdc2 värvivorming peaks olema värviline, teine ​​aga ühevärviline.