2016-03-09 281 views
1

如何清除drawimage?

爲了讓我的形象透明,我用:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Dim myGraphics As Graphics = Me.CreateGraphics 
    myGraphics.DrawImage(Img1, 500, 190) 
End Sub 

我的問題,現在我怎麼能抹去myGraphics.DrawImage(Img1, 500, 190) 或與其它圖像取代它呢?

+0

感謝您的編輯@adrianbanks –

+0

這是在表單中完成的嗎?我問的原因是因爲通常這是與繪畫事件,而不是點擊一個按鈕... – Codexer

+0

是在形式。我已經完成了我的解決方案。我使用圖片框中的代碼插入圖片,只要圖片框是透明的,圖片就變得透明。 –

回答

0

如何擦除myGraphics.DrawImage(Img1, 500, 190)或將其替換爲其他圖像?

的少量信息(無聊的東西),以幫助您瞭解如何Graphics.DrawImage的作品,你可以做清除/刪除/刪除以及上述所有的東西。

當您使用Graphics.DrawImage這是永久,因爲它是利用Graphics類中,它使用GDI繪製圖像。一旦發生這種情況,您不再可以訪問該繪製對象除非 ...您保留一份副本。此外,重要的是您應該在Paint Events中進行繪圖,而不是後者。這樣可以減少繪圖資源,也可以讓您訪問Graphics對象,而不是自己創建一個對象。

下面是解...

'Used to keep current graphics object so you can use it later... 
Private gObject As System.Drawing.Graphics = Nothing 

'Create the image... 
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim myGraphics As System.Drawing.Graphics = Me.CreateGraphics 
    myGraphics.DrawImage(Img1, 500, 50) 

    'Clear this out if we can and should... 
    If Me.gObject IsNot Nothing Then Me.gObject.Clear(Me.BackColor) 

    'Save the Graphics object... 
    Me.gObject = myGraphics 

End Sub 

那拿的關懷創建圖像並單擊該按鈕,每次清理出來的。至於替換該圖片,您不會顯示如何填充它們。代碼雖然相同,清除對象並重置它,然後將新副本保存到變量中...