Win32 API:貼圖技巧

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

透空效果:
原圖
     // 背景圖
     SelectObject(mdc, bg);
     BitBlt(hdc, 0 ,0 , 600, 450, mdc, 0, 0, SRCCOPY);
     // 透空圖
     SelectObject(mdc, dra);
     BitBlt(hdc, 280, 320, 85, 99, mdc, 85, 0, SRCAND);
     BitBlt(hdc, 280, 320, 85, 99, mdc, 0, 0, SRCPAINT);
結果
半透明:
公式:半透明圖色彩 = 前景圖色彩 * 不透明度 + 背景圖色彩 * (1 -不透明度)
// 半透明處理,處理後將半透明圖存回result
HBITMAP bg, result;
     BITMAP bm1, bm2;
     unsigned char *px1, *px2;
     px1 = new unsigned char [bm1.bmHeight * bm1.bmWidthBytes];
     GetBitmapBits(bg, bm1.bmHeight * bm1.bmWidthBytes, px1);
     //處理前景圖
     result = (HBITMAP)LoadImage(NULL,"result.bmp", IMAGE_BITMAP, 298, 329, LR_LOADFROMFILE);
     GetObject(result, sizeof(BITMAP), &bm2);
     px2 = new unsigned char [bm2.bmHeight * bm2.bmWidthBytes];
     GetBitmapBits(result, bm2.bmHeight * bm2.bmWidthBytes, px2);
     int xend,yend;
     int x,y,i;                 //迴圈用變數
     int rgb_b;
     int PxBytes = bm1.bmBitsPixel / 8 ;
     xend = xstart + 298;
     yend = ystart + 329;
     //處理背景圖像素顏色
     for(y = ystart; y < yend; y++)
     {
           for(x = xstart; x < xend; x++)
           {
                rgb_b = y * bm1.bmWidthBytes + x * PxBytes ;
                px1[rgb_b] = px1[rgb_b] * 0.7;      
                px1[rgb_b+1] = px1[rgb_b+1] * 0.7;  
                px1[rgb_b+2] = px1[rgb_b+2] * 0.7;  
           }
     }
     //處理前景圖像素顏色
     for(y = 0; y < (bm2.bmHeight); y++)
     {
           for(x = 0; x < bm2.bmWidth; x++)
           {
                rgb_b = y * bm2.bmWidthBytes + x * PxBytes ;
                i = (ystart+y) * bm1.bmWidthBytes + (xstart+x) * PxBytes;
                px2[rgb_b]   = px2[rgb_b] * 0.3 + px1[i];
                px2[rgb_b+1] = px2[rgb_b+1] * 0.3 + px1[i+1];  
                px2[rgb_b+2] = px2[rgb_b+2] * 0.3 + px1[i+2];  
           }
     }
     SetBitmapBits(result, bm2.bmHeight * bm2.bmWidthBytes, px2);
     delete [] px1;
     delete [] px2;
結果
平面貼圖:用矩陣來存如下:0表示背景、1是第一種前景、2是第二種前景...
     int mapIndex[rows*cols] = { 2,2,2,2,0,1,0,1,
                                0,2,2,0,0,0,1,1, 
                                0,0,0,0,0,0,0,1, 
                                2,0,0,0,0,0,2,2, 
                                2,0,0,0,0,2,2,2, 
                                2,0,0,0,2,2,0,0, 
                                0,0,2,2,2,0,0,1, 
                                0,0,2,0,0,0,1,1 };
斜角地圖貼圖:一樣是用矩陣存,但貼圖的座標要更改
     int xstart,ystart;
     xstart = 32 * (rows-1);
     ystart = 0;
     //依mapIndex 陣列中的定義取出對應圖塊,進行地圖拼接
     for (i=0;i<rows*cols;i++)
     {
           SelectObject(bufdc,map[mapIndex[i]]);
           rowNum = i / cols;     //求列編號
           colNum = i % cols;     //求行編號
           x = xstart + colNum * 32 + rowNum * (-32); //求貼圖X 座標
           y = ystart + rowNum * 16 + colNum * 16;     //求貼圖Y 座標
          
           BitBlt(mdc,x,y,64,32,bufdc,64,0,SRCAND);
           BitBlt(mdc,x,y,64,32,bufdc,0,0,SRCPAINT);

     }

沒有留言:

張貼留言