Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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;
        }
Esempio n. 3
0
        public static void DrawImage(Graphics graphics, Image image, Point point, bool disabled)
        {
            if (!disabled)
            {
                Rectangle destination = new Rectangle(point, image.Size);
                graphics.DrawImage(image, destination, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
            }
            else
            {
                // Painting a disabled gray scale image is done using ILS_SATURATE on WinXP.
                // This code emulates that behaviour if comctl32 version 6 is not availble.
                NativeMethods.DLLVERSIONINFO dvi = new NativeMethods.DLLVERSIONINFO();
                dvi.cbSize = Marshal.SizeOf(typeof(NativeMethods.DLLVERSIONINFO));
                NativeMethods.DllGetVersion(ref dvi);
                if (dvi.dwMajorVersion < 6)
                {
                    ImageAttributes attributes = new ImageAttributes();
                    Rectangle destination = new Rectangle(point, image.Size);
                    float[][] matrix = new float[5][];
                    matrix[0] = new float[] { 0.2222f, 0.2222f, 0.2222f, 0.0000f, 0.0000f };
                    matrix[1] = new float[] { 0.2222f, 0.2222f, 0.2222f, 0.0000f, 0.0000f };
                    matrix[2] = new float[] { 0.2222f, 0.2222f, 0.2222f, 0.0000f, 0.0000f };
                    matrix[3] = new float[] { 0.3333f, 0.3333f, 0.3333f, 0.7500f, 0.0000f };
                    matrix[4] = new float[] { 0.0000f, 0.0000f, 0.0000f, 0.0000f, 1.0000f };
                    attributes.SetColorMatrix(new ColorMatrix(matrix));
                    graphics.DrawImage(image, destination, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
                }
                else
                {
                    ImageList imageList = new ImageList();
                    imageList.ImageSize = image.Size;
                    imageList.ColorDepth = ColorDepth.Depth32Bit;
                    imageList.Images.Add(image);

                    IntPtr hdc = graphics.GetHdc();
                    NativeMethods.IMAGELISTDRAWPARAMS ildp = new NativeMethods.IMAGELISTDRAWPARAMS();
                    ildp.cbSize = Marshal.SizeOf(typeof(NativeMethods.IMAGELISTDRAWPARAMS));
                    ildp.himl = imageList.Handle;
                    ildp.i = 0; // image index
                    ildp.hdcDst = hdc;
                    ildp.x = point.X;
                    ildp.y = point.Y;
                    ildp.cx = 0;
                    ildp.cy = 0;
                    ildp.xBitmap = 0;
                    ildp.yBitmap = 0;
                    ildp.fStyle = NativeMethods.ILD_TRANSPARENT;
                    ildp.fState = NativeMethods.ILS_SATURATE;
                    ildp.Frame = -100;
                    NativeMethods.ImageList_DrawIndirect(ref ildp);
                    graphics.ReleaseHdc(hdc);
                }
            }
        }