3x5w4rup

Image to a Graphical Html Code

Apr 19th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 10.28 KB | None | 0 0
  1. Public Class Form1
  2.     Dim file1 As String
  3. #Region "Controls : "
  4.     Dim prog As New ProgressBar
  5.     Dim pic As New PictureBox
  6.     Dim WithEvents but1 As New Button
  7.     Dim WithEvents but2 As New Button
  8. #End Region
  9.     Public Sub Bitmap_To_Htmlcode(ByVal pic As Bitmap)
  10.         Dim sw As New IO.StreamWriter(file1)
  11.         sw.WriteLine("<style type=" & Chr(34) & "text/css" & Chr(34) & ">pa {font-size:5px;}" & Environment.NewLine & "</style><pa>")
  12.         Dim bmp As New RoBitmap(pic)
  13.         For y = 0 To bmp.Height - 1
  14.             Dim ste As String = ""
  15.             For x = 0 To bmp.Width - 1
  16.                 Dim col As Color = bmp.GetPixel(x, y)
  17.                 sw.Write("<font color=" & Chr(34) & "rgb(" & col.R.ToString & "," & col.G.ToString & "," & col.B.ToString & ")" & Chr(34) & ">@</font>")
  18.             Next
  19.             Me.Invoke(Sub() prog.Value += bmp.Width)
  20.             sw.Write("</br>")
  21.         Next
  22.         sw.Write("</pa>")
  23.         sw.Close()
  24.     End Sub
  25.     Public Sub lol()
  26.         Dim bmp As Bitmap = CType(pic.Image, Bitmap)
  27.         Me.Invoke(Sub() prog.Maximum = bmp.Width * bmp.Height)
  28.         Bitmap_To_Htmlcode(bmp)
  29.         MessageBox.Show("Finish")
  30.     End Sub
  31.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles but2.Click
  32.         Dim fr As New SaveFileDialog
  33.         fr.Filter = " HTML |*.html"
  34.         fr.ShowDialog()
  35.         file1 = fr.FileName
  36.         Dim th As New Threading.Thread(AddressOf lol)
  37.         th.Start()
  38.     End Sub
  39.     Private Sub but1_Click(sender As System.Object, e As System.EventArgs) Handles but1.Click
  40.         Dim fr As New OpenFileDialog
  41.         fr.Filter = "Image|*.jepg;*.jpg;*.bmp;*.png;*.gif"
  42.         fr.ShowDialog()
  43.         If IO.File.Exists(fr.FileName) = True Then
  44.             pic.Image = New Bitmap(fr.FileName)
  45.         End If
  46.     End Sub
  47.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  48.         prog.Size = New Size(400, 21)
  49.         prog.Location = New Point(12, 287)
  50.         Me.Controls.Add(prog)
  51.         pic.Size = New Size(483, 244)
  52.         pic.Location = New Point(12, 37)
  53.         Me.Controls.Add(pic)
  54.         but1.Size = New Size(71, 19)
  55.         but1.Location = New Point(12, 12)
  56.         but1.Text = "load"
  57.         Me.Controls.Add(but1)
  58.         but2.Size = New Size(77, 21)
  59.         but2.Location = New Point(418, 287)
  60.         but2.Text = "save"
  61.         Me.Controls.Add(but2)
  62.         Me.Size = New Size(521, 352)
  63.         Me.Text = "Picture to html"
  64.     End Sub
  65. End Class
  66. Class RoBitmap
  67.     Private bildDaten As Byte()
  68.     Private colore As Color(,)
  69.     Private m_width As Integer
  70.     Private m_height As Integer
  71.     Private Bild As Bitmap
  72.     Private rect As Rectangle
  73.     Private modified As Boolean
  74.     Private bytes As Integer
  75.     Private stride As Integer
  76.     Private pixelFormat As System.Drawing.Imaging.PixelFormat
  77.     Private colorPalette As System.Drawing.Imaging.ColorPalette
  78.     Public Sub New(bld As Bitmap)
  79.         Bild = bld
  80.         SetzeWerte()
  81.     End Sub
  82.     Private Sub SetzeWerte()
  83.         colorPalette = Bild.Palette
  84.         pixelFormat = Bild.PixelFormat
  85.         m_width = Bild.Width
  86.         m_height = Bild.Height
  87.         rect = New Rectangle(0, 0, m_width, m_height)
  88.         Dim bmpData As System.Drawing.Imaging.BitmapData = Bild.LockBits(rect, System.Drawing.Imaging.ImageLockMode.[ReadOnly], Bild.PixelFormat)
  89.  
  90.         Dim ptr As IntPtr = bmpData.Scan0
  91.         stride = bmpData.Stride
  92.         bytes = stride * m_height
  93.         bildDaten = New Byte(bytes - 1) {}
  94.         System.Runtime.InteropServices.Marshal.Copy(ptr, bildDaten, 0, bytes)
  95.         Bild.UnlockBits(bmpData)
  96.         colore = New Color(m_width - 1, m_height - 1) {}
  97.         Select Case pixelFormat
  98.             Case System.Drawing.Imaging.PixelFormat.Format32bppArgb
  99.                 Format32BppArgb()
  100.                 Exit Select
  101.             Case System.Drawing.Imaging.PixelFormat.Format24bppRgb
  102.                 Format24BppRgb()
  103.                 Exit Select
  104.             Case System.Drawing.Imaging.PixelFormat.Format8bppIndexed
  105.                 Format8BppIndexed()
  106.                 Exit Select
  107.             Case System.Drawing.Imaging.PixelFormat.Format4bppIndexed
  108.                 Format4BppIndexed()
  109.                 Exit Select
  110.             Case System.Drawing.Imaging.PixelFormat.Format1bppIndexed
  111.                 Format1BppIndexed()
  112.                 Exit Select
  113.         End Select
  114.         modified = False
  115.     End Sub
  116.     Private Sub Format32BppArgb()
  117.         For y As Integer = 0 To m_height - 1
  118.             For x As Integer = 0 To m_width - 1
  119.                 colore(x, y) = Color.FromArgb(bildDaten(y * stride + x * 4 + 3), bildDaten(y * stride + x * 4 + 2), bildDaten(y * stride + x * 4 + 1), bildDaten(y * stride + x * 4))
  120.             Next
  121.         Next
  122.     End Sub
  123.     Private Sub Format24BppRgb()
  124.         For y As Integer = 0 To m_height - 1
  125.             For x As Integer = 0 To m_width - 1
  126.                 colore(x, y) = Color.FromArgb(bildDaten(y * stride + x * 3 + 2), bildDaten(y * stride + x * 3 + 1), bildDaten(y * stride + x * 3))
  127.             Next
  128.         Next
  129.     End Sub
  130.     Private Sub Format8BppIndexed()
  131.         For y As Integer = 0 To m_height - 1
  132.             For x As Integer = 0 To m_width - 1
  133.                 colore(x, y) = colorPalette.Entries(bildDaten(y * stride + x))
  134.             Next
  135.         Next
  136.     End Sub
  137.     Private Sub Format4BppIndexed()
  138.         For y As Integer = 0 To m_height - 1
  139.             For x As Integer = 0 To m_width - 1
  140.                 If x Mod 2 = 0 Then
  141.                     colore(x, y) = colorPalette.Entries(LowByte(bildDaten(y * stride + x \ 2)))
  142.                 Else
  143.                     colore(x, y) = colorPalette.Entries(HighByte(bildDaten(y * stride + x \ 2)))
  144.                 End If
  145.             Next
  146.         Next
  147.     End Sub
  148.     Private Sub Format1BppIndexed()
  149.         Dim rest As Integer = m_width Mod 8
  150.         Dim bits As Byte
  151.         Dim x As Integer, y As Integer
  152.         For y = 0 To m_height - 1
  153.             For x = 0 To m_width - 9 Step 8
  154.                 bits = bildDaten(y * stride + x \ 8)
  155.                 colore(x, y) = colorPalette.Entries((bits And 128) \ 128)
  156.                 colore(x + 1, y) = colorPalette.Entries((bits And 64) \ 64)
  157.                 colore(x + 2, y) = colorPalette.Entries((bits And 32) \ 32)
  158.                 colore(x + 3, y) = colorPalette.Entries((bits And 16) \ 16)
  159.                 colore(x + 4, y) = colorPalette.Entries((bits And 8) \ 8)
  160.                 colore(x + 5, y) = colorPalette.Entries((bits And 4) \ 4)
  161.                 colore(x + 6, y) = colorPalette.Entries((bits And 2) \ 2)
  162.                 colore(x + 7, y) = colorPalette.Entries(bits And 1)
  163.             Next
  164.             bits = bildDaten(y * stride + x \ 8)
  165.             Dim teiler As Integer = 128
  166.             For i As Integer = 0 To rest - 1
  167.                 colore(x + i, y) = colorPalette.Entries((bits And teiler) \ teiler)
  168.                 teiler = CInt(teiler / 2)
  169.             Next
  170.         Next
  171.     End Sub
  172.     Private Function HighByte(zahl As Byte) As Integer
  173.         Return zahl >> 4
  174.     End Function
  175.     Private Function LowByte(zahl As Byte) As Integer
  176.  
  177.         Return zahl And 15
  178.     End Function
  179.     Public Function GetPixel(x As Integer, y As Integer) As Color
  180.         Return colore(x, y)
  181.     End Function
  182.     Public Sub SetPixel(x As Integer, y As Integer, col As Color)
  183.         colore(x, y) = col
  184.         modified = True
  185.     End Sub
  186.     Public ReadOnly Property Width() As Integer
  187.         Get
  188.             Return m_width
  189.         End Get
  190.     End Property
  191.     Public ReadOnly Property Height() As Integer
  192.         Get
  193.             Return m_height
  194.         End Get
  195.     End Property
  196.     Public Property Image() As Bitmap
  197.         Get
  198.             If Not modified Then
  199.                 Return Bild
  200.             End If
  201.             Select Case pixelFormat
  202.                 Case System.Drawing.Imaging.PixelFormat.Format32bppArgb
  203.                     Return ReturnFormat32BppArgb()
  204.                 Case System.Drawing.Imaging.PixelFormat.Format24bppRgb
  205.                     Return ReturnFormat24BppRgb()
  206.                 Case System.Drawing.Imaging.PixelFormat.Format8bppIndexed
  207.                     'ReturnFormat8BppIndexed();
  208.                     Exit Select
  209.                 Case System.Drawing.Imaging.PixelFormat.Format4bppIndexed
  210.                     'ReturnFormat4BppIndexed();
  211.                     Exit Select
  212.                 Case System.Drawing.Imaging.PixelFormat.Format1bppIndexed
  213.                     'ReturnFormat1BppIndexed();
  214.                     Exit Select
  215.             End Select
  216.             Return Nothing
  217.         End Get
  218.         Set(value As Bitmap)
  219.             Bild = value
  220.             SetzeWerte()
  221.         End Set
  222.     End Property
  223.     Private Function ReturnFormat24BppRgb() As Bitmap
  224.         For y As Integer = 0 To m_height - 1
  225.             For x As Integer = 0 To m_width - 1
  226.                 bildDaten(y * stride + x * 3 + 2) = colore(x, y).R
  227.                 bildDaten(y * stride + x * 3 + 1) = colore(x, y).G
  228.                 bildDaten(y * stride + x * 3) = colore(x, y).B
  229.             Next
  230.         Next
  231.         Dim bmpData As System.Drawing.Imaging.BitmapData = Bild.LockBits(rect, System.Drawing.Imaging.ImageLockMode.[WriteOnly], Bild.PixelFormat)
  232.         Dim ptr As IntPtr = bmpData.Scan0
  233.         System.Runtime.InteropServices.Marshal.Copy(bildDaten, 0, ptr, bytes)
  234.         Bild.UnlockBits(bmpData)
  235.         modified = False
  236.         Return Bild
  237.     End Function
  238.     Private Function ReturnFormat32BppArgb() As Bitmap
  239.         For y As Integer = 0 To m_height - 1
  240.             For x As Integer = 0 To m_width - 1
  241.                 bildDaten(y * stride + x * 4 + 3) = colore(x, y).A
  242.                 bildDaten(y * stride + x * 4 + 2) = colore(x, y).R
  243.                 bildDaten(y * stride + x * 4 + 1) = colore(x, y).G
  244.                 bildDaten(y * stride + x * 4) = colore(x, y).B
  245.             Next
  246.         Next
  247.         Dim bmpData As System.Drawing.Imaging.BitmapData = Bild.LockBits(rect, System.Drawing.Imaging.ImageLockMode.[WriteOnly], Bild.PixelFormat)
  248.         Dim ptr As IntPtr = bmpData.Scan0
  249.         System.Runtime.InteropServices.Marshal.Copy(bildDaten, 0, ptr, bytes)
  250.         Bild.UnlockBits(bmpData)
  251.         modified = False
  252.         Return Bild
  253.     End Function
  254. End Class
Add Comment
Please, Sign In to add comment