上次发过一个软件,见下文
http://www.cnblogs.com/aowind/archive/2005/03/05/113429.html
其软件的功能就是将一个图像转成HTML文件,就是用一些自定义的数字通过不同的色彩来表现出这个图像
其效果如下:
经过小弟研究了一下,在vb.net中写出了相同实现功能的代码
功能实现主要是应用到system.drawing.bitmap,和其方法getpixel()
主要代码如下:
Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bit As System.Drawing.Bitmap
bit = bit.FromFile("c:\aowindme.bmp") '读取一个图像文件
Dim w, h As Integer
w = bit.Width - 1 '取得图像每行的像素量
h = bit.Height - 1 '取得图像的行数
Dim pixel As System.Drawing.Color(,) '定义一个类型为系统色彩型的二维数组,来存放图片的所有像系的色彩信息
pixel = New System.Drawing.Color(w, h) {} '根据图像的像系每行数量和行量来重新定义数组下标
Dim i, j
'利用循环把图像所有像素的色彩信息对应存入数组
For i = 0 To h
For j = 0 To w
pixel(j, i) = bit.GetPixel(j, i)
Next
Next
Dim content As String '定义一个字符串来存放要写入html的内容
content = toweb(w, h, pixel) '生成写入html的内容
Dim y As Boolean '定义一个逻辑变量来判断是否写入成功
y = SaveTextFile("c:\999.htm", content) '写入html文件
If y Then MsgBox("ok!")
End Sub
'得到一个RGB信息的相应WEB代码
Private Function GetWEBColorinfo()Function GetWEBColorinfo(ByVal x As Color) As String
Dim r, g, b As String
r = Hex(CInt(x.R)) '取得一个像素色彩信息中的R信息,转成16进制后存成字符串型
g = Hex(CInt(x.G)) '取得一个像素色彩信息中的R信息,转成16进制后存成字符串型
b = Hex(CInt(x.B)) '取得一个像素色彩信息中的R信息,转成16进制后存成字符串型
'如果不足两位的在前面加0,因为WEB色彩表示应为#+R(两位16进制)+G(两位16进制)+B(两位16进制)
If r.Length = 1 Then r = "0" & r
If g.Length = 1 Then g = "0" & g
If b.Length = 1 Then b = "0" & b
Return "#" & r & g & b
End Function
'生成要写处html文件的字符串,即html文件的内容
Private Function toweb()Function toweb(ByVal w As Integer, ByVal h As Integer, ByVal pixel As Color(,)) As String
Dim html As String
html = "<html><head><title>傲风图像网页生成</title></head><body bgcolor='#000000'><center>" & vbCrLf
Dim i, j
For i = 0 To h
For j = 0 To w
html = html & "<font color='" & GetWEBColorinfo(pixel(j, i)) & "'>" & Int(Rnd(10) * 10) & Int(Rnd(10) * 10) & "</font>"
Next
html = html & "<br>" & vbCrLf
Next
html = html & "</center></body></html>"
Return html
End Function
'写入文件函数
Private Function SaveTextFile()Function SaveTextFile(ByVal FilePath As String, ByVal FileContent As String) As Boolean
Dim sw As System.IO.StreamWriter
Try
sw = New System.IO.StreamWriter(FilePath, False)
sw.Write(FileContent)
Return True
Catch e As Exception
Return False
Finally
If Not sw Is Nothing Then sw.Close()
End Try
End Function
还请大虾位指教!