62,046
社区成员
发帖
与我相关
我的任务
分享
IntPtr bmpptr = GlobalLock(img);
IntPtr pixptr = GetPixelInfo(bmpptr);
Bitmap bitmap = BitmapFromDIB(bmpptr, pixptr);
[DllImport("kernel32.dll", ExactSpelling = true)]
internal static extern IntPtr GlobalLock(IntPtr handle);
/// <summary>
/// 获得内存图片像素信息
/// </summary>
/// <param name="bmpptr">内存图片指针</param>
/// <returns></returns>
protected IntPtr GetPixelInfo(IntPtr bmpptr)
{
BITMAPINFOHEADER bmi = new BITMAPINFOHEADER();
Marshal.PtrToStructure(bmpptr, bmi);
Rectangle bmprect = new Rectangle(0, 0, 0, 0); ;
bmprect.X = bmprect.Y = 0;
bmprect.Width = bmi.biWidth;
bmprect.Height = bmi.biHeight;
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;
p = (p * 4) + bmi.biSize + (int)bmpptr;
return (IntPtr)p;
}
/// <summary>
/// 从DIB获得图片指针
/// </summary>
/// <param name="pDIB"></param>
/// <param name="pPix"></param>
/// <returns></returns>
public static Bitmap BitmapFromDIB(IntPtr pDIB, IntPtr pPix)
{
MethodInfo mi = typeof(Bitmap).GetMethod("FromGDIplus",
BindingFlags.Static | BindingFlags.NonPublic);
if (mi == null)
return null; // (permission problem)
IntPtr pBmp = IntPtr.Zero;
int status = GdipCreateBitmapFromGdiDib(pDIB, pPix, out pBmp);
if ((status == 0) && (pBmp != IntPtr.Zero)) // success
return (Bitmap)mi.Invoke(null, new object[] { pBmp });
else
return null; // failure
}