I have a Access database that is used by design engineers to manage their AutoCad revisions. The database uses an activex control to display a thumbnail of the AutoCad drawing, but it is not compatible with the new 2013 DWG format which uses .png instead of .bmp thumbnails.
As far as I can tell the activex control is not being manitained and so I need to find an another way to display the old and new DWG thumnails.
I found some very excellent .NET VB code originally posted by Keith, here on this forum, that works with both old and new DWG formats.
The problem I have is integrating .Net VB with VBa and the Access database.
I am attempting to package the .Net VB code into a .dll which I hope I will be able to use from Access VBa, but I'm having problems with it. I can't seem to access the GetBitmap function when I package the code as a .dll
I've compiled it to register for COM interop and made the assembly COM-visible.
I'm a bit of a newby when it comes to .NET VB and I'm not entirely sure that my plan will work, but I thought it worth a go. If anyone can offer some advice on this it would be much appreciated.
Imports System.Drawing
Imports System.IO
Public Class ThumbnailImage
' 2011 Copyright (C) jgr=&jgr, via http://www.theswamp.org
' 2012 (me): Added code to read PNG Thumbnails from DWG (2013 file format)
Public Class Thumbnail
Private Sub New()
End Sub
Public Function GetBitmap(ByVal fileName As String) As Bitmap
Using fs As New FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Using br As New BinaryReader(fs)
fs.Seek(&HD, SeekOrigin.Begin)
fs.Seek(&H14 + br.ReadInt32(), SeekOrigin.Begin)
Dim bytCnt As Byte = br.ReadByte()
If bytCnt <= 1 Then
Return Nothing
End If
Dim imageHeaderStart As Integer
Dim imageHeaderSize As Integer
Dim imageCode As Byte
For i As Short = 1 To bytCnt
imageCode = br.ReadByte()
imageHeaderStart = br.ReadInt32()
imageHeaderSize = br.ReadInt32()
If imageCode = 2 Then ' BMP Preview (2012 file format)
' BITMAPINFOHEADER (40 bytes)
br.ReadBytes(&HE)
'biSize, biWidth, biHeight, biPlanes
Dim biBitCount As UShort = br.ReadUInt16()
br.ReadBytes(4)
'biCompression
Dim biSizeImage As UInteger = br.ReadUInt32()
'br.ReadBytes(0x10); //biXPelsPerMeter, biYPelsPerMeter, biClrUsed, biClrImportant
'-----------------------------------------------------
fs.Seek(imageHeaderStart, SeekOrigin.Begin)
Dim bitmapBuffer As Byte() = br.ReadBytes(imageHeaderSize)
Dim colorTableSize As UInteger = CUInt(Math.Truncate(If((biBitCount < 9), 4 * Math.Pow(2, biBitCount), 0)))
Using ms As New MemoryStream()
Using bw As New BinaryWriter(ms)
bw.Write(CUShort(&H4D42))
bw.Write(54UI + colorTableSize + biSizeImage)
bw.Write(New UShort())
bw.Write(New UShort())
bw.Write(54UI + colorTableSize)
bw.Write(bitmapBuffer)
Return New Bitmap(ms)
End Using
End Using
ElseIf imageCode = 6 Then ' PNG Preview (2013 file format)
fs.Seek(imageHeaderStart, SeekOrigin.Begin)
Using ms As New MemoryStream
fs.CopyTo(ms, imageHeaderStart)
Dim img = Image.FromStream(ms)
Return img
End Using
ElseIf imageCode = 3 Then
Return Nothing
End If
Next
End Using
End Using
Return Nothing
End Function
End Class
End Class