开发者

Want to place several images with transparent colour on the same background

I am desparately searching for place several graphics having a transparent background on the same background with GDI+. I did not have any programming experience with Windows or graphic programming (like games) before, so it is more difficult to find a solution. GDI+ has a transparent colour. GDI+ only uses the transparency information to bitmap this colour properly on another image. Once bitmaped, however, the first image is no more transparent. If you put the same image several times on the same background, you w开发者_如何学JAVAill see that only the first placement was transparent.

My problem is placing several transparent objects on a background at once. You can see the code below that works for one ship (nNrOfShips = 1;). If you write a larger value for this variable, no ship will be placed.

How should I modify the code? I also tried to use Ship arrays, but no ship appears on the screen. You may create your own example by using a background with (slightly) changing colour and just place an image transparently. I hope that that example would help me.

Here the code example...

HDC hdcScreen = GetLockedScreen(); 

m_hdcShip = CreateCompatibleDC(hdcScreen); 
ReleaseLockedScreen(); 

// Draw the ship image on restored background 
Graphics grBkg(m_hdcNewBackground); 
grBkg.SetSmoothingMode(SmoothingModeHighQuality); 

// Restore new background 
BitBlt(m_hdcNewBackground, 0, 0,  
        GetWtsMetrics(wtsm_ScreenSizeX), GetWtsMetrics(wtsm_ScreenSizeY),  
        m_hdcSavedBackground, 0, 0, SRCCOPY);                                                           // 20100125 SAE 

BYTE nNrOfShips = 1;    // DATA->GetNrOfShips(); 
for (BYTE nShipId = 0; nShipId < nNrOfShips; nShipId++) 
{ 
        Ship ship = DATA->GetShipList()[nShipId]; 
        ShipModel shipModel = DATA->FindShipModel(ship.nShipModelId);           // 20100202 SAE 
        WORD nCurResId = DATA->FindCurShipResourceId(ship);                                     // 20100131 SAE 
        WORD nIndex = nCurResId - shipModel.nFirstResId;                                        // 20100131 SAE 
        assert(nIndex >= 0); 
        ShipResource shipRes = (*shipModel.pvectResource)[nIndex];                      // 20100202 SAE 

        // 20100126 SAE 
        // Always take the first (upper left) coordinates of the ship rectangle: 
        QuadrantVector &wpQuadrants =  
                *DATA->GetWallpapers()[DATA->SelectWallpaper()].pvectQuadrant; 
        do 
        {       // 20100115 SAE: Determine first the coordinates of the ship 
                ship.vectRectangle = DATA->RandomRectangleCoordinates(  
                                                shipModel.nHeight, shipModel.nWidth);
        } while (!DATA->AreCoordinatesValid(ship.vectRectangle, wpQuadrants) && 
                         !DATA->AreShipsTooClose(ship, DATA->GetShipList(), DATA->GetDistance())); 

        grBkg.TranslateTransform(ship.vectRectangle[0].fX,  
                                                        ship.vectRectangle[0].fY); 
        grBkg.RotateTransform(0);                                                                                       // 20100201 SAE 
        grBkg.DrawImage(shipRes.pimgPicture,
                       -shipModel.nWidth/2, -shipModel.nHeight/2); 

        // Determine bounding rectangle of ship after drawing on transformed page 
        // 20100125 SAE 
        Rect rcSrc(-shipModel.nWidth/2, -shipModel.nHeight/2,  
                                shipModel.nWidth, shipModel.nHeight); 
        TransformRect(&grBkg, &m_rcCurShip, &rcSrc,  
                                        CoordinateSpacePage, CoordinateSpaceWorld); 
}       // for 

DeleteDC(m_hdcShip); 
m_hdcShip = 0;


Use the Bitmap.MakeTransparent() method on the images when you load them. You'll need to select the color that's the background color for those images. Storing the images in the PNG format with the transparency selected in the graphics editor would be another way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜