Μπαίνω κατευθείαν στο ψητό...
Καταρχήν θα πρέπει να δημιουργήσουμε μία Class.
Υπάρχουν δύο τρόποι...
Γράφουμε των παρακάτω κώδικα χαρακτήρα - χαρακτήρα και τον σώνουμε...
Ελπίζω να μη γίνω κουραστικός...
Imports System.Drawing
Public Class BitmapShapedForm
#Region " Declarations "
Private myRegion As Region
Private myBitmap As Bitmap
#End Region
#Region " Public Properties "
Public ReadOnly Property GetScannedBitmapRegion() As Region
'
' Return the region.
'
Get
Return myRegion
End Get
End Property
Public ReadOnly Property GetScannedBitmap() As Bitmap
'
' Return the bitmap.
'
Get
Return myBitmap
End Get
End Property
#End Region
#Region " Public Methods "
Public Overloads Sub ApplyScannedBitmapRegion(ByRef theForm As Form)
theForm.Region = myRegion.Clone()
End Sub
Public Overloads Sub ApplyScannedBitmapRegion(ByRef theRegion As Region)
theRegion = myRegion.Clone()
End Sub
Public Overloads Sub ApplyScannedBitmap(ByRef theBitmap As Bitmap)
theBitmap = myBitmap
End Sub
Public Overloads Sub ApplyScannedBitmap(ByRef theForm As Form)
theForm.BackgroundImage = myBitmap
End Sub
Public Sub SetFormToScannedBitmapSize(ByRef theForm As Form)
theForm.Height = myBitmap.Height
theForm.Width = myBitmap.Width
End Sub
Public Overloads Sub ScanBitmap(ByVal theBitmapFile As String)
Dim aBitmap As Bitmap = _LoadBitmapFile(theBitmapFile)
If Not (aBitmap Is Nothing) Then
_ScanBitmap(aBitmap, aBitmap.GetPixel(0, 0))
End If
End Sub
Public Overloads Sub ScanBitmap(ByVal theBitmapFile As String, ByVal theTransparentColor As Color)
Dim aBitmap As Bitmap = _LoadBitmapFile(theBitmapFile)
If Not (aBitmap Is Nothing) Then
_ScanBitmap(aBitmap, theTransparentColor)
End If
End Sub
Public Overloads Sub ScanBitmap(ByVal theBitmap As Bitmap)
If Not (theBitmap Is Nothing) Then
_ScanBitmap(theBitmap, theBitmap.GetPixel(0, 0))
End If
End Sub
Public Overloads Sub ScanBitmap(ByVal theBitmap As Bitmap, ByVal theTransparentColor As Color)
If Not (theBitmap Is Nothing) Then
_ScanBitmap(theBitmap, theTransparentColor)
End If
End Sub
Public Overloads Sub ScanBitmap(ByVal theForm As Form, ByVal theTransparentColor As Color)
If Not (theForm.BackgroundImage Is Nothing) Then
_ScanBitmap(theForm.BackgroundImage, theTransparentColor)
End If
End Sub
Public Overloads Sub SetFormBitmapSize(ByRef theForm As Form, ByVal theBitmap As Bitmap)
theForm.Height = theBitmap.Height
theForm.Width = theBitmap.Width
End Sub
Public Overloads Sub SetFormBitmapSize(ByRef theForm As Form, ByVal theBitmapFile As String)
Dim aBitmap As Bitmap = _LoadBitmapFile(theBitmapFile)
theForm.Height = aBitmap.Height
theForm.Width = aBitmap.Width
End Sub
Public Overloads Sub SetFormBitmapSize(ByRef theForm As Size, ByVal theBitmap As Bitmap)
theForm.Height = theBitmap.Height
theForm.Width = theBitmap.Width
End Sub
Public Overloads Sub SetFormBitmapSize(ByRef theForm As Size, ByVal theBitmapFile As String)
Dim aBitmap As Bitmap = _LoadBitmapFile(theBitmapFile)
theForm.Height = aBitmap.Height
theForm.Width = aBitmap.Width
End Sub
Public Overloads Sub ApplyBitmapFile(ByRef theForm As Form, ByVal theBitmapFile As String)
Dim aBitmap As Bitmap = _LoadBitmapFile(theBitmapFile)
theForm.BackgroundImage = aBitmap
End Sub
Public Overloads Sub ApplyBitmapFile(ByRef theBitmap As Bitmap, ByVal theBitmapFile As String)
theBitmap = _LoadBitmapFile(theBitmapFile)
End Sub
Public Overloads Sub ApplyBitmap(ByRef theOldBitmap As Bitmap, ByRef theNewBitmap As Bitmap)
theOldBitmap = theNewBitmap
End Sub
Public Overloads Sub ApplyBitmap(ByRef theForm As Form, ByRef theBitmap As Bitmap)
theForm.BackgroundImage = theBitmap
End Sub
Public Overloads Sub ApplyRegion(ByRef theForm As Form, ByRef theRegion As Region)
theForm.Region = theRegion
End Sub
Public Overloads Sub ApplyRegion(ByRef theOldRegion As Region, ByRef theNewRegion As Region)
theOldRegion = theNewRegion
End Sub
Public Overloads Function GetBitmapRegion(ByVal theBitmapFile As String) As Region
Dim aBitmap As Bitmap = _LoadBitmapFile(theBitmapFile)
If Not (aBitmap Is Nothing) Then
Return _GetBitmapRegion(aBitmap, aBitmap.GetPixel(0, 0))
End If
End Function
Public Overloads Function GetBitmapRegion(ByVal theBitmap As Bitmap) As Region
If Not (theBitmap Is Nothing) Then
Return _GetBitmapRegion(theBitmap, theBitmap.GetPixel(0, 0))
End If
End Function
Public Overloads Function GetBitmapRegion(ByVal theBitmap As Bitmap, ByVal theTransparentColor As Color) As Region
If Not (theBitmap Is Nothing) Then
Return _GetBitmapRegion(theBitmap, theTransparentColor)
End If
End Function
Public Overloads Function GetBitmapRegion(ByVal theBitmapFile As String, ByVal theTransparentColor As Color) As Region
Dim aBitmap As Bitmap = _LoadBitmapFile(theBitmapFile)
If Not (aBitmap Is Nothing) Then
Return _GetBitmapRegion(aBitmap, theTransparentColor)
End If
End Function
Public Overloads Function GetBitmapRegion(ByVal theForm As Form, ByVal theTransparentColor As Color) As Region
If Not (theForm.BackgroundImage Is Nothing) Then
Return _GetBitmapRegion(theForm.BackgroundImage, theTransparentColor)
End If
End Function
#End Region
#Region " Private Methods "
Private Sub _ScanBitmap(ByVal theBitmap As Bitmap, ByVal theTransparentColor As Color)
'
' Scan the specified bitmap and create a region.
'
If theBitmap Is Nothing Then Exit Sub
Dim aBackColor As Color = theTransparentColor
Dim aHeight As Integer = theBitmap.Height - 1
Dim aWidth As Integer = theBitmap.Width
Dim aRow As Integer
Dim aCol As Integer
myBitmap = theBitmap
myRegion = New Region(New Rectangle(0, 0, 0, 0))
For aRow = 0 To aHeight
Dim aStartCol As Integer = -1
Dim aStopCol As Integer = -1
For aCol = 0 To aWidth
If aCol = aWidth Then
If aStartCol <> -1 Then
aStopCol = aCol
Dim regUnion As New Region(New Rectangle(aStartCol, aRow, aStopCol - aStartCol, 1))
myRegion.Union(regUnion)
regUnion = Nothing
End If
Else
If Not theBitmap.GetPixel(aCol, aRow).Equals(aBackColor) Then
If aStartCol = -1 Then aStartCol = aCol
ElseIf theBitmap.GetPixel(aCol, aRow).Equals(aBackColor) Then
If aStartCol <> -1 Then
aStopCol = aCol
Dim regUnion As New Region(New Rectangle(aStartCol, aRow, aStopCol - aStartCol, 1))
myRegion.Union(regUnion)
regUnion = Nothing
aStartCol = -1
aStopCol = -1
End If
End If
End If
Next
Next
End Sub
Private Function _GetBitmapRegion(ByVal theBitmap As Bitmap, ByVal theTransparentColor As Color) As Region
Dim aLocalReg As Region
If theBitmap Is Nothing Then Return aLocalReg
Dim aBackColor As Color = theTransparentColor
Dim aHeight As Integer = theBitmap.Height - 1
Dim aWidth As Integer = theBitmap.Width
Dim aRow As Integer
Dim aCol As Integer
aLocalReg = New Region(New Rectangle(0, 0, 0, 0))
For aRow = 0 To aHeight
Dim aStartCol As Integer = -1
Dim aStopCol As Integer = -1
For aCol = 0 To aWidth
If aCol = aWidth Then
If aStartCol <> -1 Then
aStopCol = aCol
Dim regUnion As New Region(New Rectangle(aStartCol, aRow, aStopCol - aStartCol, 1))
aLocalReg.Union(regUnion)
regUnion = Nothing
End If
Else
If Not theBitmap.GetPixel(aCol, aRow).Equals(aBackColor) Then
If aStartCol = -1 Then aStartCol = aCol
ElseIf theBitmap.GetPixel(aCol, aRow).Equals(aBackColor) Then
If aStartCol <> -1 Then
aStopCol = aCol
Dim regUnion As New Region(New Rectangle(aStartCol, aRow, aStopCol - aStartCol, 1))
aLocalReg.Union(regUnion)
regUnion = Nothing
aStartCol = -1
aStopCol = -1
End If
End If
End If
Next
Next
Return aLocalReg
End Function
Private Function _LoadBitmapFile(ByVal theBitmapFile As String) As Bitmap
Return Bitmap.FromFile(theBitmapFile)
End Function
#End Region
End Class
Παναγιώτης Λαβίδας
Software Developer