【问题标题】:How to convert IntPtr of HIMAGELIST to Bitmap/Image如何将 HIMAGELIST 的 IntPtr 转换为位图/图像
【发布时间】:2010-07-30 18:32:19
【问题描述】:

我正在尝试使用来自非托管 dll 的 HIMAGELIST,它给我的结果是 IntPtr。

我有没有办法将此 IntPtr 转换为位图或图像,以便我可以将其用于 Winforms 按钮,如:

myButton.Image = intPtrImage

【问题讨论】:

    标签: c# .net winforms pointers unmanaged


    【解决方案1】:

    您需要为列表中的每个图像调用ImageList_GetIcon - 这将为您提供一个易于加载到Bitmap 的HICON

    【讨论】:

    • 谢谢,之后如何将 HICON 加载到图像中?使用 Bitmap.FromHbitmap?但是 HICON 也将是 IntPtr?
    • 谢谢,我也被 ImageList_GetIcon 的 flags 参数卡住了。我该如何指定它?我想我需要 ILD_NORMAL。
    • 我找到了,是 public const int ILD_NORMAL = 0x0000;
    【解决方案2】:

    为此使用此功能:

    private const string Gdi32Dll = @".\gdi32.dll";
    [DllImport(Gdi32Dll, ExactSpelling = true)]
    internal static extern int SetDIBitsToDevice(
                                                      IntPtr hdc,
                                                      int xdst,
                                                      int ydst,
                                                      int width,
                                                      int height,
                                                      int xsrc,
                                                      int ysrc,
                                                      int start,
                                                      int lines,
                                                      IntPtr bitsptr,
                                                      IntPtr bmiptr,
                                                      int color);
    public static Bitmap BitmapFromDIB(IntPtr dibPtrArg)
    {
       Bitmapinfoheader bmiHeader;
       IntPtr pixptr = IntPtr.Zero;
       GetPixelInfo(dibPtrArg, out pixptr, out bmiHeader);
       Bitmap bitMap = new Bitmap(bmiHeader.biWidth, bmiHeader.biHeight);
       Graphics scannedImageGraphics = Graphics.FromImage(bitMap);
       IntPtr hdc = scannedImageGraphics.GetHdc();
       SetDIBitsToDevice(
           hdc,
           0, // XDest
           0, // YDest
           bmiHeader.biWidth,
           bmiHeader.biHeight,
           0, // XSrc
           0, // YSrc
           0, // uStartScan
           bmiHeader.biHeight, // cScanLines
           pixptr, // lpvBits
           dibPtrArg, // lpbmi
           DibRgbColors); // 0 = literal RGB values rather than palette
       scannedImageGraphics.ReleaseHdc(hdc);
       return bitMap;
    }
    
    private static void GetPixelInfo(IntPtr bmpptr, out IntPtr pix, out Bitmapinfoheader bmi)
    {
       bmi = new Bitmapinfoheader();
       Marshal.PtrToStructure(bmpptr, bmi); // copy into struct.
       if (bmi.biSizeImage == 0)
       {
           bmi.biSizeImage = ((((bmi.biWidth * bmi.biBitCount) + 31) & ~31) >> 3) *        bmi.biHeight;
    
       }
       int p = bmi.biClrUsed;
       if ((p == 0) && (bmi.biBitCount <= 8))
       {
           p = 1 << bmi.biBitCount;
       }
       pix = (IntPtr)((p * 2) + bmi.biSize + (int)bmpptr);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 2014-01-26
      • 2015-02-17
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多