关闭

由图片得到异形窗体

1414人阅读 评论(1) 收藏 举报

 

Public Class Form1

    Dim bmpBackground As Bitmap = Nothing

 

    Sub New()

        ' 此调用是Windows 窗体设计器所必需的。

        InitializeComponent()

 

        ' 在InitializeComponent() 调用之后添加任何初始化。

        FormBorderStyle = Windows.Forms.FormBorderStyle.None

    End Sub

 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        SetBackgroundBitmap(Color.FromArgb(255, 0, 255))

    End Sub

 

    Protected Sub SetBackgroundBitmap(ByVal transparencyColor As Color)

        Me.bmpBackground = New Bitmap(MyClass.GetType(), "skin.bmp")

        Width = Me.bmpBackground.Width

        Height = Me.bmpBackground.Height

        Region = BitmapToRegion(Me.bmpBackground, transparencyColor)

    End Sub

 

    Protected Function BitmapToRegion(ByVal bitmap As Bitmap, ByVal transparencyColor As Color) As Region

        Dim height As Integer = bitmap.Height

        Dim width As Integer = bitmap.Width

        Dim path As New Drawing2D.GraphicsPath()

        Dim i As Integer

        Dim j As Integer

 

        For j = 0 To (height - 1)

            For i = 0 To (width - 1)

                If Not bitmap.GetPixel(i, j).Equals(transparencyColor) Then

                    Dim x0 As Integer = i

                    Do While ((i < width) AndAlso (Not bitmap.GetPixel(i, j).Equals(transparencyColor)))

                        i += 1

                    Loop

                    path.AddRectangle(New Rectangle(x0, j, i - x0, 1))

                End If

            Next

        Next

 

        Dim region As New Region(path)

        path.Dispose()

        Return region

    End Function

 

    Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)

        Dim grfx As Graphics = e.Graphics

        grfx.PageUnit = GraphicsUnit.Pixel

        Dim offScreenGraphics As Graphics

        Dim offScreenBitmap As Bitmap

        offScreenBitmap = New Bitmap(Me.bmpBackground.Width, Me.bmpBackground.Height)

        offScreenGraphics = Graphics.FromImage(offScreenBitmap)

        If Not Me.bmpBackground Is Nothing Then

            offScreenGraphics.DrawImage(Me.bmpBackground, 0, 0, _

                                        Me.bmpBackground.Width, _

                                        Me.bmpBackground.Height)

        End If

        grfx.DrawImage(offScreenBitmap, 0, 0)

    End Sub

 

End Class

用到的 skin.bmp 你也可以自己找(应该看过的吧)

 

环境:Visual Studio2005
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:49497次
    • 积分:714
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:6篇
    • 译文:0篇
    • 评论:41条
    文章分类