1

我使用 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”像素格式。

这背后的原因是什么?

4

0 回答 0