MCPcopy Create free account
hub / github.com/HO-COOH/FastCopy / HBitmapToWriteableBitmap

Function HBitmapToWriteableBitmap

FastCopy/ImageUtils.cpp:39–109  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

37}
38
39winrt::Microsoft::UI::Xaml::Media::Imaging::WriteableBitmap HBitmapToWriteableBitmap(HBITMAP hBitmap, HBITMAP hMask)
40{
41 // Get the screen DC
42 HDC dc = GetDC(NULL);
43
44 // Get icon size info
45 BITMAP bm = { 0 };
46 GetObject(hBitmap, sizeof(BITMAP), &bm);
47 if (bm.bmWidth == 0 || bm.bmHeight == 0)
48 return nullptr;
49
50 // Set up BITMAPINFO
51 BITMAPINFO bmi = { 0 };
52 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
53 bmi.bmiHeader.biWidth = bm.bmWidth;
54 bmi.bmiHeader.biHeight = -bm.bmHeight;
55 bmi.bmiHeader.biPlanes = 1;
56 bmi.bmiHeader.biBitCount = 32;
57 bmi.bmiHeader.biCompression = BI_RGB;
58
59 // Extract the color bitmap
60 int nBits = bm.bmWidth * bm.bmHeight;
61 auto colorBits = std::make_unique<int32_t[]>(nBits);
62 GetDIBits(dc, hBitmap, 0, bm.bmHeight, colorBits.get(), &bmi, DIB_RGB_COLORS);
63
64 // Check whether the color bitmap has an alpha channel.
65 BOOL hasAlpha = FALSE;
66 for (int i = 0; i < nBits; i++) {
67 if ((colorBits[i] & 0xff000000) != 0) {
68 hasAlpha = TRUE;
69 break;
70 }
71 }
72
73 // If no alpha values available, apply the mask bitmap
74 if (!hasAlpha) {
75 // Extract the mask bitmap
76 int32_t* maskBits = new int32_t[nBits];
77 GetDIBits(dc, hMask, 0, bm.bmHeight, maskBits, &bmi, DIB_RGB_COLORS);
78 // Copy the mask alphas into the color bits
79 for (int i = 0; i < nBits; i++) {
80 if (maskBits[i] == 0) {
81 colorBits[i] |= 0xff000000;
82 }
83 }
84 delete[] maskBits;
85 }
86
87 // Release DC and GDI bitmaps
88 ReleaseDC(NULL, dc);
89 DeleteObject(hBitmap);
90 if (hMask != NULL)
91 DeleteObject(hMask);
92
93
94 {
95 winrt::Microsoft::UI::Xaml::Media::Imaging::WriteableBitmap bitmap{ bm.bmWidth, bm.bmHeight };
96 auto buffer = bitmap.PixelBuffer();

Callers 3

HIconToWriteableBitmapFunction · 0.85
getIconFromWin32MenuMethod · 0.85
BitmapMethod · 0.85

Calls 2

dataMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected