Programming skills 15 law three

zhaozj2021-02-08  209

1. How to replace the color value in hbitmap

#define colorRef2RGB (Color) | ((Color >> 16) & 0xFF) /

| ((Color << 16) & 0xff0000)

HBitmap ReplaceColor (Hbitmap HBMP, ColorRef CNEWCOLOR)

{

Hbitmap RetBMP = NULL;

IF (HBMP)

{

HDC bufferdc = createcompatibledc (null); // source bit map DC

IF (bufferdc)

{

SelectObject (bufferDC, HBMP); // Select DC

HDC DirectDC = CreateCompatibleDC (NULL); // Target DC

IF (DirectDC)

{

// Get the source bit map size

Bitmap BM;

GetObject (HBMP, SIZEOF (BM), & BM);

// Initialize BitmapInfo information to use createDibsection

BitmapInfo RGB32BitsBitmapInfo;

ZeromeMory (& rgb32bitsbitmapinfo, sizeof (bitmapinfo);

Rgb32bitsbitmapinfo.bmiheader.bisize = sizeof (bitmapinfoheader);

RGB32BITSBITMAPINFO.BMIHEADER.BIWIDTH = BM.BMWIDTH;

RGB32Bitsbitmapinfo.Bmiheader.biheight = bm.bmheight;

RGB32BitsBitmapinfo.Bmiheader.Biplanes = 1;

RGB32BitsbitMapInfo.Bmiheader.biBitcount = 32;

Uint * ptpixels;

Hbitmap DirectBitMap = CREATEDIBSECTION (DirectDC,

(BitMapInfo *) & rgb32bitsbitmapinfo,

DIB_RGB_COLORS, (Void **) & PTPixels, NULL, 0);

IF (DirectBitMap)

{

HgDiobj previousObject = selectObject (DirectDC, DirectBitmap);

Bitblt (DirectDC, 0, 0, Bm.Bmwidth, BM.Bmheight, Bufferdc, 0,0, Srccopy);

// Converting ColorRef for RGB

ColdColor = ColorRef2RGB (ColdColor);

CNewColor = ColorRef2RGB (CNEWCOLOR);

// Replace color

For (INT I = ((BM.BMWIDTH * BM.BMHEIGHT) -1); i> = 0; I -)

{

IF (PTPixels [I] == ColdColor) PTPixels [i] = CNewcolor;

}

// Modify bitmap DirectBitmap

SelectObject (DirectDC, PreviousObject);

// carry out

RetBMP = DirectBitMap;

}

// Release DC

DELETEDC (DirectDC);

}

// Release DC

Deletedc (BufferDC);

}

}

Return RetBMP;

}

usage:

Hbitmap hbmp2 = loadingbitmap (g_hinstance, makeintresource (idb_samplebitmap); hbitmap hbmp = replacecolor (HBMP2, 0xFF0000, 0x00FF00); // Replace Blue is green

......

DeleteObject (HBMP2);

DeleteObject (HBMP);

2. How to convert and save a bitmap

// ******************************************************** ***************************************

// * Name: DDBTODIB

// * Author: Xu Jing Zhou (jingzhou_xu@163.net)

// * Function: Device related conversion to device is unlocked

// ******************************************************** ***************************************

Handle Cscreensnapdlg :: DDBTODIB (CBITMAP & BITMAP, DWORD DWCompRESSION / * = BI_RGB * /)

{

Bitmap BM;

BitmapInfoheader Bi;

LpbitmapInfoHeader LPBI;

DWORD DWLEN;

Handle HDIB;

Handle Handle;

HDC HDC;

HPALETTE HPAL;

CWindowDC DC (this);

CPALETTE PAL;

// If you support the palette, create it

IF (DC.GETDEVICECAPS (RasterCaps) & rc_palette

{

UINT nsize = sizeof (logpalette) (SIZEOF (PALETTEENTRY) * 256);

Logpalette * PLP = (logpalette *) New byte [nsize];

PLP-> paversion = 0x300;

PLP-> PALNUMENTRIES = (Unsigned Short) GetsystemPaletteentries (DC, 0, 255,

PLP-> PALPALENTRY

PAL.CREATEPALETTTE (PLP);

//freed

DELETE [] PLP;

}

Assert (Bitmap.getsafeHandle ());

/ / Do not support bi_bitfields type

IF (dwcompression == bi_bitfields)

Return NULL;

// If the palette is empty, use the default palette

HPAL = (HPALETTE) PAL.GETSAFEHANDLE ();

IF (hpal == null)

HPAL = (HPALETTE) GetStockObject (default_palette);

// Get bitmap information

Bitmap.GetObject (SizeOf (BM), (LPSTR) & BM);

// Initialization bitmap information head

bi.bisize = sizeof (bitmapinfohead);

Bi.biwidth = bm.bmwidth; bi.biheight = bm.bmHeight;

Bi.Biplanes = 1;

Bi.biBitCount = (unsigned short) (BM.BMPLANES * BM.BMBITSPIXEL);

Bi.Bicompression = dwcompression;

Bi.bisizeImage = 0;

bi.bixpelspermeter = 0;

Bi.biypelspermeter = 0;

Bi.bi.biRused = 0;

Bi.Biclrimportant = 0;

// Calculate the information head and color table size

INT ncolors = 0;

IF (bi.bibitcount <= 8)

{

Ncolors = (1 << bi.biBitcount);

}

Dwlen = bi.bisize ncolors * sizeof (rgbquad);

HDC = :: getDC (null);

HPAL = SELECTPALETTTE (HDC, HPAL, FALSE);

RealizePalette (HDC);

/ / Assign memory for the information head and color table

HDIB = GLOBALLOC (GMEM_FIXED, DWLEN);

IF (! hdib) {

SelectPalette (HDC, HPAL, FALSE);

:: ReleaseDC (NULL, HDC);

Return NULL;

}

LPBI = (LPbitMapInfoHeader) Globalock (HDIB);

* lpbi = bi;

// Call getDibits to calculate the image size

GetDibits (HBitmap) Bitmap.getsafehandle (), 0L, (DWORD) BI.BIHEIGHT,

(Lpbyte) NULL, (LPBITMAPINFO) LPBI, (DWORD) DIB_RGB_COLORS;

Bi = * lpbi;

// Each line of the image is aligned (32bit) boundary

IF (bi.bisizeimage == 0) {

Bi.bisizeImage = (((bi.biwidth * bi.biBitcount) 31) & ~ 31) / 8)

* bi.biheight;

IF (dwcompression! = bi_rgb)

Bi.bisizeImage = (bi.bisizeimage * 3) / 2;

}

/ / Re-allocate the memory size in order to put down all data

DWLEN = bi.bisizeImage;

Handle = GlobalRealloc (HDIB, DWLEN, GMEM_MOVEABLE);

IF (Handle! = NULL)

HDIB = Handle;

Else

{

GlobalFree (HDIB);

// Reset the original palette

SelectPalette (HDC, HPAL, FALSE);

:: ReleaseDC (NULL, HDC);

Return NULL;

}

// Get bitmap data

LPBI = (lpbitmapinfoheader) HDIB;

// finally obtained DIB

Bool bgotbits = getDibits (hbitmap) Bitmap.getsafehandle (),

0L, // Scanning start

(Dword) bi.biheight, // Scan line

(Lpbyte) lpbi // bitmap data address 1 (Bi.bisize ncolors * sizeof (rgbquad),

(Lpbitmapinfo) lpbi, // bitmap information address

(DWORD) DIB_RGB_COLORS); // Color Board Using RGB

IF (! bgotbits)

{

GlobalFree (HDIB);

SelectPalette (HDC, HPAL, FALSE);

:: ReleaseDC (NULL, HDC);

Return NULL;

}

SelectPalette (HDC, HPAL, FALSE);

:: ReleaseDC (NULL, HDC);

Return HDIB;

}

// ******************************************************** ***************************************

// * Name: SaveBitmaptofile

// * Modification: Jingzhou_xu@163.net)

// * Features: Save as bitmap file

// ******************************************************** ***************************************

Bool CscreensnaPdlg :: SaveBitmaptofile (Hbitmap Hbitmap, CString LpfileName)

{

HDC HDC; // Device Description Table

INT iBITS; // Currently display the number of items per pixel at the resolution

Word wbitcount; // number of bytes per pixel in the bitmap

DWORD dwpalettesize = 0, // Defines the sliding board size, bitmap pixel byte size, bitmap file size, write file byte

DWBMBITSSIZE,

DWDIBSIZE, DWWWWRITEN

Bitmap bitmap;

BitmapfileHeader BMFHDR; // bit map attribute structure

BitmapInfoHeader Bi; // bitmap file header structure

LPbitmapInfoHeader lpbi; // bitmap information head structure

Handle FH, HDIB, HPAL, HOLDPAL = NULL; / / Pointing the bitmap information head structure, defining files, assigning memory handles, palette handle

/ / Calculate the number of bytes per pixel per pixel

HDC = CREATEDC ("Display", NULL, NULL, NULL;

Ibits = getDeviceCaps (HDC, Bitspixel) *

GetDeviceCaps (HDC, Plaso);

Deletedc (HDC);

IF (iBITS <= 1)

Wbitcount = 1;

Else IF (iBITS <= 4)

Wbitcount = 4;

Else IF (iBITS <= 8) wbitcount = 8;

Else IF (iBITS <= 24)

Wbitcount = 24;

/ / Calculate the size of the palette

IF (wbitcount <= 8)

DWPALETESIZE = (1 << wbitcount) * sizeof (rgbquad);

/ / Set the bitmap information head structure

GetObject (HbitMap, Sizeof (Bitmap), (LPSTR) & Bitmap;

bi.bisize = sizeof (bitmapinfohead);

bi.biwidth = bitmap.bmwidth;

bi.biheight = bitmap.bmheight;

Bi.Biplanes = 1;

Bi.biBitcount = WbitCount;

Bi.Bicompression = BI_RGB;

Bi.bisizeImage = 0;

bi.bixpelspermeter = 0;

Bi.biypelspermeter = 0;

Bi.bi.biRused = 0;

Bi.Biclrimportant = 0;

DWBMBITSSIZE = ((bitmap.bmwidth *

Wbitcount 31) / 32) * 4

* Bitmap.bmheight;

/ / Distribute memory for bitmap content

HDIB = GLOBALLOC (GHND, DWBMBITSSIZE

DWPALETESIZE SIZEOF (BitmapInfoHeader);

LPBI = (LPbitMapInfoHeader) Globalock (HDIB);

* lpbi = bi;

// Treat palette

HPAL = getStockObject (Default_palette);

IF (HPAL)

{

HDC = :: getDC (null);

Holdpal = SELECTPALETTE (HDC, HPALETTE) HPAL, FALSE);

RealizePalette (HDC);

}

/ / Get new pixel values ​​under this palette

GetDibits (HDC, Hbitmap, 0, (UINT) Bitmap.Bmheight,

(LPSTR) LPBI SIZEOF (BitMapInfoHeader) DWPALETESIZE,

(Lpbitmapinfo) lpbi, dib_rgb_colors;

// Restore palette

IF (Holdpal)

{

SelectPalette (HPALETTE) HOLDPAL, TRUE;

RealizePalette (HDC);

:: ReleaseDC (NULL, HDC);

}

// Create a bitmap file

FH = CREATEFILE (lpfilename, generic_write,

0, NULL, CREATE_ALWAYS,

File_attribute_normal | file_flag_sequential_scan, null;

IF (fH == invalid_handle_value)

Return False;

/ / Set the bitmap file header

BMFHDR.BFTYPE = 0x4d42; // "BM"

DWDIBSIZE = SizeOf (BitmapfileHeader) Sizeof (BitmapInfoHeader) DWPALETESIZE DWBMBITSSIZE; BMFHDR.BFSIZE = DWDIBSIZE

BMFHDR.BFRESERVED1 = 0;

BMFHDR.BFRESERVED2 = 0;

BMFHDR.BFOFFBITS = (DWORD) SIZEOF (BitmapFileHeader)

(DWORD) SIZEOF (BitmapInfoHeader)

DWPALETESIZE;

// Write the bitmap file header

Writefile (FH, (LPSTR) & BMFHDR, SIZEOF

(BitmapfileHeader), & dwwritten, null;

/ / Write the rest of the file

Writefile (FH, (LPSTR) LPBI, DWDIBSIZE,

& dwwritten, null;

// Eliminate memory allocation

GlobalUnlock (HDIB);

GlobalFree (HDIB);

CloseHandle (FH);

Return True;

}

Contact information:

Email: jingzhou_xu@163.com

Future Studio (Future Studio)

转载请注明原文地址:https://www.9cbs.com/read-2164.html

New Post(0)