无法使颜色矩阵和透明度正常工作



我正在制作一款RTS风格的游戏,以学习更多关于使用类,继承和接口编程的知识-它进展顺利,没有重大问题。

我遇到的问题是与颜色矩阵。当在地图上放置单位时,我使用颜色矩阵来添加绿色或红色叠加,这取决于位置的有效性。同样,使用下面的代码(为了清晰起见,代码被删减了)也能很好地工作。但是我还想让图像是半透明的。

这是我正在使用的代码,它首先使单位灰度,然后将其染成红色,但不使其半透明。

Public Overrides Function GetCurrentFrame() As Image
    _AnimationFrame += _AnimationFrameTime
    If _AnimationFrame > 5 Then _AnimationFrame = 0
    Dim CurrentFrame As Image = Nothing
    CurrentFrame = My.Resources.ResourceManager.GetObject("dragoon_walk_l_" & Convert.ToInt16(_AnimationFrame).ToString)
    Dim g As Graphics = Graphics.FromImage(CurrentFrame)
    Dim cm As System.Drawing.Imaging.ColorMatrix = New System.Drawing.Imaging.ColorMatrix(New Single()() _
         {New Single() {0.3, 0.3, 0.3, 0, 0}, _
        New Single() {0.59, 0.59, 0.59, 0, 0}, _
        New Single() {0.11, 0.11, 0.11, 0, 0}, _
        New Single() {0, 0, 0, 1, 0}, _
        New Single() {0.5, 0, 0, 0.5, 1}})
    Dim ia As System.Drawing.Imaging.ImageAttributes = New System.Drawing.Imaging.ImageAttributes()
    ia.SetColorMatrix(cm)
    g.DrawImage(CurrentFrame, New Rectangle(0, 0, CurrentFrame.Width, CurrentFrame.Height), 0, 0, CurrentFrame.Width, CurrentFrame.Height, GraphicsUnit.Pixel, ia)
    g.DrawImage(My.Resources.ResourceManager.GetObject("health" & (Int(20 * (_UnitHealth / _UnitHealthMax)) - 1).ToString), 0, 14)
    g.DrawImage(My.Resources.ResourceManager.GetObject("level" & _Level.ToString), 6, 0)
    g.Dispose()
    Return CurrentFrame
End Function

为了澄清,问题是我如何才能让我的图像是红色和半透明使用颜色矩阵。

我已经看了2天了,我才想起来。

问题是我是从原始图像创建位图,而不是空白位图。并返回图像而不是位图:(

这是我使用的解决方案

Public Overrides Function GetCurrentFrame() As Image
    _AnimationFrame += _AnimationFrameTime
    If _AnimationFrame > 5 Then _AnimationFrame = 0
    Dim CurrentFrame As Image = Nothing
    CurrentFrame = My.Resources.ResourceManager.GetObject("dragoon_walk_l_" & Convert.ToInt16(_AnimationFrame).ToString)
    Dim bm As Bitmap = New Bitmap(CurrentFrame.Width, CurrentFrame.Height)
    Dim g As Graphics = Graphics.FromImage(bm)
    Dim cm As System.Drawing.Imaging.ColorMatrix = New System.Drawing.Imaging.ColorMatrix(New Single()() _
         {New Single() {.3, .3, .3, 0, 0}, _
        New Single() {0.59, .59,.59, 0, 0}, _
        New Single() {.1, 0.1, 0.1, 0, 0}, _
        New Single() {0, 0, 0, 0.75, 0}, _
        New Single() {0.5, 0, 0, 0, 1}})
    Dim ia As System.Drawing.Imaging.ImageAttributes = New System.Drawing.Imaging.ImageAttributes()
    ia.SetColorMatrix(cm)
    g.DrawImage(CurrentFrame, New Rectangle(0, 0, CurrentFrame.Width, CurrentFrame.Height), 0, 0, CurrentFrame.Width, CurrentFrame.Height, GraphicsUnit.Pixel, ia)
    g.DrawImage(My.Resources.ResourceManager.GetObject("health" & (Int(20 * (_UnitHealth / _UnitHealthMax)) - 1).ToString), 0, 14)
    g.DrawImage(My.Resources.ResourceManager.GetObject("level" & _Level.ToString), 6, 0)
    g.Dispose()
    Return bm
End Function

相关内容

  • 没有找到相关文章

最新更新