VB.NET:生成底色透明的PNG圖片

今天研究了下.NET窗體應用程序生成透明底色的PNG圖像。函數

個人.NET版本爲.NET 4.5,VS版本爲VS2012,操做系統版本爲Win7旗艦版ServicePack1。工具

例程1:生成100%純透明的PNG圖片,這個例程生成的是一張沒有任何雜質的透明PNG圖片。spa

''' <summary>
''' 生成100%透明的PNG圖片
''' </summary>
''' <param name="Width">橫向長度</param>
''' <param name="Height">縱向長度</param>
''' <param name="OutputPath">圖片輸出地址</param>
''' <remarks></remarks>
Public Sub GenTransparentPNG(
    ByVal width As Integer,
    ByVal height As Integer,
    ByVal outputPath As String)
    Dim image As Bitmap = New Bitmap(width, height)
    Dim g As Graphics = Graphics.FromImage(image)
    g.Clear(Color.Transparent)
    image.Save(outputPath, System.Drawing.Imaging.ImageFormat.Png)
End Sub

例程2:生成100%透明的PNG圖片+一個矩形。操作系統

''' <summary>
''' 生成100%透明的PNG圖片+一個矩形
''' </summary>
''' <param name="Width">橫向長度</param>
''' <param name="Height">縱向長度</param>
''' <param name="OutputPath">圖片輸出地址</param>
''' <remarks></remarks>
Public Sub GenTransparentPNGAndRect(
    ByVal width As Integer,
    ByVal height As Integer,
    ByVal outputPath As String)
    Dim image As Bitmap = New Bitmap(width, height)
    Dim g As Graphics = Graphics.FromImage(image)
    g.Clear(Color.Transparent)
    If width > 20 And height > 20 Then
        g.DrawRectangle(Pens.Black, New Rectangle(10, 10, width - 20, height - 20))
    End If
    image.Save(outputPath, System.Drawing.Imaging.ImageFormat.Png)
End Sub

例程3:生成白色底色的PNG圖片+一個矩形。指針

''' <summary>
''' 生成白色底色的PNG圖片+一個矩形
''' </summary>
''' <param name="Width">橫向長度</param>
''' <param name="Height">縱向長度</param>
''' <param name="OutputPath">圖片輸出地址</param>
''' <remarks></remarks>
Public Sub GenWhitePNGAndRect(
    ByVal width As Integer,
    ByVal height As Integer,
    ByVal outputPath As String)
    Dim image As Bitmap = New Bitmap(width, height)
    Dim g As Graphics = Graphics.FromImage(image)
    g.Clear(Color.White)
    If width > 20 And height > 20 Then
        g.DrawRectangle(Pens.Black, New Rectangle(10, 10, width - 20, height - 20))
    End If
    image.Save(outputPath, System.Drawing.Imaging.ImageFormat.Png)
End Sub

例程4:將指定顏色替換爲透明,若是有須要還能夠將此例程進行改造,使得在必定範圍內的顏色都被替換爲透明。code

''' <summary>
''' 將指定顏色替換爲透明
''' </summary>
''' <param name="bitmapPath">被替換圖像地址</param>
''' <param name="outputPath">新圖像輸出地址</param>
''' <param name="color">被替換顏色</param>
''' <remarks></remarks>
Public Sub ChangeBackgroundToTransparent(
    ByVal bitmapPath As String,
    ByVal outputPath As String,
    ByVal color As Color)
    Dim image As Bitmap = New Bitmap(bitmapPath)
    For i As Integer = 0 To image.Width - 1
        For j As Integer = 0 To image.Height - 1
            Dim colorTmp = image.GetPixel(i, j)
            If colorTmp.R = color.R And colorTmp.G = color.G And colorTmp.B = color.B Then
                image.SetPixel(i, j, Drawing.Color.Transparent)
            End If
        Next
    Next
    image.Save(outputPath, System.Drawing.Imaging.ImageFormat.Png)
End Sub

Main函數調用orm

Sub Main()
    '生成200*200的純透明PNG圖片
    GenTransparentPNG(200, 200, "C:\Users\Tsybius\Desktop\transparent.png")
    '生成200*200的透明PNG圖片外加一個矩形
    GenTransparentPNGAndRect(200, 200, "C:\Users\Tsybius\Desktop\transparentRect.png")
    '生成200*200的白色背景PNG圖像外加一個矩形
    GenWhitePNGAndRect(200, 200, "C:\Users\Tsybius\Desktop\whiteRect.png")
    '將200*200的白色背景PNG圖像中白色摳去替換爲透明
    GenWhitePNGAndRect(200, 200, "C:\Users\Tsybius\Desktop\whiteRect2.png")
    ChangeBackgroundToTransparent("C:\Users\Tsybius\Desktop\whiteRect2.png", "C:\Users\Tsybius\Desktop\transparentRect2.png", Color.White)
    '生成完畢
    Console.WriteLine("生成完畢")
    Console.ReadLine()
End Sub

運行結果以下:圖片

附上另外兩個實用的小技巧:ip

一、獲取圖像指定點的RGB值,用QQ截圖工具,按下Ctrl+Alt+A進入截圖模式後,鼠標右下角會自動顯示出當前鼠標指針指向位置的顏色信息。rem

在截圖模式下,按下Ctrl鍵,能夠看到十六進制顯示的RGB信息

二、判斷圖像的背景色是否爲透明的,能夠打開一個PowerPoint演示文稿,設置一個背景,而後將要判斷的圖像放入便可。

我使用的Office版本爲:Microsoft Office Professional Plus 2010,PPT版本爲:14.0.4760.1000(32位)。

END

相關文章
相關標籤/搜索