[游戏模版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 ,如需转载请自行联系原作者
相关文章
|
JSON 自然语言处理 图形学
|
图形学
unity3d UGUI常用游戏进度条实现方式
测试.png 直接将脚本挂载到进度条image对象上即可,这种方式可以解决当进度条使用图片的时候,防止图片拉伸变形 using UnityEngine; using UnityEngine.
2740 0
|
21天前
|
定位技术
Pyglet综合应用|推箱子游戏地图编辑器之图片跟随鼠标
Pyglet综合应用|推箱子游戏地图编辑器之图片跟随鼠标
20 0
|
11月前
|
Python 容器
tkinter模块高级操作(二)—— 界面切换效果、立体阴影字效果及gif动图的实现
tkinter模块高级操作(二)—— 界面切换效果、立体阴影字效果及gif动图的实现
180 0
|
12月前
零基础VB教程018期:shape控件制作小球运动实验
零基础VB教程018期:shape控件制作小球运动实验
|
iOS开发 MacOS Windows
Unity2D像素游戏开发——Aseprite简单人物绘画+动画制作导出精灵表示例
Unity2D像素游戏开发——Aseprite简单人物绘画+动画制作导出精灵表示例
538 0
Unity2D像素游戏开发——Aseprite简单人物绘画+动画制作导出精灵表示例
用 Pyqt5 制作一个动态水波进度条
最近做了一个小项目,里面有一个需求需要添加一个动态进度条,进度条的样式就类似于水波来回起伏的那种形状,下面就是最初的展示效果(有一点区别,这里我加了一个进度自动增加的功能):