TheSwamp
Code Red => .NET => Topic started by: frostrap on March 03, 2009, 12:48:37 PM
-
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
-
Just a guess, what about the Windows Explorer thumbs.db?
Thmubnails are viewable in Explorer....
-
I've tried several dwg thumbnail/preview options.
My home-grown control did fine until drawing file format changed in 2007. Then it slowed way down. Who knows what will work with the new 2010 file format.
I ended up using DWGTHUMBNAILLib from Autodesk.
-
Just a guess, what about the Windows Explorer thumbs.db?
Thmubnails are viewable in Explorer....
You can't count on Thumbs.db being there for you. The file is only generated on demand (when Thumbnail or Filmstrip view is used on the folder in Explorer) and is easily deleted. Somewhere in that process, AcThumbnail16.dll is involved. It's a shell extension for the DWG file type.
I'm pretty sure, though, that DWGPROPS are stored in a separate stream on the DWG file (ie. Structured Storage). I wonder if the Thumbnail is stored similarly or in the same stream? Would make sense, but I'm now in way over my head.
-
I can't offer the slightest bit of advice.....sorry.
However, I am feeling nosey and would like to know exactly, or in general, what your program needs these for and ultimately what it does?
Cheers.
-
Just a guess, what about the Windows Explorer thumbs.db?
Thmubnails are viewable in Explorer....
You can't count on Thumbs.db being there for you. The file is only generated on demand (when Thumbnail or Filmstrip view is used on the folder in Explorer) and is easily deleted. Somewhere in that process, AcThumbnail16.dll is involved. It's a shell extension for the DWG file type.
I'm pretty sure, though, that DWGPROPS are stored in a separate stream on the DWG file (ie. Structured Storage). I wonder if the Thumbnail is stored similarly or in the same stream? Would make sense, but I'm now in way over my head.
DWGPROPS in 2007 or later are stored as header variables
(e.g., system variables that are saved with the dwg file).
There's no OLE Structured Storage stream.
-
Thanks for the update, Tony. I skipped from 2005 to 2009. Missed the change, I guess.
[/quote]
DWGPROPS in 2007 or later are stored as header variables
(e.g., system variables that are saved with the dwg file).
There's no OLE Structured Storage stream.
[/quote]
-
The option to place it into the header of the file can be turned off in the Options of AutoCAD also.
-
I've shown a way that this can be done very very quickly in this thread...
http://www.theswamp.org/index.php?topic=26621.0;all
I've written applications in this method that create libraries of bitmaps retrieved from drawings on-the-fly.