>_<: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: