我帮你改了一下,复制到全局代码中用:
Private Structure BITMAPFILEHEADER
Dim bfType As Short
Dim bfSize As Integer
Dim bfReserved1 As Short
Dim bfReserved2 As Short
Dim bfOffBits As Integer
End Structure
Public Function GetDwgImage(ByVal FileName As String) As Image
If Not io.File.Exists(FileName) Then Exit Function
Dim DwgF As io.FileStream '文件流
Dim PosSentinel As Integer '文件描述块的位置
Dim br As io.BinaryReader '读取二进制文件
Dim TypePreview As Integer '缩略图格式
Dim PosBMP As Integer '缩略图位置
Dim LenBMP As Integer '缩略图大小
Dim biBitCount As Short '缩略图比特深度
Dim biH As BITMAPFILEHEADER 'BMP文件头,DWG文件中不包含位图文件头,要自行加上去
Dim BMPInfo() As Byte '包含在DWG文件中的BMP文件体
Dim BMPF As New io.MemoryStream '保存位图的内存文件流
Dim bmpr As New io.BinaryWriter(BMPF) '写二进制文件类
Dim myImg As Image
Try
DwgF = New io.FileStream(FileName, io.FileMode.Open, io.FileAccess.Read) '文件流
br = New io.BinaryReader(DwgF)
DwgF.Seek(13, io.SeekOrigin.Begin) '从第十三字节开始读取
PosSentinel = br.ReadInt32 '第13到17字节指示缩略图描述块的位置
DwgF.Seek(PosSentinel + 30, io.SeekOrigin.Begin) '将指针移到缩略图描述块的第31字节
TypePreview = br.ReadByte '第31字节为缩略图格式信息,2 为BMP格式,3为WMF格式
Select Case TypePreview
Case 1
Case 2, 3
PosBMP = br.ReadInt32 'DWG文件保存的位图所在位置
LenBMP = br.ReadInt32 '位图的大小
DwgF.Seek(PosBMP + 14, io.SeekOrigin.Begin) '移动指针到位图块
biBitCount = br.ReadInt16 '读取比特深度
DwgF.Seek(PosBMP, io.SeekOrigin.Begin) '从位图块开始处读取全部位图内容备用
BMPInfo = br.ReadBytes(LenBMP) '不包含文件头的位图信息
br.Close()
DwgF.Close()
With biH '建立位图文件头
.bfType = &H4D42
If biBitCount < 9 Then .bfSize = 54 + 4 * (2 ^ biBitCount) + LenBMP Else .bfSize = 54 + LenBMP
.bfReserved1 = 0 '保留字节
.bfReserved2 = 0 '保留字节
.bfOffBits = 14 + &H28 + 1024 '图像数据偏移
End With
'以下开始写入位图文件头
bmpr.Write(biH.bfType) '文件类型
bmpr.Write(biH.bfSize) '文件大小
bmpr.Write(biH.bfReserved1) '0
bmpr.Write(biH.bfReserved2) '0
bmpr.Write(biH.bfOffBits) '图像数据偏移
bmpr.Write(BMPInfo) '写入位图
BMPF.Seek(0, io.SeekOrigin.Begin) '指针移到文件开始处
myImg = Image.FromStream(BMPF) '创建位图文件对象
Return myImg
bmpr.Close()
BMPF.Close()
End Select
Catch ex As Exception
Return Nothing
End Try
End Function