Very interesting. I see that warning in the documentation. I'm glad you said something, or I probably wouldn't have understood the importance of the warning. That may very well explain why I'm getting memory violation errors.
I admit I don't fully understand the details about what's going on here. The documentation makes it sound as though you are never supposed to set buildDefaultDrawing to true. When would be a proper time to set the that parameter to true? There must be a time, or it wouldn't be an option.
The code is now as follows:
Public Function DrawThumbnail(ByVal DWGPath As String, ByVal PictureBoxWidth As Single, ByVal PictureBoxHeight As String, _
Optional ByVal InvertThumbs As Boolean = False) As Bitmap
Dim mytransman As DatabaseServices.TransactionManager
Dim myTrans As DatabaseServices.Transaction
Dim myDB As DatabaseServices.Database
Try
myDB = New Autodesk.AutoCAD.DatabaseServices.Database(False, True)
myDB.ReadDwgFile(DWGPath, IO.FileShare.ReadWrite, True, "")
mytransman = myDB.TransactionManager
myTrans = mytransman.StartTransaction
' 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
bm_dest.Dispose()
bm_source.Dispose()
gr_dest.Dispose()
Catch ex As Exception
MsgBox(ex.Message & ControlChars.CrLf & ex.StackTrace)
Return Nothing
Finally
myTrans.Dispose()
myDB.Dispose()
End Try