c#bitblitからbitmapからコントロール(コンパクトフレームワーク)
-
20-09-2019 - |
質問
Bitbltを1回使用して、画面ファイル(.NET Compact Framework v3.5、Windows Mobile 2003以降)にスクリーンショットを保存しました。うまくいきました。今、私はフォームにビットマップを描きたいです。使用できます this.CreateGraphics().DrawImage(mybitmap, 0, 0)
, 、しかし、私はそれが以前のようにbitbltで動作するかどうか疑問に思っていました、そして、ただパラップを交換しました。だから私は書いた:
[DllImport("coredll.dll")]
public static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);
(そしてさらに下:)
IntPtr hb = mybitmap.GetHbitmap();
BitBlt(this.Handle, 0, 0, mybitmap.Width, mybitmap.Height, hb, 0, 0, 0x00CC0020);
しかし、フォームは無地のままです。何故ですか?私がコミットしたエラーはどこにありますか?ご意見をお寄せいただきありがとうございます。乾杯、デビッド
解決
this.Handle
aです ウィンドウハンドル ではありません デバイスコンテキスト.
交換 this.Handle
と this.CreateGraphics().GetHdc()
もちろん、グラフィックオブジェクトなどを破壊する必要があります...
IntPtr hb = mybitmap.GetHbitmap();
using (Graphics gfx = this.CreateGraphics())
{
BitBlt(gfx.GetHdc(), 0, 0, mybitmap.Width, mybitmap.Height, hb, 0, 0, 0x00CC0020);
}
加えて hb
aです Bitmap Handle
ではありません device context
したがって、上記のスニペットはまだ機能しません。ビットマップからデバイスコンテキストを作成する必要があります。
using (Bitmap myBitmap = new Bitmap("c:\test.bmp"))
{
using (Graphics gfxBitmap = Graphics.FromImage(myBitmap))
{
using (Graphics gfxForm = this.CreateGraphics())
{
IntPtr hdcForm = gfxForm.GetHdc();
IntPtr hdcBitmap = gfxBitmap.GetHdc();
BitBlt(hdcForm, 0, 0, myBitmap.Width, myBitmap.Height, hdcBitmap, 0, 0, 0x00CC0020);
gfxForm.ReleaseHdc(hdcForm);
gfxBitmap.ReleaseHdc(hdcBitmap);
}
}
}
他のヒント
これらの線に沿って何かを意味しますか?
public void CopyFromScreen(int sourceX, int sourceY, int destinationX,
int destinationY, Size blockRegionSize,
CopyPixelOperation copyPixelOperation)
{
IntPtr desktopHwnd = GetDesktopWindow();
if (desktopHwnd == IntPtr.Zero)
{
throw new System.ComponentModel.Win32Exception();
}
IntPtr desktopDC = GetWindowDC(desktopHwnd);
if (desktopDC == IntPtr.Zero)
{
throw new System.ComponentModel.Win32Exception();
}
if (!BitBlt(hDC, destinationX, destinationY, blockRegionSize.Width,
blockRegionSize.Height, desktopDC, sourceX, sourceY,
copyPixelOperation))
{
throw new System.ComponentModel.Win32Exception();
}
ReleaseDC(desktopHwnd, desktopDC);
}
参考までに、これはそうです SDFからすぐに.
編集:このスニペットでは実際には明確ではありませんが、BITBLTのHDCはターゲットビットマップのHDCです(ペイントしたい)。
よろしいですか this.Handle
有効なデバイスコンテキストを指しますか?の返品値をチェックしてみましたか BitBlt
関数?
以下を試してください:
[DllImport("coredll.dll", EntryPoint="CreateCompatibleDC")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("coredll.dll", EntryPoint="GetDC")]
public static extern IntPtr GetDC(IntPtr hwnd);
IntPtr hdc = GetDC(this.Handle);
IntPtr hdcComp = CreateCompatibleDC(hdc);
BitBlt(hdcComp, 0, 0, mybitmap.Width, mybitmap.Height, hb, 0, 0, 0x00CC0020);