从 WPF 应用程序窗口获取位图?

发布于 2024-07-14 21:40:24 字数 257 浏览 5 评论 0原文

Winforms System.Windows.Forms.Control 类有一个实例方法“DrawToBitmap”,我认为它在各种情况下都非常有用。 我想知道是否有从 WPF 应用程序获取 System.Drawing.Bitmap 的等效方法?

我意识到我可以做一些 P/Invoke 的事情来获取应用程序窗口,但是我不喜欢这样做,因为它不能很好地适应 64 位转换,并且不允许我仅渲染子控件,如 DrawToBitmap做。

谢谢, 理查德

The Winforms System.Windows.Forms.Control class has an instance method "DrawToBitmap" which I think is very useful in a variety of circumstances. I'm wondering if there's an equivalent way of getting a System.Drawing.Bitmap from a WPF application?

I realize I could do some P/Invoke stuff to just get the application window, however I don't like this because it doesn't accomodate the 64bit transition very well, and doesn't let me render sub-controls only, as DrawToBitmap does.

Thanks,
Richard

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吐个泡泡 2024-07-21 21:40:24

按照 MSDN 上的方式使用 RenderTargetBitmap

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

Use RenderTargetBitmap as on MSDN

RenderTargetBitmap bitmap = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
bitmap.Render(this.YourVisualControlNameGoesHere); 
深爱成瘾 2024-07-21 21:40:24

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)

TFD is spot on.
You could also use the less elegant reference example from 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文