ibennz

Converter V2

Nov 19th, 2013
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.17 KB | None | 0 0
  1. Imports System.Drawing.Imaging
  2. Imports System.Runtime.InteropServices
  3. Imports System.Runtime.CompilerServices
  4.  
  5. 'Author : ibennz
  6. 'Credits : ibennz
  7. 'Version : 2
  8. 'If you use this , please credit me ! <3
  9. Module Converter
  10.     <Extension()> _
  11.     Public Function ToBitmap(ByVal data As Byte(), Optional ByVal Alpha As Integer = 255, Optional ByVal green As Integer = 100, _
  12.                              Optional ByVal red As Integer = 100) As Bitmap
  13.  
  14.         Dim mSB() As Byte = data
  15.         Dim mImage As Bitmap = New Bitmap(500, CInt(data.Length / 125) + 1)
  16.         Dim pxf As PixelFormat = PixelFormat.Format32bppArgb
  17.         Dim bmpData As BitmapData = mImage.LockBits(New Rectangle(0, 0, mImage.Width, mImage.Height), ImageLockMode.ReadWrite, pxf)
  18.  
  19.         Dim cPos As IntPtr = bmpData.Scan0
  20.         Dim xVal As Integer = 0
  21.  
  22.         For Each mByte As Byte In mSB
  23.             Dim cBytes(3) As Byte
  24.             cBytes(3) = CByte(Alpha)
  25.             cBytes(2) = mByte
  26.             cBytes(1) = CByte(green)
  27.             cBytes(0) = CByte(red)
  28.             Marshal.Copy(cBytes, 0, cPos, 4)
  29.             cPos += 4
  30.             xVal += 1
  31.         Next
  32.         mImage.UnlockBits(bmpData)
  33.         Return mImage
  34.     End Function
  35.     <Extension()> _
  36.     Public Function ToBitmap(ByVal data As String, Optional ByVal Alpha As Integer = 255, Optional ByVal green As Integer = 100, _
  37.                             Optional ByVal red As Integer = 100) As Bitmap
  38.         Dim mSB() As Byte = System.Text.Encoding.UTF8.GetBytes(data)
  39.         Dim mImage As Bitmap = New Bitmap(500, CInt(data.Length / 125) + 1)
  40.         Dim pxf As PixelFormat = PixelFormat.Format32bppArgb
  41.         Dim bmpData As BitmapData = mImage.LockBits(New Rectangle(0, 0, mImage.Width, mImage.Height), ImageLockMode.ReadWrite, pxf)
  42.  
  43.         Dim cPos As IntPtr = bmpData.Scan0
  44.         Dim xVal As Integer = 0
  45.  
  46.         For Each mByte As Byte In mSB
  47.             Dim cBytes(3) As Byte
  48.             cBytes(3) = CByte(Alpha)
  49.             cBytes(2) = mByte
  50.             cBytes(1) = CByte(green)
  51.             cBytes(0) = CByte(red)
  52.             Marshal.Copy(cBytes, 0, cPos, 4)
  53.             cPos += 4
  54.             xVal += 1
  55.         Next
  56.         mImage.UnlockBits(bmpData)
  57.         Return mImage
  58.  
  59.     End Function
  60.     <Extension()> _
  61.     Public Function ToBytes(ByVal file As Image) As Byte()
  62.  
  63.         Dim img As New Bitmap(file)
  64.  
  65.         Dim mList As New System.Collections.Generic.List(Of Byte)
  66.         Dim pxf As PixelFormat = PixelFormat.Format32bppArgb
  67.         Dim bmpData As BitmapData = img.LockBits(New Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadWrite, pxf)
  68.         Dim mPos As IntPtr = bmpData.Scan0
  69.         Dim mPixels As Integer = (Math.Abs(bmpData.Stride)) * img.Height - 1
  70.  
  71.  
  72.         For i As Integer = 0 To mPixels - 1
  73.             Dim currentByte(3) As Byte
  74.             Marshal.Copy(mPos, currentByte, 0, 4)
  75.             If currentByte(3) = 0 And currentByte(2) = 0 And currentByte(1) = 0 And currentByte(0) = 0 Then
  76.                 Exit For
  77.             End If
  78.  
  79.             mList.Add(currentByte(2))
  80.             mPos = New IntPtr(CInt(mPos) + 4)
  81.         Next
  82.         img.UnlockBits(bmpData)
  83.         Return mList.ToArray
  84.     End Function
  85.     <Extension()> _
  86.     Public Function ToString(ByVal file As Image) As String
  87.  
  88.         Dim img As New Bitmap(file)
  89.  
  90.         Dim mList As New System.Text.StringBuilder
  91.         Dim pxf As PixelFormat = PixelFormat.Format32bppArgb
  92.         Dim bmpData As BitmapData = img.LockBits(New Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadWrite, pxf)
  93.         Dim mPos As IntPtr = bmpData.Scan0
  94.         Dim mPixels As Integer = (Math.Abs(bmpData.Stride)) * img.Height - 1
  95.  
  96.  
  97.         For i As Integer = 0 To mPixels - 1
  98.             Dim currentByte(3) As Byte
  99.             Marshal.Copy(mPos, currentByte, 0, 4)
  100.             If currentByte(3) = 0 And currentByte(2) = 0 And currentByte(1) = 0 And currentByte(0) = 0 Then
  101.                 Exit For
  102.             End If
  103.  
  104.             mList.Append(System.Convert.ToChar(currentByte(2)))
  105.             mPos = New IntPtr(CInt(mPos) + 4)
  106.         Next
  107.         img.UnlockBits(bmpData)
  108.         Return mList.ToString
  109.     End Function
  110. End Module
Advertisement
Add Comment
Please, Sign In to add comment