2009-03-08 18 views
5

La classe System.Windows.Forms.Control di Winform ha un metodo di istanza "DrawToBitmap" che ritengo sia molto utile in una varietà di circostanze. Mi chiedo se c'è un modo equivalente di ottenere un System.Drawing.Bitmap da un'applicazione WPF?Ottieni una bitmap da una finestra dell'applicazione WPF?

Mi rendo conto che potrei fare alcune cose P/Invoke per ottenere solo la finestra dell'applicazione, tuttavia non mi piace perché non soddisfa molto bene la transizione a 64 bit, e non mi permette di renderizzare i sottocomandi solo come DrawToBitmap.

Grazie, Richard

risposta

9

Usa RenderTargetBitmap come su MSDN

RenderTargetBitmap bitmap = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32); 
bitmap.Render(this.YourVisualControlNameGoesHere); 
2

TFD è a posto. È anche possibile utilizzare l'esempio di riferimento meno elegante di 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) 
Problemi correlati