Mit billede format er bygget op på følgende måde.
Først står bredden på billedet som en streng som er skrævet direkte i filen med Asc funktionen til at omdanne vær char til byte.
Bagefter kommer der et regn der ikke er et tal, jeg har valgt en binde streg også skrævet direkte i filen.
Til sidst kommer billedet, i argb farve rækkefølge.
Hvis bredden er 1920, vil det se nogen lunne sådan her ud i Notesblok:
1920-ÿÀÊßÿX5ÿúÓ˜ÿúÑ•ÿúÑ'ÿùБÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùÐÿùБÿúÑ'ÿúÑ•ÿúÓ˜ÿU2ÿ³¿Øÿf}°ÿf}°ÿd|®ÿd|®ÿbzÿax«ÿ`w©ÿ]t§ÿ]t§ÿ[s¦ÿ[s¥ÿZs¥ÿYr¤ÿYq¤ÿWp£ÿWo£ÿVo£ÿVn¢ÿTn¢ÿTn¢ÿTm¡ÿSl ÿQk ÿPj ÿOj ÿNiŸÿMgžÿMgŸÿLgŸÿKfžÿIdÿIcÿHcœÿGbœÿGb›ÿEa›ÿD`šÿD_šÿB_˜ÿA]˜ÿ?\—ÿ?\—ÿ>[-ÿ=Z-ÿ=Z•ÿ=Y-ÿ;X•ÿ;W•ÿ:V•ÿ9V"ÿ8U"ÿ7U'ÿ7S"ÿ5T'ÿ5S'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"
Public Sub New(ByVal Bytes() As Byte)
Dim WidthStr As String = ""
For i = 0 To Bytes.Length - 1
If IsNumeric(Chr(Bytes(i))) Then
WidthStr &= Chr(Bytes(i))
Else
Exit For
End If
Next
Dim picByte(Bytes.Length - 1 - WidthStr.Length - 1) As Byte
Dim i2 As Integer = 0
For i = WidthStr.Length + 1 To Bytes.Length - 1
picByte(i2) = Bytes(i)
i2 += 1
Next
pixels = picByte
width_ = WidthStr
End Sub
Public Sub New(ByVal FileName As String)
Dim Bytes() As Byte = File.ReadAllBytes(FileName)
Dim WidthStr As String = ""
For i = 0 To Bytes.Length - 1
If IsNumeric(Chr(Bytes(i))) Then
WidthStr &= Chr(Bytes(i))
Else
Exit For
End If
Next
Dim picByte(Bytes.Length - 1 - WidthStr.Length - 1) As Byte
Dim i2 As Integer = 0
For i = WidthStr.Length + 1 To Bytes.Length - 1
picByte(i2) = Bytes(i)
i2 += 1
Next
pixels = picByte
width_ = WidthStr
End Sub
Public Sub New(ByVal Stream As Stream)
Dim Bytes(Stream.Length - 1) As Byte
Stream.Read(Bytes, 0, Stream.Length)
Stream.Close()
Dim WidthStr As String = ""
For i = 0 To Bytes.Length - 1
If IsNumeric(Chr(Bytes(i))) Then
WidthStr &= Chr(Bytes(i))
Else
Exit For
End If
Next
Dim picByte(Bytes.Length - 1 - WidthStr.Length - 1) As Byte
Dim i2 As Integer = 0
For i = WidthStr.Length + 1 To Bytes.Length - 1
picByte(i2) = Bytes(i)
i2 += 1
Next
pixels = picByte
width_ = WidthStr
End Sub
Public Sub New(ByVal Image As Image)
Dim img As Bitmap = Image.Clone
Dim btsdata As BitmapData = img.LockBits(New Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb)
Dim ptr As IntPtr = btsdata.Scan0
Dim bytes(btsdata.Stride * btsdata.Height - 1) As Byte
Dim pixbytes(bytes.Length - 1) As Byte
Marshal.Copy(ptr, bytes, 0, bytes.Length)
For i = 0 To bytes.Length - 4 Step 4
pixbytes(i) = bytes(i + 3)
pixbytes(i + 1) = bytes(i + 2)
pixbytes(i + 2) = bytes(i + 1)
pixbytes(i + 3) = bytes(i)
Next
img.UnlockBits(btsdata)
pixels = pixbytes
width_ = img.Width
End Sub
#End Region
Public Function ToBitmap() As Bitmap
Dim pic As New Bitmap(Me.Width, Me.Height)
Dim data As BitmapData = pic.LockBits(New Rectangle(0, 0, pic.Width, pic.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb)
Dim ptr As IntPtr = data.Scan0
Dim bytes(data.Stride * data.Height - 1) As Byte
Marshal.Copy(ptr, bytes, 0, bytes.Length)
For i = 0 To pixels.Length - 4 Step 4
bytes(i + 3) = pixels(i)
bytes(i + 2) = pixels(i + 1)
bytes(i + 1) = pixels(i + 2)
bytes(i) = pixels(i + 3)
Next
Marshal.Copy(bytes, 0, ptr, bytes.Length)
pic.UnlockBits(data)
Return pic
End Function
Public Function Clone() As MHBitmap
Return New MHBitmap(Me.GetBytes) With {.NowMode = Me.NowMode}
End Function
Public Function GetIntPtrResource() As IntPtr
Return GCHandle.ToIntPtr(GCHandle.Alloc(Me.GetBytes))
End Function
Public Shared Sub SetWidth(ByVal Image As MHBitmap, ByVal Width As Integer)
Image.width_ = Width
End Sub
#Region "Shared"
Public Shared Function FromFile(ByVal FileName As String) As MHBitmap
Return New MHBitmap(FileName)
End Function
Public Shared Function FromStream(ByVal Stream As Stream) As MHBitmap
Return New MHBitmap(Stream)
End Function
Public Shared Function FromImage(ByVal Image As Image) As MHBitmap
Return New MHBitmap(Image)
End Function
Public Shared Function FromByteArray(ByVal Array() As Byte) As MHBitmap
Return New MHBitmap(Array)
End Function
Public Shared Function FromIntPtrResource(ByVal Ptr As IntPtr) As MHBitmap
Return New MHBitmap(CType(GCHandle.FromIntPtr(Ptr).Target, Byte()))
End Function
Public Shared Function FromIntPtrObject(ByVal Ptr As IntPtr) As MHBitmap
Return GCHandle.FromIntPtr(Ptr).Target
End Function
#End Region
#Region "Bytes"
Public Sub Save(ByVal FileName As String)
Dim lenstrchs() As Char = width_.ToString
Dim chslen As Integer = lenstrchs.Length
Dim Bytes(pixels.Length + chslen) As Byte
For i = 0 To chslen - 1
Bytes(i) = Asc(lenstrchs(i))
Next
Bytes(chslen) = Asc("-")
For i = 0 To pixels.Length - 1
Bytes(i + chslen + 1) = pixels(i)
Next
File.WriteAllBytes(FileName, Bytes)
End Sub
Public Sub Save(ByVal Stream As Stream)
Dim lenstrchs() As Char = width_.ToString
Dim chslen As Integer = lenstrchs.Length
Dim Bytes(pixels.Length + chslen) As Byte
For i = 0 To chslen - 1
Bytes(i) = Asc(lenstrchs(i))
Next
Bytes(chslen) = Asc("-")
For i = 0 To pixels.Length - 1
Bytes(i + chslen + 1) = pixels(i)
Next
Stream.Write(Bytes, 0, Bytes.Length)
End Sub
Public Function GetBytes() As Byte()
Dim lenstrchs() As Char = width_.ToString
Dim chslen As Integer = lenstrchs.Length
Dim Bytes(pixels.Length + chslen) As Byte
For i = 0 To chslen - 1
Bytes(i) = Asc(lenstrchs(i))
Next
Bytes(chslen) = Asc("-")
For i = 0 To pixels.Length - 1
Bytes(i + chslen + 1) = pixels(i)
Next
Return Bytes
End Function
#End Region
#Region "Rotate"
#Region "Private"
Private Sub Rotate90Right()
Dim bytes(pixels.Length - 1) As Byte
Dim heightX4 As Integer = Me.Height * 4
Dim width_X4 As Integer = width_ * 4
Dim index As Integer = 0
For i = pixels.Length - width_X4 To 0 Step -width_X4
Dim height_index As Integer = 0
For i2 = 0 To width_X4 - 4 Step 4
Dim bytestart As Integer = height_index + index
Dim pixstart As Integer = i + i2
bytes(bytestart) = pixels(pixstart)
bytes(bytestart + 1) = pixels(pixstart + 1)
bytes(bytestart + 2) = pixels(pixstart + 2)
bytes(bytestart + 3) = pixels(pixstart + 3)
height_index += heightX4
Next
index += 4
Next
pixels = bytes
width_ = Me.Height
End Sub
Private Sub Rotate180()
Dim pixlength As Integer = pixels.Length
Dim bytes(pixlength - 1) As Byte
For i = 0 To pixlength - 4 Step 4
Dim start As Integer = pixlength - i - 4
bytes(start) = pixels(i)
bytes(start + 1) = pixels(i + 1)
bytes(start + 2) = pixels(i + 2)
bytes(start + 3) = pixels(i + 3)
Next
pixels = bytes
End Sub
Private Sub Rotate90Left()
Dim bytes(pixels.Length - 1) As Byte
Dim heightX4 As Integer = Me.Height * 4
Dim width_X4 As Integer = width_ * 4
Dim index As Integer = 0
For i = 0 To pixels.Length - 4 Step width_X4
Dim height_index As Integer = 0
For i2 = width_X4 - 4 To 0 Step -4
Dim bytestart As Integer = height_index + index
Dim pixstart As Integer = i + i2
bytes(bytestart) = pixels(pixstart)
bytes(bytestart + 1) = pixels(pixstart + 1)
bytes(bytestart + 2) = pixels(pixstart + 2)
bytes(bytestart + 3) = pixels(pixstart + 3)
height_index += heightX4
Next
index += 4
Next
pixels = bytes
width_ = Me.Height
End Sub
Private Sub RotateTo(ByVal tomode As RotateToMode, ByVal nowmode As RotateToMode)
Select Case tomode
Case RotateToMode.RotateTo180
Select Case nowmode
Case RotateToMode.RotateToNone
Me.Rotate180()
Case RotateToMode.RotateTo180
Case RotateToMode.RotateTo90
Me.Rotate90Right()
Case RotateToMode.RotateTo270
Me.Rotate90Left()
End Select
Case RotateToMode.RotateToNone
Select Case nowmode
Case RotateToMode.RotateToNone
Case RotateToMode.RotateTo180
Me.Rotate180()
Case RotateToMode.RotateTo90
Me.Rotate90Left()
Case RotateToMode.RotateTo270
Me.Rotate90Right()
End Select
Case RotateToMode.RotateTo90
Select Case nowmode
Case RotateToMode.RotateToNone
Me.Rotate90Right()
Case RotateToMode.RotateTo180
Me.Rotate90Left()
Case RotateToMode.RotateTo90
Case RotateToMode.RotateTo270
Me.Rotate180()
End Select
Case RotateToMode.RotateTo270
Select Case nowmode
Case RotateToMode.RotateToNone
Me.Rotate90Left()
Case RotateToMode.RotateTo180
Me.Rotate90Right()
Case RotateToMode.RotateTo90
Me.Rotate180()
Case RotateToMode.RotateTo270
End Select
End Select
End Sub
#End Region
#Region "Rotate"
Public Shared Sub Rotate(ByVal FileName As String, ByVal Mode As RotateMode)
Dim mhbimg As New MHBitmap(FileName)
Select Case Mode
Case RotateMode.Rotate180
mhbimg.Rotate180()
Case RotateMode.Rotate90Left
mhbimg.Rotate90Left()
Case RotateMode.Rotate90Right
mhbimg.Rotate90Right()
End Select
mhbimg.Save(FileName)
End Sub
Public Shared Sub Rotate(ByVal Image As MHBitmap, ByVal Mode As RotateMode)
Select Case Mode
Case RotateMode.Rotate180
Image.Rotate180()
Case RotateMode.Rotate90Left
Image.Rotate90Left()
Case RotateMode.Rotate90Right
Image.Rotate90Right()
End Select
End Sub
Public Sub Rotate(ByVal Mode As RotateMode)
Select Case Mode
Case RotateMode.Rotate180
Me.Rotate180()
Case RotateMode.Rotate90Left
Me.Rotate90Left()
Case RotateMode.Rotate90Right
Me.Rotate90Right()
End Select
End Sub
#End Region
#Region "RotateTo"
Shared PathList As New List(Of String)
Shared RotateList As New List(Of RotateToMode)
Public Shared Sub RotateTo(ByVal FileName As String, ByVal Mode As RotateToMode)
Dim mhbimg As New MHBitmap(FileName)
Dim Rotate As RotateToMode = RotateToMode.RotateToNone
Dim index As Integer
If PathList.Contains(FileName) Then
index = PathList.IndexOf(FileName)
Rotate = RotateList(index)
Else
PathList.Add(FileName)
RotateList.Add(Mode)
index = PathList.Count - 1
End If
mhbimg.RotateTo(Mode, Rotate)
RotateList(index) = Mode
mhbimg.Save(FileName)
End Sub
Shared ImageList As New List(Of MHBitmap)
Shared ModeList As New List(Of RotateToMode)
Public Shared Sub RotateTo(ByVal Image As MHBitmap, ByVal Mode As RotateToMode)
Dim index As Integer
Dim Rotate As RotateToMode = RotateToMode.RotateToNone
If ImageList.Contains(Image) Then
index = ImageList.IndexOf(Image)
Rotate = ModeList(index)
Else
ImageList.Add(Image)
ModeList.Add(Mode)
index = ModeList.Count - 1
End If
Image.RotateTo(Mode, Rotate)
ModeList(index) = Mode
End Sub
Dim NowMode As RotateToMode = RotateToMode.RotateToNone
Public Sub RotateTo(ByVal Mode As RotateToMode)
Me.RotateTo(Mode, NowMode)
NowMode = Mode
End Sub
#End Region
Public Sub Rotate(ByVal Degrees As Integer, Optional ByVal BackColor As Color = Nothing)
Dim colorbytes() As Byte = {BackColor.A, BackColor.R, BackColor.G, BackColor.B}
Dim wm As Integer = Me.Height / 2 / 90 * Degrees
Dim [step] As Double = Me.Height / 2 / 90
Dim newwidth As Integer = width_ + wm
Dim newbytessize As Integer = newwidth * Me.Height * 4
Dim newbytes(newbytessize - 1) As Byte
For i = 0 To newbytes.Length - 4 Step 4
newbytes(i) = colorbytes(0)
newbytes(i + 1) = colorbytes(1)
newbytes(i + 2) = colorbytes(2)
newbytes(i + 3) = colorbytes(3)
Next
Dim pp As Double = 0
Dim newwidthX4 As Integer = newwidth * 4
For i = 0 To newwidthX4 - 4 Step 4
Dim start As Integer = i + (CType(pp, Integer) * newwidthX4)
'newbytes(start) =
pp += [step]
Next
pixels = newbytes
width_ = newwidth
End Sub
#End Region
#Region "Flip"
Private Sub FlipVertical()
Dim bytes(pixels.Length - 1) As Byte
Dim widthX4 As Integer = width_ * 4
Dim iM As Integer = pixels.Length - widthX4
For i = 0 To iM Step widthX4
For i2 = 0 To widthX4 - 4 Step 4
Dim bytestart As Integer = iM + i2
Dim pixstart As Integer = i + i2
bytes(bytestart) = pixels(pixstart)
bytes(bytestart + 1) = pixels(pixstart + 1)
bytes(bytestart + 2) = pixels(pixstart + 2)
bytes(bytestart + 3) = pixels(pixstart + 3)
Next
iM -= widthX4
Next
pixels = bytes
End Sub
Private Sub FlipHorizontal()
Dim bytes(pixels.Length - 1) As Byte
Dim widthX4 As Integer = width_ * 4
For i = 0 To pixels.Length - widthX4 Step widthX4
Dim i2M As Integer = widthX4 - 4
For i2 = 0 To i2M Step 4
Dim bytestart As Integer = i + i2M
Dim pixstart As Integer = i + i2
bytes(bytestart) = pixels(pixstart)
bytes(bytestart + 1) = pixels(pixstart + 1)
bytes(bytestart + 2) = pixels(pixstart + 2)
bytes(bytestart + 3) = pixels(pixstart + 3)
i2M -= 4
Next
Next
pixels = bytes
End Sub
Public Sub Flip(ByVal Mode As FlipMode)
Select Case Mode
Case FlipMode.Vertical
Me.FlipVertical()
Case FlipMode.Horizontal
Me.FlipHorizontal()
End Select
End Sub
#End Region
#Region "Size"
Public ReadOnly Property Width As Integer
Get
Return width_
End Get
End Property
Public ReadOnly Property Height As Integer
Get
Return pixels.Length / width_ / 4
End Get
End Property
Public ReadOnly Property Size As Size
Get
Return New Size(Width, Height)
End Get
End Property
#End Region
#Region "Pixel"
Public Sub SetPixel(ByVal X As Integer, ByVal Y As Integer, ByVal Color As Color)
Dim index As Integer = (X * width_ + Y) * 4
pixels(index) = Color.A
pixels(index + 1) = Color.R
pixels(index + 2) = Color.G
pixels(index + 3) = Color.B
End Sub
Public Function GetPixel(ByVal X As Integer, ByVal Y As Integer) As Color
Dim index As Integer = (X * width_ + Y) * 4
Return Color.FromArgb(pixels(index), pixels(index + 1), pixels(index + 2), pixels(index + 3))
End Function
#End Region
End Class