由于现在网上对于graphics的教程还不是特别多,貌似也没有特别笼统的教程出现,现贴出一份可以作为制作掩码图使用的函数。
IMAGE maskWork(IMAGE *img) {
IMAGE maskbitmap(WIDTH1, HEIGH1);
DWORD *pmemimg = GetImageBuffer(img);
DWORD *pmemmaskbitmap = GetImageBuffer(&maskbitmap);
COLORREF maskbk = pmemimg[0];
for (int i = 0; i < WIDTH1*HEIGH1; i++)
{
if (pmemimg[i] <= 0x555555 && pmemimg[i] >= 0)//0x55555555是深灰色
{
pmemmaskbitmap[i] = WHITE;
}
else
pmemmaskbitmap[i] = BLACK;
}
return maskbitmap;
}
函数使用方法
// main.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
using namespace std;
#define WIDTH 1080
#define HEIGH 640
#define WIDTH1 50
#define HEIGH1 50
enum INDEX {
i_gold = 4,
i_stone = 8,
i_money = 10,
};
IMAGE img[15];
IMAGE img2[3];
void loadimg() {
//项目->配置->常规->字符集切换成多字节可正常运行
loadimage(img, "./images/0.jpg", WIDTH, HEIGH);
for (int i = 1; i <= 10; i++) {
char filename[20] = "";
sprintf(filename, "./images/%d.jpg", i);
loadimage(img + i, filename, WIDTH1, HEIGH1);
}
loadimage(img2, "./images/stone.jpg", WIDTH1, HEIGH1);
loadimage(img2 + 1, "./images/gold.jpg", WIDTH1, HEIGH1);
loadimage(img2 + 2, "./images/wallet.jpg", WIDTH1, HEIGH1);
}
IMAGE maskWork(IMAGE *img) {
IMAGE maskbitmap(WIDTH1, HEIGH1);
DWORD *pmemimg = GetImageBuffer(img);
DWORD *pmemmaskbitmap = GetImageBuffer(&maskbitmap);
COLORREF maskbk = pmemimg[0];
for (int i = 0; i < WIDTH1*HEIGH1; i++)
{
if (pmemimg[i] <= 0x555555 && pmemimg[i] >= 0)//0x55555555是深灰色
{
pmemmaskbitmap[i] = WHITE;
}
else
pmemmaskbitmap[i] = BLACK;
}
return maskbitmap;
}
int _tmain(int argc, _TCHAR* argv[])
{
int n;
initgraph(WIDTH, HEIGH/*, SHOWCONSOLE*/);
loadimg();
putimage(0, 0, img);
IMAGE maskbitmap = maskWork(img + i_money);
putimage(300, 380, &maskbitmap, SRCAND);
putimage(300, 380, img + i_money, SRCPAINT);
maskbitmap = maskWork(img + i_stone);
putimage(300, 500, &maskbitmap, SRCAND);
putimage(300, 500, img + i_stone, SRCPAINT);
while(1);
return 0;
}