Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Drawing.Imaging
- Imports System.Runtime.InteropServices
- Imports System.Threading
- Public Class Converter
- Private Shared datapool() As Char = _
- CType("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789=/+ .".ToCharArray & Chr(10), Char())
- Public Shared Function ConvertTostring(ByVal img As Bitmap) As String
- Dim mList As New System.Text.StringBuilder
- Dim mDict As Dictionary(Of Color, String) = passCode(img)
- Dim pxf As PixelFormat = PixelFormat.Format32bppArgb
- Dim bmpData As BitmapData = img.LockBits(New Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadWrite, pxf)
- Dim mPos As IntPtr = bmpData.Scan0 + (Math.Abs(bmpData.Stride) * 1) 'skip first row
- Dim mPixels As Integer = (Math.Abs(bmpData.Stride)) * img.Height - 1
- For i As Integer = 0 To mPixels - 1
- Dim currentByte(3) As Byte
- Marshal.Copy(mPos, currentByte, 0, 4)
- If currentByte(3) = 0 And currentByte(2) = 0 And currentByte(1) = 0 And currentByte(0) = 0 Then
- Exit For
- End If
- If mDict.ContainsKey(Color.FromArgb(currentByte(3), currentByte(2), currentByte(1), currentByte(0))) Then
- mList.Append(mDict.Item(Color.FromArgb(currentByte(3), currentByte(2), currentByte(1), currentByte(0))))
- End If
- mPos += 4 '<-- position
- Next
- img.UnlockBits(bmpData)
- Return mList.ToString
- End Function
- Private Shared Function passCode(ByVal img As Bitmap) As Dictionary(Of Color, String)
- Dim mDict As New Dictionary(Of Color, String)
- Dim pxf As PixelFormat = PixelFormat.Format32bppArgb 'Format32bppArgb
- Dim bmpData As BitmapData = img.LockBits(New Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadWrite, pxf)
- 'keep alpha
- Dim ptr As IntPtr = bmpData.Scan0
- For i As Integer = 0 To datapool.Length - 1
- Dim currentByte(3) As Byte
- Marshal.Copy(ptr, currentByte, 0, 4)
- mDict.Add(Color.FromArgb(currentByte(3), currentByte(2), currentByte(1), currentByte(0)), datapool(i))
- ptr += 4 '<-- position
- Next
- img.UnlockBits(bmpData)
- Return mDict
- End Function
- Public Shared Function ConvertToPixel(ByVal data As String, Optional ByVal mWidth As Integer = 500, Optional ByVal mHeight As Integer = 500) As Image
- list.Clear()
- 'faster
- Dim mSB As New System.Text.StringBuilder : mSB.Append(data)
- Dim mDict As Dictionary(Of String, Color) = setdata()
- Dim mImage As Bitmap = New Bitmap(500, 500)
- Dim pxf As PixelFormat = PixelFormat.Format32bppArgb
- Dim bmpData As BitmapData = mImage.LockBits(New Rectangle(0, 0, mImage.Width, mImage.Height), ImageLockMode.ReadWrite, pxf)
- Dim dPos As IntPtr = bmpData.Scan0
- For i As Integer = 0 To mDict.Values.Count
- Dim hColor As Color = mDict.Values(i)
- Dim cHolder(3) As Byte
- cHolder(3) = hColor.A
- cHolder(2) = hColor.R
- cHolder(1) = hColor.G
- cHolder(0) = hColor.B
- Marshal.Copy(cHolder, 0, dPos, 4)
- dPos += 4
- Next
- 'rescan
- Dim cPos As IntPtr = bmpData.Scan0 + (Math.Abs(bmpData.Stride) * 1) 'dragon broke dis.
- Dim xVal As Integer = 0
- For Each mCh As Char In mSB.ToString
- ' Small error, not needed.
- 'If xVal = bmpData.Width Then
- ' cPos += Math.Abs(bmpData.Stride)
- ' xVal = 0
- ' End If
- Dim hColor As Color = mDict(mCh)
- Dim cBytes(3) As Byte
- cBytes(3) = hColor.A
- cBytes(2) = hColor.R
- cBytes(1) = hColor.G
- cBytes(0) = hColor.B
- Marshal.Copy(cBytes, 0, cPos, 4)
- cPos += 4
- xVal += 1
- Next
- mImage.UnlockBits(bmpData)
- Return mImage
- End Function
- Private Shared Function setdata() As Dictionary(Of String, Color)
- Dim mDict As New Dictionary(Of String, Color)
- For Each i As Char In datapool
- mDict.Add(i, Color.FromArgb(255, randomnumbers, randomnumbers, randomnumbers))
- Next
- Return mDict
- End Function
- Private Shared rand As New Random
- Private Shared list As New System.Text.StringBuilder
- Private Shared Function randomnumbers() As Integer
- Dim tmpvar As String = CStr(rand.Next(0, 255))
- If list.Length = 0 Then
- list.AppendLine(CStr(rand.Next(0, 255)))
- End If
- For i As Integer = 0 To list.Length - 1
- If tmpvar = list(i) Then
- randomnumbers()
- End If
- Next
- Return CInt(tmpvar)
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment