Note that there are some explanatory texts on larger screens.

plurals
  1. POgdi32.GetObject doesn't work when running 64-bit
    primarykey
    data
    text
    <p>This code works fine when running 32-bit. But when I switch to 64-bit the <code>GetObject</code> method does not work and <code>BITMAP</code> struct is empty. </p> <pre><code>IntPtr hBmp = ObtainValidBitmapHandleFromSystem(); BITMAP bmpData = new BITMAP(); /* BITMAP consists of four 32bit ints, * two 16 bit uints and one IntPtr */ * 4 + sizeof(UInt16) * 2 + IntPtr.Size; int cbBuffer = sizeof(Int32) * 4 + sizeof(UInt16) * 2 + IntPtr.Size; NativeMethods.GetObject(hBmp, cbBuffer, out bmpData); Bitmap bmp = new Bitmap(bmpData.Width, bmpData.Height, PixelFormat.Format32bppPArgb); </code></pre> <p>The native method implementation:</p> <pre><code>private static class NativeMethods { [DllImport("gdi32", CharSet = CharSet.Auto)] internal extern static int GetObject( IntPtr hgdiobj, // handle to graphics object int cbBuffer, // size of buffer for object information out BITMAP lpvObject // Should be IntPtr, but we know we will use it only for BITMAP. ); } </code></pre> <p>The BITMAP structure implementation (removed documentation to keep code compact):</p> <pre><code>[StructLayoutAttribute(LayoutKind.Sequential)] private struct BITMAP { public Int32 Type; public Int32 Width; public Int32 Height; public Int32 WidthBytes; public UInt16 Planes; public UInt16 BitsPixel; public IntPtr Bits; } </code></pre> <p>The idea behind this code is fully described in this <a href="https://stackoverflow.com/questions/4627376/use-native-hbitmap-in-c-sharp-while-preserving-alpha-channel-transparency">question</a>.</p> <p>At first I thought that the issue is caused by different size of <code>IntPtr</code> resulting in different size of <code>cbBuffer</code>, but it seems that this is not the case as changing <code>cbBuffer</code> size did not help.</p> <p>What is the correct way to use GDI's <code>GetObject</code> method on 64-bit system?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload