【问题标题】:Want to place several images with transparent colour on the same background想在同一个背景上放置几张透明色的图片
【发布时间】:2011-01-12 13:18:14
【问题描述】:

我正在绝望地寻找将几个具有透明背景的图形放置在与 GDI+ 相同的背景上。我之前没有任何 Windows 或图形编程(如游戏)的编程经验,因此更难找到解决方案。 GDI+ 具有透明色。 GDI+ 仅使用透明度信息将这种颜色正确位映射到另一个图像上。然而,一旦位图化,第一张图像就不再透明。如果您在同一背景上多次放置同一张图片,您会看到只有第一个位置是透明的。

我的问题是一次在背景上放置几个透明对象。您可以看到下面适用于一艘船的代码 (nNrOfShips = 1;)。如果你为这个变量写一个更大的值,将不会放置任何船。

我应该如何修改代码?我也尝试使用 Ship 数组,但屏幕上没有出现船。您可以通过使用(稍微)改变颜色的背景来创建自己的示例,然后透明地放置图像。我希望这个例子对我有帮助。

这里是代码示例...

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;

【问题讨论】:

    标签: c++ windows gdi+ transparency


    【解决方案1】:

    在加载图像时对图像使用 Bitmap.MakeTransparent() 方法。您需要选择作为这些图像的背景颜色的颜色。以 PNG 格式存储图像并在图形编辑器中选择透明度是另一种方式。

    【讨论】:

    • 感谢您的回复!位图已经是透明的,因为我们使用 Paint.NET 可以设置透明颜色。但是,我正在寻找一种将多个透明图像放置在同一背景上的方法,而我从现在开始遇到的问题是大问题。我无法理解如此简单的任务为何如此困难。我没有书(你知道一本好的 GDI+ 书吗?),我也不认识任何有 GDI+ 经验的人。所以,我有麻烦了。现在,我发现由 CreateCompatibleDC() 创建的 m_hdcNewScreen 可能是问题所在,但我会看到...
    猜你喜欢
    • 2017-03-12
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 2023-01-04
    • 1970-01-01
    • 2017-11-07
    • 1970-01-01
    相关资源
    最近更新 更多