Returns the preview bitmap from a DWG file, without opening the file (database)
For AutoCAD 2007 ~ 2011. 32-bit and 64-bit
VB.NET:
Imports System.Drawing
Imports System.Runtime.InteropServices
#If ACAD_17 Then
Const ACDBDLL_NAME = "acdb17.dll"
#Else
Const ACDBDLL_NAME = "acdb18.dll"
#End If
<System.Runtime.InteropServices.DllImportAttribute(ACDBDLL_NAME, CallingConvention:=CallingConvention.Cdecl, CharSet:=CharSet.Unicode, EntryPoint:="?acdbGetPreviewBitmapFromDwg@@YA_NPB_WPAPAUHBITMAP__@@PAPAUHPALETTE__@@@Z")> _
Private Shared Function acdbGetPreviewBitmapFromDwg32(<System.Runtime.InteropServices.InAttribute(), System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)> ByVal pszDwgfilename As String, ByRef pPreviewBmp As System.IntPtr, ByRef pRetPal As System.IntPtr) As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.I1)> Boolean
End Function
<System.Runtime.InteropServices.DllImportAttribute(ACDBDLL_NAME, CallingConvention:=CallingConvention.Cdecl, CharSet:=CharSet.Unicode, EntryPoint:="?acdbGetPreviewBitmapFromDwg@@YA_NPEB_WPEAPEAUHBITMAP__@@PEAPEAUHPALETTE__@@@Z")> _
Private Shared Function acdbGetPreviewBitmapFromDwg64(<System.Runtime.InteropServices.InAttribute(), System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)> ByVal pszDwgfilename As String, ByRef pPreviewBmp As System.IntPtr, ByRef pRetPal As System.IntPtr) As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.I1)> Boolean
End Function
Private Function GetPreviewBitmapFromDwg(ByVal fileName As String) As Bitmap
Dim hBmp As System.IntPtr
Dim hPal As System.IntPtr
If IntPtr.Size > 4 Then ' 64-bit
acdbGetPreviewBitmapFromDwg64(fileName, hBmp, hPal)
Else ' 32-bit
acdbGetPreviewBitmapFromDwg32(fileName, hBmp, hPal)
End If
If hBmp.Equals(IntPtr.Zero) Then
Return Nothing
Else
Return Bitmap.FromHbitmap(hBmp)
End If
End Function
CSHARP (using a translator)
#if ACAD_17
private const string ACDBDLL_NAME = "acdb17.dll";
#else
private const string ACDBDLL_NAME = "acdb18.dll";
#endif
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.I1)]
[System.Runtime.InteropServices.DllImportAttribute(ACDBDLL_NAME, CallingConvention=CallingConvention.Cdecl, CharSet=CharSet.Unicode, EntryPoint="?acdbGetPreviewBitmapFromDwg@@YA_NPB_WPAPAUHBITMAP__@@PAPAUHPALETTE__@@@Z")]
private extern static bool acdbGetPreviewBitmapFromDwg32([System.Runtime.InteropServices.InAttribute(), System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string pszDwgfilename, ref System.IntPtr pPreviewBmp, ref System.IntPtr pRetPal);
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.I1)]
[System.Runtime.InteropServices.DllImportAttribute(ACDBDLL_NAME, CallingConvention=CallingConvention.Cdecl, CharSet=CharSet.Unicode, EntryPoint="?acdbGetPreviewBitmapFromDwg@@YA_NPEB_WPEAPEAUHBITMAP__@@PEAPEAUHPALETTE__@@@Z")]
private extern static bool acdbGetPreviewBitmapFromDwg64([System.Runtime.InteropServices.InAttribute(), System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string pszDwgfilename, ref System.IntPtr pPreviewBmp, ref System.IntPtr pRetPal);
private Bitmap GetPreviewBitmapFromDwg(string fileName)
{
System.IntPtr hBmp = default(System.IntPtr);
System.IntPtr hPal = default(System.IntPtr);
if (IntPtr.Size > 4) // 64-bit
{
acdbGetPreviewBitmapFromDwg64(fileName, ref hBmp, ref hPal);
}
else // 32-bit
{
acdbGetPreviewBitmapFromDwg32(fileName, ref hBmp, ref hPal);
}
if (hBmp.Equals(IntPtr.Zero))
{
return null;
}
else
{
return Bitmap.FromHbitmap(hBmp);
}
}