详细VB.NET代码之图像转成HTML文件

开发 后端
这里就图像转成HTML文件案例做出详细VB.NET代码,想知道是怎么实现的吗?文章有详细的代码和解释,看了一定会给你启发的。

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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.

以上就是将图像转成HTML文件,VB.NET代码。

【编辑推荐】

  1. VB.NET编写托盘程序经验杂谈
  2. 手把手指导VB.NET Socket编程
  3. 快速了解VB.NET可选参数
  4. 详细介绍VB.NET MyClass
  5. 自己动手用代码实现VB.NET ListView加载数据
责任编辑:田树 来源: 乐博网
相关推荐

2009-10-28 09:55:29

VB.NET MyCl

2009-10-12 15:41:09

VB.NET动态代码

2010-01-20 13:42:10

VB.NET访问INIGetPrivateP

2010-01-07 15:18:10

VB.NET常量

2009-10-13 17:03:55

VB.NET面向对象

2009-10-12 13:54:22

VB.NET Data

2009-11-02 15:45:03

VB.NET IEnu

2010-01-21 16:45:00

VB.NET继承规则

2009-10-12 15:02:51

VB.NET动态控件

2010-01-22 14:19:38

VB.NET调用jar

2009-11-10 12:42:47

VB.NET Prin

2009-10-10 16:44:52

VB.NET开发控件

2010-01-12 11:37:34

VB.NET读取图像

2009-10-13 17:16:40

VB.NET Web服

2009-10-12 12:54:58

VB.NET声明API

2009-10-15 15:04:42

VB.NET PadL

2009-11-02 09:45:23

VB.NET文件系统对

2010-01-11 11:37:08

VB.NET操作CSV

2010-01-11 11:02:27

VB.NET调用存储过

2009-10-12 16:56:36

VB.NET常量VB.NET枚举
点赞
收藏

51CTO技术栈公众号