Author Topic: P/Invoke the acdbDisplayPreviewFromDwg function  (Read 4336 times)

0 Members and 1 Guest are viewing this topic.

jvicario

  • Guest
P/Invoke the acdbDisplayPreviewFromDwg function
« on: June 17, 2008, 01:09:09 PM »
Which is the correct syntax to P/Invoke the acdbDisplayPreviewFromDwg function, specially the third argument?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: P/Invoke the acdbDisplayPreviewFromDwg function
« Reply #1 on: June 18, 2008, 04:25:43 AM »

You sure you don't mean
the Database.ThumbnailBitmap Property
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8833
  • AKA Daniel
Re: P/Invoke the acdbDisplayPreviewFromDwg function
« Reply #2 on: June 18, 2008, 09:12:08 AM »
I dunno, for some reason the color didn’t take.   :mrgreen:
Here’s what I have anyway

Code: [Select]
   [DllImport("acdb17.dll", EntryPoint = "?acdbDisplayPreviewFromDwg@@YA_NPB_WPAXPBK@Z",
      CallingConvention = CallingConvention.Cdecl,CharSet = CharSet.Unicode)]
    extern static private bool DisplayPreviewFromDwg(string filename,IntPtr hwnd,int color);

    private void Form_Shown(object sender, EventArgs e)
    {
      int intColor = System.Drawing.ColorTranslator.ToWin32(Color.White);
      bool res = DisplayPreviewFromDwg("c:\\64.dwg", this.Handle, intColor);
    }


It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8833
  • AKA Daniel
Re: P/Invoke the acdbDisplayPreviewFromDwg function
« Reply #3 on: June 18, 2008, 09:31:35 AM »
Kerry is right though, Database.ThumbnailBitmap might be a better choice

jvicario

  • Guest
Re: P/Invoke the acdbDisplayPreviewFromDwg function
« Reply #4 on: June 18, 2008, 02:04:19 PM »
Thank you Daniel, but this syntax does not work: the third parameter of the (P/Invoke) function is ignored and the thumbnail background color in my PictureBox control always is white. :x

jvicario

  • Guest
Re: P/Invoke the acdbDisplayPreviewFromDwg function
« Reply #5 on: June 22, 2008, 06:32:56 AM »
I believe that I have found a solution to P/Invoke the acdbDisplayPreviewFromDwg function using unsafe code (code using pointers) in C#. Here's the sample code:

Code: [Select]
      // Compile with: /unsafe
      [DllImport("acdb17.dll", CallingConvention = CallingConvention.Cdecl,
         EntryPoint = "?acdbDisplayPreviewFromDwg@@YA_NPB_WPAXPBK@Z")]
      private static extern unsafe bool acdbDisplayPreviewFromDwg(
         [MarshalAs(UnmanagedType.LPWStr)] string pszDwgfilename,
         IntPtr pPreviewWnd, uint* pBgColor);

      // This code works correctly
      private unsafe void DlgPreview_Paint(object sender, PaintEventArgs e)
      {
         string str = "C:\\AutoCAD 2009\\Sample\\colorwh.dwg";
         // Color mBgColor = Color.Black;
         // PictureBox pbImage;
         uint clr = (uint)mBgColor.R +
            (((uint)mBgColor.G) << 8) +
            (((uint)mBgColor.B) << 16);
         acdbDisplayPreviewFromDwg(str, pbImage.Handle, &clr);
      }


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: P/Invoke the acdbDisplayPreviewFromDwg function
« Reply #6 on: June 22, 2008, 07:04:59 AM »

Thanks for coming back and posting your solution
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8833
  • AKA Daniel
Re: P/Invoke the acdbDisplayPreviewFromDwg function
« Reply #7 on: June 22, 2008, 07:23:06 AM »
...(code using pointers) ...

Ah!  Great find  8-)
in that case this will work as well . note the ref

Code: [Select]
    [DllImport("acdb17.dll", EntryPoint = "?acdbDisplayPreviewFromDwg@@YA_NPB_WPAXPBK@Z",
      CallingConvention = CallingConvention.Cdecl,CharSet = CharSet.Unicode)]
    extern static private bool DisplayPreviewFromDwg(string filename,IntPtr hwnd,ref int color);

    private void Form_Shown(object sender, EventArgs e)
    {
      int intColor = System.Drawing.ColorTranslator.ToWin32(Color.White);
      bool res = DisplayPreviewFromDwg("c:\\64.dwg", this.Handle,ref intColor);
    }

jvicario

  • Guest
Re: P/Invoke the acdbDisplayPreviewFromDwg function
« Reply #8 on: June 22, 2008, 07:58:41 AM »
Thanks Daniel, your syntax (with ref) works well and I don't need to use unsafe code.  :-)

Chumplybum

  • Newt
  • Posts: 97
Re: P/Invoke the acdbDisplayPreviewFromDwg function
« Reply #9 on: June 25, 2008, 02:33:53 AM »
hi all, just go this working... but, the image comes out quiet pixelatted.

is this a form sizing issue or is this the quality of the image return from the pinvoke call????

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8833
  • AKA Daniel
Re: P/Invoke the acdbDisplayPreviewFromDwg function
« Reply #10 on: June 25, 2008, 04:13:05 AM »
A quote from the docs

Quote
This function obtains the preview image (if any) of the drawing specified by pszDwgfilename and displays it in the window identified by the HWND argument pPreviewWnd.

The image size varies up to a maximum size no larger than 256 x 188 pixels. If the pPreviewWnd window is larger than the image, then the image will be stretched to fit. But, if the window is smaller than the image, the image will not be adjusted and will spill out beyond the window's borders.

If it is non-null, pBgColor must be a long that represents an RGB color. The format for the long is the format used by the Windows SDK COLORREF type.

Returns true if successful; otherwise, it returns false.