Frage

Die WinForms System.Windows.Forms.Control Klasse hat eine Instanz-Methode „DrawToBitmap“, die ich denke, ist sehr nützlich in einer Vielzahl von Umständen. Ich frage mich, ob es eine gleichwertige Möglichkeit, einen System.Drawing.Bitmap aus einer WPF-Anwendung zu bekommen?

Ich weiß, ich könnte etwas tun P / stuff aufrufe, um nur das Anwendungsfenster zu bekommen, aber ich mag das nicht, weil es nicht den 64-Bit-Übergang sehr gut funktioniert empfangen, und habe mich Unter Kontrollen nur nicht zulassen, machen , wie DrawToBitmap der Fall ist.

Danke, Richard

War es hilfreich?

Lösung

Verwenden Rendertargetbitmap wie auf MSDN

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

Andere Tipps

TFD ist vor Ort auf. Sie könnten auch das weniger elegante Referenzbeispiel aus MSDN verwenden:

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)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top