2013-03-28 90 views
0

我正在研究RTS風格的遊戲,以瞭解有關使用類,繼承和接口進行編程的更多信息 - 它進展順利,沒有重大問題。無法獲得色彩矩陣和透明度正常工作

我遇到的問題是colormatrix。在地圖上放置單位時,我使用顏色矩陣來添加綠色或紅色疊加層,具體取決於位置的有效性。再次,這是工作正常使用下面的代碼(爲了清晰起見砍下)。但我也想把圖像做成半透明的。

這是我正在使用的代碼,它首先使單位灰度,然後使其呈現紅色,但不使其變爲半透明。

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 

爲了澄清,問題是,我如何才能讓我的形象使用嘉洛斯是紅色半透明的。

回答

0

一直在看着這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 
溶液