ibennz

Converter

Nov 3rd, 2013
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.86 KB | None | 0 0
  1. Imports System.Drawing.Imaging
  2. Imports System.Runtime.InteropServices
  3. Imports System.Threading
  4.  
  5. Public Class Converter
  6.     Private Shared datapool() As Char = _
  7.         CType("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789=/+ .".ToCharArray & Chr(10), Char())
  8.  
  9.  
  10.     Public Shared Function ConvertTostring(ByVal img As Bitmap) As String
  11.  
  12.         Dim mList As New System.Text.StringBuilder
  13.         Dim mDict As Dictionary(Of Color, String) = passCode(img)
  14.         Dim pxf As PixelFormat = PixelFormat.Format32bppArgb
  15.         Dim bmpData As BitmapData = img.LockBits(New Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadWrite, pxf)
  16.         Dim mPos As IntPtr = bmpData.Scan0 + (Math.Abs(bmpData.Stride) * 1) 'skip first row
  17.         Dim mPixels As Integer = (Math.Abs(bmpData.Stride)) * img.Height - 1
  18.  
  19.      
  20.  
  21.         For i As Integer = 0 To mPixels - 1
  22.             Dim currentByte(3) As Byte
  23.             Marshal.Copy(mPos, currentByte, 0, 4)
  24.             If currentByte(3) = 0 And currentByte(2) = 0 And currentByte(1) = 0 And currentByte(0) = 0 Then
  25.                 Exit For
  26.             End If
  27.  
  28.             If mDict.ContainsKey(Color.FromArgb(currentByte(3), currentByte(2), currentByte(1), currentByte(0))) Then
  29.                 mList.Append(mDict.Item(Color.FromArgb(currentByte(3), currentByte(2), currentByte(1), currentByte(0))))
  30.             End If
  31.  
  32.             mPos += 4  '<-- position
  33.  
  34.         Next
  35.  
  36.         img.UnlockBits(bmpData)
  37.         Return mList.ToString
  38.     End Function
  39.  
  40.  
  41.  
  42.     Private Shared Function passCode(ByVal img As Bitmap) As Dictionary(Of Color, String)
  43.         Dim mDict As New Dictionary(Of Color, String)
  44.         Dim pxf As PixelFormat = PixelFormat.Format32bppArgb 'Format32bppArgb
  45.         Dim bmpData As BitmapData = img.LockBits(New Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadWrite, pxf)
  46.         'keep alpha
  47.  
  48.         Dim ptr As IntPtr = bmpData.Scan0
  49.         For i As Integer = 0 To datapool.Length - 1
  50.             Dim currentByte(3) As Byte
  51.             Marshal.Copy(ptr, currentByte, 0, 4)
  52.             mDict.Add(Color.FromArgb(currentByte(3), currentByte(2), currentByte(1), currentByte(0)), datapool(i))
  53.             ptr += 4  '<-- position
  54.         Next
  55.         img.UnlockBits(bmpData)
  56.         Return mDict
  57.     End Function
  58.  
  59.  
  60.  
  61.     Public Shared Function ConvertToPixel(ByVal data As String, Optional ByVal mWidth As Integer = 500, Optional ByVal mHeight As Integer = 500) As Image
  62.         list.Clear()
  63.  
  64.         'faster
  65.         Dim mSB As New System.Text.StringBuilder : mSB.Append(data)
  66.  
  67.         Dim mDict As Dictionary(Of String, Color) = setdata()
  68.         Dim mImage As Bitmap = New Bitmap(500, 500)
  69.      
  70.         Dim pxf As PixelFormat = PixelFormat.Format32bppArgb
  71.         Dim bmpData As BitmapData = mImage.LockBits(New Rectangle(0, 0, mImage.Width, mImage.Height), ImageLockMode.ReadWrite, pxf)
  72.         Dim dPos As IntPtr = bmpData.Scan0
  73.  
  74.         For i As Integer = 0 To mDict.Values.Count
  75.             Dim hColor As Color = mDict.Values(i)
  76.             Dim cHolder(3) As Byte
  77.             cHolder(3) = hColor.A
  78.             cHolder(2) = hColor.R
  79.             cHolder(1) = hColor.G
  80.             cHolder(0) = hColor.B
  81.             Marshal.Copy(cHolder, 0, dPos, 4)
  82.             dPos += 4
  83.         Next
  84.  
  85.         'rescan
  86.         Dim cPos As IntPtr = bmpData.Scan0 + (Math.Abs(bmpData.Stride) * 1) 'dragon broke dis.
  87.         Dim xVal As Integer = 0
  88.  
  89.         For Each mCh As Char In mSB.ToString
  90.             ' Small error, not needed.
  91.             'If xVal = bmpData.Width Then
  92.             '    cPos += Math.Abs(bmpData.Stride)
  93.             '    xVal = 0
  94.             ' End If
  95.             Dim hColor As Color = mDict(mCh)
  96.             Dim cBytes(3) As Byte
  97.             cBytes(3) = hColor.A
  98.             cBytes(2) = hColor.R
  99.             cBytes(1) = hColor.G
  100.             cBytes(0) = hColor.B
  101.             Marshal.Copy(cBytes, 0, cPos, 4)
  102.             cPos += 4
  103.             xVal += 1
  104.         Next
  105.         mImage.UnlockBits(bmpData)
  106.         Return mImage
  107.     End Function
  108.    
  109.  
  110.     Private Shared Function setdata() As Dictionary(Of String, Color)
  111.         Dim mDict As New Dictionary(Of String, Color)
  112.         For Each i As Char In datapool
  113.             mDict.Add(i, Color.FromArgb(255, randomnumbers, randomnumbers, randomnumbers))
  114.         Next
  115.         Return mDict
  116.     End Function
  117.  
  118.     Private Shared rand As New Random
  119.     Private Shared list As New System.Text.StringBuilder
  120.     Private Shared Function randomnumbers() As Integer
  121.         Dim tmpvar As String = CStr(rand.Next(0, 255))
  122.         If list.Length = 0 Then
  123.             list.AppendLine(CStr(rand.Next(0, 255)))
  124.         End If
  125.         For i As Integer = 0 To list.Length - 1
  126.             If tmpvar = list(i) Then
  127.                 randomnumbers()
  128.             End If
  129.         Next
  130.         Return CInt(tmpvar)
  131.     End Function
  132. End Class
Advertisement
Add Comment
Please, Sign In to add comment