5

Winforms の System.Windows.Forms.Control クラスには、さまざまな状況で非常に役立つインスタンス メソッド "DrawToBitmap" があります。WPF アプリケーションから System.Drawing.Bitmap を取得する同等の方法があるかどうか疑問に思っていますか?

アプリケーション ウィンドウを取得するだけで P/Invoke を実行できることはわかっていますが、64 ビットの遷移にうまく対応できず、DrawToBitmap のようにサブコントロールのみをレンダリングできないため、これは好きではありません。します。

ありがとう、リチャード

4

2 に答える 2

10

MSDN のように RenderTargetBitmap を使用する

RenderTargetBitmap bitmap = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
bitmap.Render(this.YourVisualControlNameGoesHere); 
于 2009-03-09T00:19:46.700 に答える
2

TFDはスポットです。MSDN のあまり洗練されていない参考例を使用することもできます。

Dim width As Integer = 128
Dim height As Integer = width
Dim stride As Integer = CType(width / 8, Integer)
Dim pixels(height * stride) As Byte

' Try creating a new image with a custom palette.
Dim colors As New List(Of System.Windows.Media.Color)()
colors.Add(System.Windows.Media.Colors.Red)
colors.Add(System.Windows.Media.Colors.Blue)
colors.Add(System.Windows.Media.Colors.Green)
Dim myPalette As New BitmapPalette(Colors)

' Creates a new empty image with the pre-defined palette
Dim image As BitmapSource = System.Windows.Media.Imaging.BitmapSource.Create(width, height, 96, 96, PixelFormats.Indexed1, myPalette, pixels, stride)
Dim stream As New FileStream("new.bmp", FileMode.Create)
Dim encoder As New BmpBitmapEncoder()
Dim myTextBlock As New TextBlock()
myTextBlock.Text = "Codec Author is: " + encoder.CodecInfo.Author.ToString()
encoder.Frames.Add(BitmapFrame.Create(image))
encoder.Save(stream)
于 2009-03-09T06:22:25.317 に答える