新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
詳細(xì)VB.NET代碼之圖像轉(zhuǎn)成HTML文件
VB.NET還是比較常用的,于是我研究了一下VB.NET,在這里拿出來(lái)和大家分享一下,希望對(duì)大家有用。在vb.net中寫(xiě)出了相同實(shí)現(xiàn)功能的VB.NET代碼
功能實(shí)現(xiàn)主要是應(yīng)用到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") '讀取一個(gè)圖像文件
- Dim w, h As Integer
- w = bit.Width - 1 '取得圖像每行的像素量
- h = bit.Height - 1 '取得圖像的行數(shù)
- Dim pixel As System.Drawing.Color(,) '定義一個(gè)類型為系統(tǒng)色彩型的二維數(shù)組,來(lái)存放圖片的所有像系的色彩信息
- pixel = New System.Drawing.Color(w, h) {} '根據(jù)圖像的像系每行數(shù)量和行量來(lái)重新定義數(shù)組下標(biāo)
- Dim i, j
- '利用循環(huán)把圖像所有像素的色彩信息對(duì)應(yīng)存入數(shù)組
- For i = 0 To h
- For j = 0 To w
- pixel(j, i) = bit.GetPixel(j, i)
- Next
- Next
- Dim content As String '定義一個(gè)字符串來(lái)存放要寫(xiě)入html的內(nèi)容
- content = toweb(w, h, pixel) '生成寫(xiě)入html的內(nèi)容
- Dim y As Boolean '定義一個(gè)邏輯變量來(lái)判斷是否寫(xiě)入成功
- y = SaveTextFile("c:\999.htm", content) '寫(xiě)入html文件
- If y Then MsgBox("ok!")
- End Sub
- '得到一個(gè)RGB信息的相應(yīng)WEB代碼
- Private Function GetWEBColorinfo()Function GetWEBColorinfo(ByVal x As Color) As String
- Dim r, g, b As String
- r = Hex(CInt(x.R)) '取得一個(gè)像素色彩信息中的R信息,轉(zhuǎn)成16進(jìn)制后存成字符串型
- g = Hex(CInt(x.G)) '取得一個(gè)像素色彩信息中的R信息,轉(zhuǎn)成16進(jìn)制后存成字符串型
- b = Hex(CInt(x.B)) '取得一個(gè)像素色彩信息中的R信息,轉(zhuǎn)成16進(jìn)制后存成字符串型
- '如果不足兩位的在前面加0,因?yàn)閃EB色彩表示應(yīng)為#+R(兩位16進(jìn)制)+G(兩位16進(jìn)制)+B(兩位16進(jìn)制)
- 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
- '生成要寫(xiě)處html文件的字符串,即html文件的內(nèi)容
- Private Function toweb()Function toweb(ByVal w As Integer, ByVal h As Integer, ByVal pixel As Color(,)) As String
- Dim html As String
- html = "
傲風(fēng)圖像網(wǎng)頁(yè)生成 " & vbCrLf - Dim i, j
- For i = 0 To h
- For j = 0 To w
- htmlhtml = html & " color='" & GetWEBColorinfo(pixel(j, i)) & "'>" & Int(Rnd(10) * 10) & Int(Rnd(10) * 10) & ""
- Next
- htmlhtml = html & "
" & vbCrLf- Next
- htmlhtml = html & ""
- Return html
- End Function
- '寫(xiě)入文件函數(shù)
- 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
以上就是將圖像轉(zhuǎn)成HTML文件,VB.NET代碼。
分享標(biāo)題:詳細(xì)VB.NET代碼之圖像轉(zhuǎn)成HTML文件
文章位置:http://www.dlmjj.cn/article/dhpiode.html


咨詢
建站咨詢
