制作:KrK (Knuth for Kludge)
#include <windows.h> // Win32API
//-------------------------------------------------
// ビットマップにアルファ値をセット
// 作成 20141112 KrK
// 引数 hBmp:ビットマップハンドル
//-------------------------------------------------
VOID SetAlpha(HBITMAP hBmp)
{
HDC hdc; // デバイスコンテキスト
BITMAPINFO bmi = { 0 }; // ビットマップヘッダ
RGBQUAD *pPixels; // ピクセル配列
INT x, y; // ビットマップ位置:ループ変数
INT p; // ポジション
BITMAP bmp; // ビットマップ情報構造体
// サイズ取得
GetObject(hBmp, sizeof(BITMAP), &bmp);
// ピクセル配列取得
hdc = CreateCompatibleDC(NULL);
HBITMAP hBmpOld = (HBITMAP)SelectObject(hdc, hBmp);
pPixels = new RGBQUAD[bmp.bmWidth * bmp.bmHeight];
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
bmi.bmiHeader.biWidth = bmp.bmWidth;
bmi.bmiHeader.biHeight = bmp.bmHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
GetDIBits(hdc, hBmp, 0, bmp.bmHeight, pPixels, &bmi, DIB_RGB_COLORS);
// アルファ値を適用
for (x = 0; x < bmp.bmWidth; x++)
{
for (y = 0; y < bmp.bmHeight; y++)
{
p = (y * bmp.bmWidth) + x;
pPixels[p].rgbRed = pPixels[p].rgbRed * pPixels[p].rgbReserved / 255;
pPixels[p].rgbGreen = pPixels[p].rgbGreen * pPixels[p].rgbReserved / 255;
pPixels[p].rgbBlue = pPixels[p].rgbBlue * pPixels[p].rgbReserved / 255;
}
}
SetDIBits(hdc, hBmp, 0, bmp.bmHeight, pPixels, &bmi, DIB_RGB_COLORS);
// 解放
SelectObject(hdc, hBmpOld);
DeleteDC(hdc);
delete[] pPixels;
}
HBITMAP hBmp; // ビットマップハンドル
// ビットマップを読み込む
hBmp = ~;
// アルファ値をセット
SetAlpha(hBmp);
(中略)
// 開放
DeleteObject(hBmp);
最終更新:2023/09/13