[游戏模版9] Win32 半透明 图像处理



 

>_<:Previous part we talk about how to map a transparent picture, and this time we will solve the problem how to change a picture's transparency.

>_<:Here my method is reading the picture's information and change its RGB or add another picture's RGB to it according to certain proportion(here result must between 0~255, like following example you can let background picture's RGB multiply by 0.7 and wanted picture's RGB multiply by 0.3), then map it on window.

>_<:Here is the method introduction:

  • firstly: load picture to handle:
  • bg=(HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,600,450,LR_LOADFROMFILE);

 

  • secondly:get the picture information to bm1 (suggestion find the knowledge about bitmap struct and it's better to know how computer save .bmp picture)
  • BITMAP bm1;
  • GetObject(bg,sizeof(BITMAP),&bm1);

 

  • Thirdly:ensure whether the picture is suit for this model.
1 if(bm1.bmBitsPixel!=32 && bm1.bmBitsPixel!=24)
2 {
3     MessageBox(NULL,"此程序只能在 32 bit 或 24 bit 显示模式中运行","警告",0);
4     return FALSE;
5 }
  • Fourthly:get the RGB of .bmp picture saving it in unsigned char array.
1 unsigned char *px1=new unsigned char[bm1.bmHeight * bm1.bmWidthBytes];
2 GetBitmapBits(bg,bm1.bmHeight*bm1.bmWidthBytes,px1);
  • Fifthly:use the same way to  deal with another picture that you want to change transparency,the first one show as background.
1 dra=(HBITMAP)LoadImage(NULL,"dra.bmp",IMAGE_BITMAP,243,120,LR_LOADFROMFILE);
2 GetObject(dra,sizeof(BITMAP),&bm2);
3 px2=new unsigned char[bm2.bmHeight * bm2.bmWidthBytes];
4 GetBitmapBits(dra,bm2.bmHeight*bm2.bmWidthBytes,px2);
  • Sixth:according to wanted transparent picture's lenth and width change corresponding background picture's char array's RGB value.At the same time changing this picture's char array's value,and after this step operation the px2 array will including the already-seted-transparency picture information.
复制代码
 1 int xend,yend;
 2 int x,y,i;
 3 int rgb_b;
 4 int PxBytes=bm1.bmBitsPixel/8;
 5 
 6 xend=xstart+298;
 7 yend=ystart+329;
 8 
 9 for(y=ystart;y<yend;y++)
10 {
11    for(x=xstart;x<xend;x++)
12    {
13        rgb_b=y*bm1.bmWidthBytes+x*PxBytes;
14 
15        px1[rgb_b]=px1[rgb_b]*0.7;
16        px1[rgb_b+1]=px1[rgb_b+1]*0.7;
17        px1[rgb_b+2]=px1[rgb_b+2]*0.7;
18    }
19 }
20 
21 
22 for(y=0;y<(bm2.bmHeight);y++)
23 {
24    for(x=0;x<bm2.bmWidth;x++)
25    {
26        rgb_b=y*bm2.bmWidthBytes+x*PxBytes;
27        i=(ystart+y)*bm1.bmWidthBytes+(xstart+x)*PxBytes;
28 
29        px2[rgb_b]=px2[rgb_b]*0.3+px1[i];
30        px2[rgb_b+1]=px2[rgb_b+1]*0.3+px1[i+1];
31        px2[rgb_b+2]=px2[rgb_b+2]*0.3+px1[i+2];
32    }
33 }
复制代码
  • Finally:use px2[] to reflash dra and make the handle dra include new seted-transparency picture's information.
1 SetBitmapBits(dra,bm2.bmHeight*bm2.bmWidthBytes,px2);

>_<:now the handle of bg.bmp is in bg;the handle of seted-transparency dra.bmp is in dra.Next,you can directly map them on window's dc.

复制代码
1 void MyPaint(HDC hdc)
2 {
3     SelectObject(mdc,bg);
4     BitBlt(hdc,0,0,1366,768,mdc,0,0,SRCCOPY);//在窗口位置、大小、原图剪切位
5 
6     SelectObject(mdc,dra);
7     BitBlt(hdc,xstart,ystart,121,120,mdc,0,0,SRCCOPY);
8 }
复制代码

 >_<:Here is all the code:

  resourse.h
  StdAfx.h
  main.cpp

 

 

标签:  Win32



本文转自beautifulzzzz博客园博客,原文链接:http://www.cnblogs.com/zjutlitao/p/3733164.html ,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值