将 XAML-Canvas 转换为位图 [英] Convert XAML-Canvas to bitmap

查看:28
本文介绍了将 XAML-Canvas 转换为位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将大量 XAML 格式的图标(都只使用画布)转换为位图格式.

I need to convert a large amount of icons in XAML format (all just use canvas) to bitmap format.

有人知道有这样的工具吗?我只找到了 xamltoys/xaml2emf.这有效但不正确 - 颜色混乱且分辨率非常低.

Does anybody know about a tool that does that? I only found xamltoys/xaml2emf. This works but not correctly - colors are messed up and resolution is very low.

推荐答案

我做的这段代码只允许你将任何 FrameworkElement 保存到磁盘:

This piece of code I did will just allow you to save any FrameworkElement to disk:

Public Shared Sub SaveImageOfControlToDisk(ByVal MyControl As FrameworkElement, ByVal FileName As String)
    Dim ThisVisualBrush As New VisualBrush(MyControl)
    Dim ThisDrawingVisual As New DrawingVisual()
    Dim dc As DrawingContext = ThisDrawingVisual.RenderOpen()
    dc.DrawRectangle(ThisVisualBrush, Nothing, New Rect(New Point(), New Size(MyControl.ActualWidth, MyControl.ActualHeight)))
    dc.Close()
    Dim render As New RenderTargetBitmap(MyControl.ActualWidth, MyControl.ActualHeight, 96, 96, PixelFormats.Pbgra32)
    render.Render(ThisDrawingVisual)
    Dim PngEncoder As New PngBitmapEncoder()
    PngEncoder.Frames.Add(BitmapFrame.Create(render))
    Dim ThisStream As New IO.FileStream(FileName, IO.FileMode.Create)
    PngEncoder.Save(ThisStream)
    ThisStream.Flush()
    ThisStream.Close()
End Sub

这篇关于将 XAML-Canvas 转换为位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆