Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim StrImg As String = "1.gif"
Dim Img As Bitmap
Dim Sb As System.Text.StringBuilder
Dim Sw As IO.StreamWriter
Try
Img = Bitmap.FromFile(StrImg)
Sb = New System.Text.StringBuilder
Sb.Append("<html><head><title>Img2Txt</title></head><body style=""font-size: 6pt""><meta http-equiv=""Content-Type"" content=""text/html; charset=gb2312"">" & vbCrLf)
For h As Integer = 0 To Img.Height - 1
For w As Integer = 0 To Img.Width - 1
Dim c As Color = Img.GetPixel(w, h)
Sb.Append("<font color=#" & int2Hex(c.R) & int2Hex(c.G) & int2Hex(c.B) & ">图</font>")
Next
Sb.Append("<br>" & vbCrLf)
Next
Sb.Append("</body></html>")
Sw = New IO.StreamWriter("1.htm", False, System.Text.Encoding.GetEncoding("Gb2312"))
Sw.Write(Sb.ToString)
Catch ex As Exception
MsgBox("看起来不是图片。。")
Finally
Img.Dispose()
If Not Sw Is Nothing Then
Sw.Close()
End If
End Try
End Sub
Function int2Hex(ByVal Int As Int32) As String
Return System.Convert.ToString(Int, 16)
End Function
Dim StrImg As String = "1.gif"
Dim Img As Bitmap
Dim Sb As System.Text.StringBuilder
Dim Sw As IO.StreamWriter
Try
Img = Bitmap.FromFile(StrImg)
Sb = New System.Text.StringBuilder
Sb.Append("<html><head><title>Img2Txt</title></head><body style=""font-size: 6pt"">" & vbCrLf)
For h As Integer = 0 To Img.Height - 1
For w As Integer = 0 To Img.Width - 1
Sb.Append("<font color=#" & System.Convert.ToString(Img.GetPixel(w, h).ToArgb, 16).Substring(0, 6) & ">图</font>")
Next
Sb.Append("<br>" & vbCrLf)
Next
Sb.Append("</body></html>")
Sw = New IO.StreamWriter("1.htm", False, System.Text.Encoding.Default)
Sw.Write(Sb.ToString)
Catch ex As Exception
MsgBox("看起来不是图片。。")
Finally
Img.Dispose()
If Not Sw Is Nothing Then
Sw.Close()
End If
End Try
================================================
每个点用相同颜色的字代替。。
生成的文件很庞大,几乎没有什么意义。
实在想玩的话,考虑压缩方法,看看怎么样生成的html最小!
Dim Filename As String = "c:\1.gif"
Dim fs As New IO.FileStream(Filename, IO.FileMode.Open)
Dim Sr As New IO.BinaryReader(fs)
Dim IntLen As Int32 = fs.Length
Dim IntPar As Int32 = Math.Ceiling(IntLen / 256)
Dim IntRead As Int32 = 0
Dim Sb As New System.Text.StringBuilder
Sb.Append("'FileAnyWhere V1.0" & vbCrLf)
Sb.Append("'By inlove" & vbCrLf)
Sb.Append("'Qq:1982426 Email:1982426@qq.com" & vbCrLf)
Sb.Append("'" & Now & vbCrLf)
Sb.Append("'" & Filename & vbCrLf)
Filename = Filename.Substring(Filename.LastIndexOf("\") + 1)
Filename = Filename.Replace(".", "_")
Filename = Filename.Replace("{", "_")
Filename = Filename.Replace("}", "_")
Filename = Filename.Replace("(", "_")
Filename = Filename.Replace(")", "_")
'
Sb.Append("Namespace Res" & vbCrLf)
Sb.Append("Public Class C_" & Filename & vbCrLf)
Sb.Append("#Region ""数据""" & vbCrLf)
For i As Integer = 0 To IntPar - 1
Sb.Append(" Private Shared part" & i & " As Byte() = New Byte() {")
For ii As Integer = 0 To 255
Sb.Append(Sr.ReadByte & ",")
Dim intYU As Int32
Math.DivRem(ii + 16, 16, intYU)
If intYU = 15 Then
Sb.Append(" _ " & vbCrLf & " ")
End If
IntRead += 1
If IntRead = IntLen Then
Exit For
End If
Next
Sb.Append("0}" & vbCrLf)
Next
Sb.Append("#End Region" & vbCrLf)
Sb.Append(" Public Shared ReadOnly Property MyfileStream() As IO.Stream" & vbCrLf)
Sb.Append(" Get" & vbCrLf)
Sb.Append(" Dim s As New IO.MemoryStream" & vbCrLf)
Sb.Append(" Dim Sw As New IO.BinaryWriter(s)" & vbCrLf)
For I As Integer = 0 To IntPar - 1
Sb.Append(" Sw.Write(part" & i & ", 0, part" & i & ".Length - 1)" & vbCrLf)
Next
Sb.Append(" Return s" & vbCrLf)
Sb.Append(" End Get" & vbCrLf)
Sb.Append(" End Property" & vbCrLf)
Sb.Append("End Class" & vbCrLf)
Sb.Append("End Namespace" & vbCrLf)