明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2558|回复: 3

求助如何提取dwg文件中的bmp文件,预览dwg文件

[复制链接]
发表于 2007-9-4 13:24:00 | 显示全部楼层 |阅读模式

求助如何提取dwg文件中的bmp文件,预览dwg文件

谢谢

发表于 2007-9-6 07:02:00 | 显示全部楼层
DwgThumbnail CONTROL
发表于 2007-9-11 09:30:00 | 显示全部楼层

you kongjian!!

发表于 2007-9-11 16:42:00 | 显示全部楼层

Option Explicit


Private Type BITMAPINFOHEADER
        biSize As Long
        biWidth As Long
        biHeight As Long
        biPlanes As Integer
        biBitCount As Integer
        biCompression As Long
        biSizeImage As Long
        biXPelsPerMeter As Long
        biYPelsPerMeter As Long
        biClrUsed As Long
        biClrImportant As Long
End Type
Private Type BITMAPFILEHEADER
        bfType As Integer
        bfSize As Long
        bfReserved1 As Integer
        bfReserved2 As Integer
        bfOffBits As Long
End Type

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private mvarPicture As PictureBox
Dim mvarBuffer() As Long
Dim mvarPictureType As Integer
Dim mvarFileName As String
Public Function GetDrawingPreview(dwgPath As String, Tmppath As String) As Boolean
On Error GoTo errH
    Dim fh As Integer, tmpBuffer() As Byte, i As Long
    Dim biHeader As BITMAPINFOHEADER, ver As Integer
    Dim sentinel As Long, nOffset As Long, imageSize As Long
    Dim tmp As Long, previewType As Byte, retval As Boolean
    mvarFileName = Tmppath
    If Dir$(dwgPath, vbNormal) = "" Then
'        mvarErrorCode = vbObjectError + 6
'        mvarErrorText = "File not found"
        retval = False
    Else
        fh = FreeFile
        Open dwgPath For Binary As #fh
        ' Read the first 18 bytes of the drawing
        ReDim tmpBuffer(0 To 17)
        Get #fh, , tmpBuffer
        ' Ensure that the drawing is at least R14
        ver = Val(Chr(tmpBuffer(4)) & Chr(tmpBuffer(5)))
        If ver < 14 Then
' If not, close the file and report an error
            Close #fh
'            mvarErrorCode = vbObjectError + 4
'            mvarErrorText = "Cannot process pre-14 drawings"
            retval = False
        Else
            ' Bytes 13 through 17 contain the starting position for a 39-byte
            ' block of information that desribes the preview.
            ' Thanks to Denis Gagne for this tip.
            CopyMemory sentinel, tmpBuffer(13), 4
            'Retrieve the preview descriptor block
            ReDim tmpBuffer(18 To sentinel + 39)
            'CopyMemory sentinel6, tmpBuffer(13), 2
            Get #fh, , tmpBuffer
            ' Determine the type of preview (if any) and act accordingly.
            ' Thanks to Paul Marshall for this tip.
                        mvarPictureType = tmpBuffer(UBound(tmpBuffer) - 9)
                        If mvarPictureType > 0 Then
                                ' Of the 39 bytes, bytes 31 through 34 contain the
                                ' starting location of the preview image data
                                CopyMemory nOffset, tmpBuffer(UBound(tmpBuffer) - 8), 4
                                ' Bytes 35 through 39 contain the size of the image data
                                CopyMemory imageSize, tmpBuffer(UBound(tmpBuffer) - 4), 4
                                tmp = UBound(tmpBuffer)
                                ' Retrieve the preview data
                                ReDim tmpBuffer(0 To imageSize + nOffset - tmp)
                                Get #fh, , tmpBuffer
                                Close #fh
                                ReDim mvarBuffer(0 To imageSize - 1)
                                ' Isolate the image data
                                CopyMemory mvarBuffer(0), tmpBuffer(nOffset - tmp - 1), imageSize
                                ' and make a bitmap file from it
                                WriteImage
                                ' Load it

                                'Set Form1.Picture1.Picture = LoadPicture(mvarFileName)
                                ' Delete it
                                'Kill mvarFileName
                                retval = True
                         End If
        End If
    End If
errH:
    GetDrawingPreview = retval '& vbCrLf & mvarFileName
End Function
Private Sub WriteImage()
On Error Resume Next
    Dim fh As Integer, bfHeader As BITMAPFILEHEADER
    Dim biHeader As BITMAPINFOHEADER, clrTableSize As Long
    Dim i As Integer, tmp As String, pixels As Long
    'Dim mvarBuffer(10) '''''''-----------
    'If mvarPictureType = IVW_PREVIEWBITMAP Then
        ' Copy the BITMAPINFOHEADER from the buffer into a structure
        CopyMemory biHeader, mvarBuffer(0), 40
        clrTableSize = IIf(biHeader.biBitCount < 9, 4 * (2 ^ biHeader.biBitCount), 0)
        ' Fill the BITMAPFILEHEADER structure
        With bfHeader
            .bfType = &H4D42           ' Image type
            .bfSize = 54 + clrTableSize + biHeader.biSizeImage ' Length of file (in bytes)
            .bfOffBits = 54 + clrTableSize         ' Number of bytes to the start of the pixel data
        End With
    'End If
    fh = FreeFile
    Open mvarFileName For Binary As #fh
    'If mvarPictureType = IVW_PREVIEWBITMAP Then
    Put #fh, , bfHeader
    Put #fh, , mvarBuffer
    Close #fh
End Sub
我也是从网上找的

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2025-2-22 18:43 , Processed in 0.138080 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表