private Bitmap GetBitmap(int index, ImageListDrawItemConstants flags) { var bitmapSize = GetImageListIconSize(); var bitmap = new Bitmap(bitmapSize.Width, bitmapSize.Height); using (var g = Graphics.FromImage(bitmap)) { try { g.FillRectangle(Brushes.White, new Rectangle(0, 0, bitmapSize.Width, bitmapSize.Height)); var hdc = g.GetHdc(); var pimldp = new NativeMethods.IMAGELISTDRAWPARAMS { hdcDst = hdc }; pimldp.cbSize = Marshal.SizeOf(pimldp.GetType()); pimldp.i = index; pimldp.x = 0; pimldp.y = 0; pimldp.cx = bitmapSize.Width; pimldp.cy = bitmapSize.Height; pimldp.fStyle = (int)flags; if (_iImageList == null || Thread.CurrentThread.GetApartmentState() == ApartmentState.MTA) { NativeMethods.ImageList_DrawIndirect(ref pimldp); } else { _iImageList.Draw(ref pimldp); } } finally { g.ReleaseHdc(); } } bitmap.MakeTransparent(); return(bitmap); }
private Bitmap GetBitmap(int index, ImageListDrawItemConstants flags) { var bitmapSize = GetImageListIconSize(); var bitmap = new Bitmap(bitmapSize.Width, bitmapSize.Height); using (var g = Graphics.FromImage(bitmap)) { try { g.FillRectangle(Brushes.White, new Rectangle(0, 0, bitmapSize.Width, bitmapSize.Height)); var hdc = g.GetHdc(); var pimldp = new NativeMethods.IMAGELISTDRAWPARAMS { hdcDst = hdc }; pimldp.cbSize = Marshal.SizeOf(pimldp.GetType()); pimldp.i = index; pimldp.x = 0; pimldp.y = 0; pimldp.cx = bitmapSize.Width; pimldp.cy = bitmapSize.Height; pimldp.fStyle = (int)flags; if (_iImageList == null || Thread.CurrentThread.GetApartmentState() == ApartmentState.MTA) { NativeMethods.ImageList_DrawIndirect(ref pimldp); } else { _iImageList.Draw(ref pimldp); } } finally { g.ReleaseHdc(); } } bitmap.MakeTransparent(); return bitmap; }