为System.Drawing.Image自ImageSource参考资料 [英] System.Drawing.Image from ImageSource in Resources

查看:586
本文介绍了为System.Drawing.Image自ImageSource参考资料的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很类似这样的:的 WPF图像资源,并在运行时 WPF控件改变形象,但有轻微的扭曲。

My question is very similar to this one: wpf image resources and changing image in wpf control at runtime, but with a slight twist.

下面是我的ResourceDictionary.xaml:

Here is my ResourceDictionary.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ImageSource x:Key="DisconnectedIcon">Images/disconnect.png</ImageSource>
<ImageSource x:Key="Synced">Images/tick.png</ImageSource>
<ImageSource x:Key="NotSynced">Images/x.png</ImageSource>



对于C#代码背后,我能够从资源与下面加载的ImageSource。其中,我能看到的元数据和图像名称,但无法弄清楚如何把它变成一个System.Drawings.Image。

As for the C# code behind, I am able to load the ImageSource from the resources with the following. In which, I am able to see the metadata and the image name, but can't figure out how to get it into a System.Drawings.Image.

var imageSource = (ImageSource)Application.Current.FindResource("Synced");

System.Drawing.Image img = Image.FromFile(???)

我想它转换为System.Drawing.Image对象的原因是将其发送到打印机。

The reason I am trying to convert it to a System.Drawing.Image is to send it to a printer.

谢谢!

推荐答案

在WPF中,每一个用户界面元素扩展 视觉其中的提供渲染WPF 的支持。还有一个 RenderTargetBitmap 具有的 渲染方法,需要一个视觉对象作为输入参数。所以,你可以设置你的的ImageSource 图像的来源属性并简单地渲染图片位图图片:

In WPF, every UI element extends the Visual Class which Provides rendering support in WPF. There is also a RenderTargetBitmap Class that has a Render Method that takes a Visual object as an input parameter. So you could set your ImageSource as the Source property of an Image and simply render the Image to a Bitmap image:

Image yourImageObject = new Image();
yourImageObject.Source = yourImageSource;

RenderTargetBitmap renderTargetBitmap = 
    new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
renderTargetBitmap.Render(yourImageObject);

// Save to .png file
PngBitmapEncoder pngBitmapEncoder = new PngBitmapEncoder();    
pngBitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));    
using (Stream stream = File.Create(filepath))    
{    
    pngBitmapEncoder.Save(stream);    
}



由于这是很好在互联网上记录的,我不会刻意去重复整个故事在这里。要了解完整的故事,请参阅的如何渲染位图或打印在WPF 页面一个Visual从点网网站技巧,这也将帮助您与您的打印需求。

As this is well documented on the internet, I won't bother to repeat the whole story here. To find out the full story, please see the How to Render Bitmap or to Print a Visual in WPF page from the Dot NET Tricks website, which will also help you with your printing requirement.

这篇关于为System.Drawing.Image自ImageSource参考资料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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