[VB.NET]圖片透明化

  • 28556
  • 0
  • 2010-08-31

滑鼠點選圖片上的某一顏色,讓此顏色在圖片上透明化

 

有沒有辦法做到圖片的透明化,我們可以透過 MakeTransparent 達成

MSDN : Bitmap.MakeTransparent 方法 : 為這個 Bitmap 將預設的透明色彩變為透明。

 

而在此程式中,加上了讓使用者點選顏色的功能,並且讓點選的顏色透明化

以下為程式碼

VB.NET


Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PictureBox1.ImageLocation = "Test.bmp"
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim bmp As Bitmap
        bmp = PictureBox1.Image
        bmp.MakeTransparent(Color.White)   'Color白色的部份透明 
        bmp.Save("Result.bmp")
        PictureBox2.ImageLocation = "Result.bmp"
    End Sub

    Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        Dim bmp As Bitmap
        bmp = PictureBox1.Image
        bmp.GetPixel(e.X, e.Y)
        bmp.MakeTransparent(bmp.GetPixel(e.X, e.Y))
        bmp.Save("Result.bmp")
        PictureBox2.Image = bmp
    End Sub
End Class

 

執行結果

滑鼠點選白色,使白色成為透明

 

滑鼠點選藍色,使藍色成為透明

 

範例下載

[C#]滑鼠點選以外的顏色變透明.zip

[C#]滑鼠點選顏色變透明.zip