VB.NET还是比较常用的,于是我研究了一下VB.NET,在这里拿出来和大家分享一下,希望对大家有用。在vb.net中写出了相同实现功能的VB.NET代码
功能实现主要是应用到system.drawing.bitmap,和其方法getpixel()
主要VB.NET代码 如下:
- 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
- bitbit = 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
- htmlhtml = html & "<font color='" & GetWEBColorinfo(pixel(j, i)) & "'>" & Int(Rnd(10) * 10) & Int(Rnd(10) * 10) & "</font>"
- Next
- htmlhtml = html & "<br>" & vbCrLf
- Next
- htmlhtml = 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
以上就是将图像转成HTML文件,VB.NET代码。
【编辑推荐】