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);
}
}
描画後にメソッドの最後で「ビットマップ」のピクセル形式を確認すると、正しい「ピクセル形式」が得られますが、コントロールを描画するビットマップ (メソッドに渡されたもの) を確認すると、「 DontCare" ピクセル形式。
この背後にある理由は何ですか?