Hello all,
I'm using the function shown below to retrieve thumbnail images from drawings. Thanks to some help on another post, the code seems to be generally stable. Unfortunately, it can also be rather slow, especially when reteiving the thumb from large files.
Right now it seems as though the entire file is being loaded into memory, which takes a lot of time. Isn't there a method for extracting the thumbnail without having to load the entire drawing file into memory first?
Is there another method for retrieving the thumbnail image from a dwg that is not so time hungry?
Looking forward to your ideas,
Joe
Public Function DrawThumbnail(ByVal DWGPath As String, ByVal PictureBoxWidth As Integer, ByVal PictureBoxHeight As Integer, _
Optional ByVal InvertThumbs As Boolean = False) As Bitmap
Dim mytransman As DatabaseServices.TransactionManager
Dim myTrans As DatabaseServices.Transaction
Dim myDB As DatabaseServices.Database
myDB = New Autodesk.AutoCAD.DatabaseServices.Database(False, True)
mytransman = myDB.TransactionManager
myTrans = mytransman.StartTransaction
Try
myDB.ReadDwgFile(DWGPath, IO.FileShare.ReadWrite, True, "")
'check to make sure the file has a thumbnail. If it doesn't, return a placeholder image.
If IsNothing(myDB.ThumbnailBitmap) Then
Dim theThumb As New Bitmap(PictureBoxWidth, PictureBoxHeight)
Dim gr As Graphics = Graphics.FromImage(theThumb)
gr.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
gr.DrawString("N/A", New Font("Verdana", 12, FontStyle.Regular), New SolidBrush(Color.Red), 15, 10)
gr.Dispose()
Return theThumb
End If
'if we're here, the dwg has a preview thumbnail
' Set the scale factor.
Dim WidthScale As Single = PictureBoxWidth / myDB.ThumbnailBitmap.Width()
Dim HeightScale As Single = PictureBoxHeight / myDB.ThumbnailBitmap.Height()
Dim theScale As Single = WidthScale
If WidthScale > HeightScale Then
theScale = HeightScale
End If
' Get the source bitmap.
Dim bm_source As New Bitmap(myDB.ThumbnailBitmap)
' Make a bitmap for the result.
Dim bm_dest As New Bitmap( _
CInt(bm_source.Width * theScale), _
CInt(bm_source.Height * theScale))
' Make a Graphics object for the result Bitmap.
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
gr_dest.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
gr_dest.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
gr_dest.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBilinear
' Copy the source image into the destination bitmap.
gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width + 1, bm_dest.Height + 1)
If InvertThumbs Then
Dim X As Integer
Dim Y As Integer
Dim r As Integer
Dim g As Integer
Dim b As Integer
For X = 0 To bm_dest.Width - 1
For Y = 0 To bm_dest.Height - 1
r = 255 - bm_dest.GetPixel(X, Y).R
g = 255 - bm_dest.GetPixel(X, Y).G
b = 255 - bm_dest.GetPixel(X, Y).B
bm_dest.SetPixel(X, Y, Color.FromArgb(r, g, b))
Next Y
Next X
End If
'Return the result.
Return bm_dest
'commit the transaction
myTrans.Commit()
'clean up the graphics resources
gr_dest.Dispose()
bm_dest.Dispose()
bm_source.Dispose()
Catch ex As Exception
MsgBox(ex.Message & ControlChars.CrLf & ex.StackTrace)
Return Nothing
Finally
myTrans.Dispose()
myDB.Dispose()
End Try
End Function