.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to Access Raster Preview/Thumbnail Stored in DWG

12 REPLIES 12
Reply
Message 1 of 13
Anonymous
2296 Views, 12 Replies

How to Access Raster Preview/Thumbnail Stored in DWG

Hello,

Can anyone tell how to access the raster preview image stored in the dwg
from a C# .NET 2.0 program?

Warren
12 REPLIES 12
Message 2 of 13
Anonymous
in reply to: Anonymous

http://discussion.autodesk.com/search.jspa?numResults=25&inputEntered=true&source=thread-threaded%7C152&q=Thumbnail&objID=f152
Message 3 of 13
Anonymous
in reply to: Anonymous

I read most of these posts. Thank you very much! I am trying to get a
decent size thumbnail image of 640x480 pixels strictly through the Shell
(without using any ARX or other programs) with the following code:

SIZE sz = new SIZE();
sz.cx = desiredSize.Width; // set to 640
sz.cy = desiredSize.Height; // set to 480
StringBuilder location = new StringBuilder(260, 260);
int priority = 0;
int requestedColorDepth = 32;
EIEIFLAG flags = EIEIFLAG.IEIFLAG_ASPECT | EIEIFLAG.IEIFLAG_SCREEN |
EIEIFLAG.IEIFLAG_ORIGSIZE | EIEIFLAG.IEIFLAG_QUALITY;
int uFlags = (int)flags;

extractImage.GetLocation(
location,
location.Capacity,
ref priority,
ref sz,
requestedColorDepth,
ref uFlags);

extractImage.Extract(out hBmp);
if (hBmp != IntPtr.Zero)
{
// create the image object:
thumbNail = System.Drawing.Bitmap.FromHbitmap(hBmp);
// is thumbNail owned by the Bitmap?
}

thumbNail.Save (@"c:\temp\wan.jpg", ImageFormat.Jpeg);

Marshal.ReleaseComObject(extractImage);
extractImage = null;


My question is can I specify to AutoCAD through IExtractImage to give me
a true thumbnail of 640x480 pixels? The image I get back is 640x480,
however, it looks like it has been presumably scaled up from a smaller
image (see 'Thumbnail from DWG.jpg). I guess you could argue to scale up
the image to the desired resolution, but the quality then suffers. If I
run the same code on a generic jpg, the Shell will give me a true
640x480 thumbnail (see attached 'Thumbnail from JPG.jpg').

Thank you,
Warren








Alexander Rivilis wrote:
> http://discussion.autodesk.com/search.jspa?numResults=25&inputEntered=true&source=thread-threaded%7C152&q=Thumbnail&objID=f152
Message 4 of 13
Anonymous
in reply to: Anonymous

From ObjectARX SDK Documents:
The image size varies up to a maximum size no larger than 256 x 188 pixels.
Message 5 of 13
Anonymous
in reply to: Anonymous

The thumbnail image that you access via the
shell or via any other API is stored in the drawing
file, and is generated when the file is saved, and
hence, has a fixed size.

IOW, the thumbnail image generation is not
dynamic, so you can't specify any size you want,
you're basically stuck with the size it is saved
as, and any other size you get will be the result
of resampling of that stored image.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

"Warren Newhauser" wrote in message news:5709950@discussion.autodesk.com...
I read most of these posts. Thank you very much! I am trying to get a
decent size thumbnail image of 640x480 pixels strictly through the Shell
(without using any ARX or other programs) with the following code:

SIZE sz = new SIZE();
sz.cx = desiredSize.Width; // set to 640
sz.cy = desiredSize.Height; // set to 480
StringBuilder location = new StringBuilder(260, 260);
int priority = 0;
int requestedColorDepth = 32;
EIEIFLAG flags = EIEIFLAG.IEIFLAG_ASPECT | EIEIFLAG.IEIF
LAG_SCREEN |
EIEIFLAG.IEIFLAG_ORIGSIZE | EIEIFLAG.IEIFLAG_QUALITY;
int uFlags = (int)flags;

extractImage.GetLocation(
location,
location.Capacity,
ref priority,
ref sz,
requestedColorDepth,
ref uFlags);

extractImage.Extract(out hBmp);
if (hBmp != IntPtr.Zero)
{
// create the image object:
thumbNail = System.Drawing.Bitmap.FromHbitmap(hBmp);
// is thumbNail owned by the Bitmap?
}

thumbNail.Save (@"c:\temp\wan.jpg", ImageFormat.Jpe
g);

Marshal.ReleaseComObject(extractImage);
extractImage = null;


My question is can I specify to AutoCAD through IExtractImage to give me
a true thumbnail of 640x480 pixels? The image I get back is 640x480,
however, it looks like it has been presumably scaled up from a smaller
image (see 'Thumbnail from DWG.jpg). I guess you could argue to scale up
the image to the desired resolution, but the quality then suffers. If I
run the same code on a generic jpg, the Shell will give
me a true
640x480 thumbnail (see attached 'Thumbnail from JPG.jpg').

Thank you,
Warren








Alexander Rivilis wrote:
> http://discussion.autodesk.com/search.jspa?numResults=25&inputEntered=true&source=thread-threaded%7C152&q=Thumbnail&objID=f152
Message 6 of 13
Ed.Jobe
in reply to: Anonymous

Part of the problem is that you are referring to 640x480 as a "thumbnail". That's a LARGE thumbnail. The intention of a thumbnail is to present a small visualization. To get what you want, you need to plot the dwg to a raster format.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 7 of 13
Anonymous
in reply to: Anonymous

Agree 100% - I am looking for an arbitrary size preview without having
to write/execute any code to get it, but that does not look doable.

Thank you for your help.
Ed_Jobe wrote:
> Part of the problem is that you are referring to 640x480 as a "thumbnail". That's a LARGE thumbnail. The intention of a thumbnail is to present a small visualization. To get what you want, you need to plot the dwg to a raster format.
Message 8 of 13
Mikko
in reply to: Anonymous

With a few extra minutes on my hands, I've looked at a few
examples on this thumbnail shell stuff and found them a bit
overwhelming for what I need. Not wanting to implement
every little nuance of the Shell API, I've somewhat shortened
the code, became brain dead and am left with this ERROR:

Attempted to read or write protected memory. This is often an
indication that other memory is corrupt.

Just wondering if anybody can see the problem???? I don't think
this happened with that VBaccelerator C# code. Here's the VB code
I'm using. Thanks.

======= Windows Form with Button and PictureBox

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim img As Image = ThumOne.CADImage("c:\temp\tryme.dwg")
If Not img Is Nothing Then
PictureBox1.Image = img
End If
End Sub

======= ThumOne Class

Imports System
Imports System.Text
Imports System.Runtime.InteropServices

Public Class ThumOne

Public Structure SIZE
Public wide As Integer
Public tall As Integer
End Structure

<GuidAttribute("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1"), _
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _
Public Interface IExtractImage
Sub GetLocation(ByVal pszPathBuffer As IntPtr, ByVal cch As Integer, ByRef pdwPriority As Integer, ByRef prgSize As SIZE, ByVal dwRecClrDepth As Integer, ByRef pdwFlags As Integer)
Sub Extract(ByRef phBmpThumbnail As IntPtr)
End Interface

<GuidAttribute("000214E6-0000-0000-C000-000000000046"), _
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _
Public Interface IShellFolder
Sub ParseDisplayName(ByVal hWnd As IntPtr, ByVal pbc As IntPtr, ByVal pszDisplayName As String, ByRef pchEaten As Integer, ByRef ppidl As System.IntPtr, ByRef pdwAttributes As Integer)
Sub EnumObjects(ByVal hwndOwner As IntPtr, <MarshalAs(UnmanagedType.U4)> ByVal grfFlags As Integer, <Out()> ByRef ppenumIDList As IntPtr)
Sub BindToObject(ByVal pidl As IntPtr, ByVal pbcReserved As IntPtr, ByRef riid As Guid, ByRef ppvOut As IShellFolder)
Sub BindToStorage(ByVal pidl As IntPtr, ByVal pbcReserved As IntPtr, ByRef riid As Guid, <Out()> ByVal ppvObj As IntPtr)
<PreserveSig()> _
Function CompareIDs(ByVal lParam As IntPtr, ByVal pidl1 As IntPtr, ByVal pidl2 As IntPtr) As Integer
Sub CreateViewObject(ByVal hwndOwner As IntPtr, ByRef riid As Guid, ByVal ppvOut As Object)
Sub GetAttributesOf(ByVal cidl As Integer, ByVal apidl As IntPtr, <MarshalAs(UnmanagedType.U4)> ByRef rgfInOut As Integer)
Sub GetUIObjectOf(ByVal hwndOwner As IntPtr, ByVal cidl As Integer, ByRef apidl As IntPtr, ByRef riid As Guid, <Out()> ByVal prgfInOut As Integer, <Out(), MarshalAs(UnmanagedType.IUnknown)> ByRef ppvOut As Object)
End Interface

<DllImport("shell32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function SHGetDesktopFolder(<Out()> ByRef ppshf As IShellFolder) As Integer
End Function

Public Shared Function CADImage(ByVal cadFileName As String) As Image
Dim DirShell = New Guid("000214E6-0000-0000-C000-000000000046")
Dim FileShell = New Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1")
Dim cadDir As IShellFolder = Nothing
Dim dwgFile As IShellFolder = Nothing
Dim xImg As IExtractImage = Nothing
Dim pidl As IntPtr
Dim filePidl As IntPtr
Dim NameDir = IO.Path.GetDirectoryName(cadFileName)
Dim CADName = IO.Path.GetFileName(cadFileName)
Dim img As IntPtr
Dim imgLoc As IntPtr
Dim size As SIZE
size.wide = 100
size.tall = 75
SHGetDesktopFolder(cadDir)
cadDir.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, NameDir, 0, pidl, 0)
cadDir.BindToObject(pidl, IntPtr.Zero, DirShell, dwgFile)
dwgFile.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, CADName, 0, filePidl, 0)
dwgFile.GetUIObjectOf(IntPtr.Zero, 1, filePidl, FileShell, 0, xImg)
imgLoc = Marshal.AllocHGlobal(0)
xImg.GetLocation(imgLoc, 260, 0, size, 2, 0)
xImg.Extract(img)
Try
CADImage = Image.FromHbitmap(img)
Catch
CADImage = Nothing
Finally
Marshal.FreeHGlobal(imgLoc)
Marshal.FreeCoTaskMem(pidl)
Marshal.FreeCoTaskMem(filePidl)
End Try
End Function

End Class
Message 9 of 13
Mikko
in reply to: Anonymous

This line should read:

imgLoc = Marshal.AllocHGlobal(260)

just in case anyone uses this.
Message 10 of 13
rtejada
in reply to: Anonymous

Hi Mikko,
I ran into the same problem, I was wondering if you found the answer.

Thanks
Raul
Message 11 of 13
ivst
in reply to: Anonymous

I have successfully used this code for many years (since Inventor 2009) to view .ipt thumbnails on a form.

However, after I installed Inventor 2012 the code has stopped working.

If I start Inventor 2011 and run the code, the thumbnail is extracted successfully.

If I start Inventor 2012 and run the code, it fails at the xImg.Extract(img) line (see attachment).

If I again start Inventor 2011 and run the code, the thumbnail is extracted successfully.

etc.

Are there anyone who can help in solving this?

Message 12 of 13
Mikko
in reply to: ivst

 
Message 13 of 13
mohnston
in reply to: Mikko

Guess what happened when I tried this on .dwg files?

 

OK, I'll tell you, I got previews.

Dwg files were save as 2000 or 2007 file type.

CAD Programming Solutions

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost