/// <include file='doc\ImageList.uex' path='docs/doc[@for="ImageList.Draw2"]/*' /> /// <devdoc> /// Draw the image indicated by the given index using the location, size /// and raster op code specified. The image is stretched or compressed as /// necessary to fit the bounds provided. /// </devdoc> public void Draw(Graphics g, int x, int y, int width, int height, int index) { int rop = NativeMethods.SRCCOPY; if (width != imageSize.Width || height != imageSize.Height) { Bitmap bitmap = GetBitmap(index); g.DrawImage(bitmap, new Rectangle(x, y, width, height)); } else { IntPtr dc = g.GetHdc(); try { IntPtr handle = Handle; // Force handle creation if (index < 0 || index >= Images.Count) { throw new ArgumentOutOfRangeException(SR.GetString(SR.InvalidArgument, "index", index.ToString())); } NativeMethods.IMAGELISTDRAWPARAMS dp = new NativeMethods.IMAGELISTDRAWPARAMS(); int backColor = NativeMethods.CLR_NONE; int mode = NativeMethods.ILD_TRANSPARENT | NativeMethods.ILD_ROP; dp.himl = Handle; dp.i = index; dp.hdcDst = dc; dp.x = x; dp.y = y; dp.cx = width; dp.cy = height; dp.xBitmap = 0; dp.yBitmap = 0; dp.rgbBk = backColor; dp.rgbFg = NativeMethods.CLR_NONE; dp.fStyle = mode; dp.dwRop = rop; SafeNativeMethods.ImageList_DrawIndirect(dp); } finally { // SECREVIEW : GetHdc allocs the handle, so we must release it. // IntSecurity.Win32HandleManipulation.Assert(); try { g.ReleaseHdc(dc); } finally { CodeAccessPermission.RevertAssert(); } } } }
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); } } }