将图像转成HTML文件,VB.net源代码

上次发过一个软件,见下文
http://www.cnblogs.com/aowind/archive/2005/03/05/113429.html
其软件的功能就是将一个图像转成HTML文件,就是用一些自定义的数字通过不同的色彩来表现出这个图像
其效果如下:
code.gif

经过小弟研究了一下,在vb.net中写出了相同实现功能的代码
功能实现主要是应用到system.drawing.bitmap,和其方法getpixel()
主要代码如下:

ExpandedBlockStart.gif ContractedBlock.gif Private   Sub Button1_Click() Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click
InBlock.gif        
Dim bit As System.Drawing.Bitmap
InBlock.gif        bit 
= bit.FromFile("c:\aowindme.bmp"'读取一个图像文件
InBlock.gif
        Dim w, h As Integer
InBlock.gif        w 
= bit.Width - 1 '取得图像每行的像素量
InBlock.gif
        h = bit.Height - 1 '取得图像的行数
InBlock.gif
        Dim pixel As System.Drawing.Color(,) '定义一个类型为系统色彩型的二维数组,来存放图片的所有像系的色彩信息
InBlock.gif
        pixel = New System.Drawing.Color(w, h) {} '根据图像的像系每行数量和行量来重新定义数组下标
InBlock.gif
        Dim i, j
InBlock.gif        
'利用循环把图像所有像素的色彩信息对应存入数组
InBlock.gif
        For i = 0 To h
InBlock.gif            
For j = 0 To w
InBlock.gif                pixel(j, i) 
= bit.GetPixel(j, i)
InBlock.gif            
Next
InBlock.gif        
Next
InBlock.gif        
Dim content As String '定义一个字符串来存放要写入html的内容
InBlock.gif
        content = toweb(w, h, pixel) '生成写入html的内容
InBlock.gif
        Dim y As Boolean '定义一个逻辑变量来判断是否写入成功
InBlock.gif
        y = SaveTextFile("c:\999.htm", content) '写入html文件
InBlock.gif
        If y Then MsgBox("ok!")
ExpandedBlockEnd.gif    
End Sub

None.gif
None.gif    
' 得到一个RGB信息的相应WEB代码
ExpandedBlockStart.gifContractedBlock.gif
     Private   Function GetWEBColorinfo() Function GetWEBColorinfo(ByVal x As Color) As String
InBlock.gif        
Dim r, g, b As String
InBlock.gif        r 
= Hex(CInt(x.R)) '取得一个像素色彩信息中的R信息,转成16进制后存成字符串型
InBlock.gif
        g = Hex(CInt(x.G)) '取得一个像素色彩信息中的R信息,转成16进制后存成字符串型
InBlock.gif
        b = Hex(CInt(x.B)) '取得一个像素色彩信息中的R信息,转成16进制后存成字符串型
InBlock.gif
        '如果不足两位的在前面加0,因为WEB色彩表示应为#+R(两位16进制)+G(两位16进制)+B(两位16进制)
InBlock.gif
        If r.Length = 1 Then r = "0" & r
InBlock.gif        
If g.Length = 1 Then g = "0" & g
InBlock.gif        
If b.Length = 1 Then b = "0" & b
InBlock.gif        
Return "#" & r & g & b
ExpandedBlockEnd.gif    
End Function

None.gif
None.gif    
' 生成要写处html文件的字符串,即html文件的内容
ExpandedBlockStart.gifContractedBlock.gif
     Private   Function toweb() Function toweb(ByVal w As IntegerByVal h As IntegerByVal pixel As Color(,)) As String
InBlock.gif        
Dim html As String
InBlock.gif        html 
= "<html><head><title>傲风图像网页生成</title></head><body bgcolor='#000000'><center>" & vbCrLf
InBlock.gif        
Dim i, j
InBlock.gif        
For i = 0 To h
InBlock.gif            
For j = 0 To w
InBlock.gif                html 
= html & "<font color='" & GetWEBColorinfo(pixel(j, i)) & "'>" & Int(Rnd(10* 10& Int(Rnd(10* 10& "</font>"
InBlock.gif
            Next
InBlock.gif            html 
= html & "<br>" & vbCrLf
InBlock.gif        
Next
InBlock.gif        html 
= html & "</center></body></html>"
InBlock.gif
        Return html
ExpandedBlockEnd.gif    
End Function

None.gif    
' 写入文件函数
ExpandedBlockStart.gifContractedBlock.gif
     Private   Function SaveTextFile() Function SaveTextFile(ByVal FilePath As StringByVal FileContent As StringAs Boolean
InBlock.gif        
Dim sw As System.IO.StreamWriter
InBlock.gif        
Try
InBlock.gif            sw 
= New System.IO.StreamWriter(FilePath, False)
InBlock.gif            sw.
Write(FileContent)
InBlock.gif            
Return True
InBlock.gif        
Catch e As Exception
InBlock.gif            
Return False
InBlock.gif        
Finally
InBlock.gif            
If Not sw Is Nothing Then sw.Close()
InBlock.gif        
End Try
ExpandedBlockEnd.gif    
End Function

还请大虾位指教!

转载于:https://www.cnblogs.com/aowind/archive/2005/03/09/115442.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值