为什么Control.DrawToBitmap将pixelformat更改为DontCare?

时间:2013-12-26 23:03:53

标签: c# .net bitmap pixelformat

我使用IlSpy来研究它被设计为保留目标位图的像素格式的方法,这里是代码:

public void DrawToBitmap(Bitmap bitmap, Rectangle targetBounds)
    {
        if (bitmap == null)
        {
            throw new ArgumentNullException("bitmap");
        }
        if (targetBounds.Width <= 0 || targetBounds.Height <= 0 || targetBounds.X < 0 || targetBounds.Y < 0)
        {
            throw new ArgumentException("targetBounds");
        }
        if (!this.IsHandleCreated)
        {
            this.CreateHandle();
        }
        int nWidth = Math.Min(this.Width, targetBounds.Width);
        int nHeight = Math.Min(this.Height, targetBounds.Height);
        Bitmap image = new Bitmap(nWidth, nHeight, bitmap.PixelFormat);
        using (Graphics graphics = Graphics.FromImage(image))
        {
            IntPtr hdc = graphics.GetHdc();
            UnsafeNativeMethods.SendMessage(new HandleRef(this, this.Handle), 791, hdc, (IntPtr)30);
            using (Graphics graphics2 = Graphics.FromImage(bitmap))
            {
                IntPtr hdc2 = graphics2.GetHdc();
                SafeNativeMethods.BitBlt(new HandleRef(graphics2, hdc2), targetBounds.X, targetBounds.Y, nWidth, nHeight, new HandleRef(graphics, hdc), 0, 0, 13369376);
                graphics2.ReleaseHdcInternal(hdc2);
            }
            graphics.ReleaseHdcInternal(hdc);
        }
    }

如果在绘图后检查方法最末端的“位图”的像素格式,则会给出正确的“Pixelformat”但是一旦检查了绘制控件的位图(传递给方法的位图)它给出了“DontCare”pixelformat。

这背后的原因是什么?

0 个答案:

没有答案