如何在WPF Canvas上渲染图像 [英] How can I get an Image to render on a WPF Canvas

查看:330
本文介绍了如何在WPF Canvas上渲染图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用C#/ WPF,尝试将图像插入画布。该映像位于根目录中。当我使用完整路径时,它会在画布上呈现,但在我使用时则不会; Source = MyImage.png。我错过了什么吗?




I’m working with C#/WPF, trying to insert an image onto a canvas. The image is located in the root directory. It is rendered on the canvas when I use the complete path but not when I use; Source= MyImage.png . Am I missing something?

<Canvas  Background="Wheat" UseLayoutRounding="False" AllowDrop="True" >
   <Image Canvas.Left="102" Canvas.Top="122" Width="400" OpacityMask="#FFC2E5DB"
          Source="C:/Users/Tip/C# Files/BookKeeper/MYImage.png"/>
</Canvas>




"MyImage" appears on the Canvas.




 <Canvas  Background="Wheat" UseLayoutRounding="False" AllowDrop="True" >
    <Image Canvas.Left="102" Canvas.Top="122" Width="400" OpacityMask="#FFC2E5DB"

           Source="MYImage.png"/>
 </Canvas>

The program runs normally but "MyImage" does not appear on the Canvas.

Any help would be greatly appreciated.

推荐答案

当然,使用完整路径不是解决方案;在其他系统上找不到该文件,即使在你的系统上也无法移植。



首先,你不必拥有文件在可执行目录中。如果这是永久文件(产品的一部分),则最好将其嵌入资源中,然后使用 URI方案pack://在XAML中引用。例如,参见:

http://www.wpfsharp.com/2012/01/23/using-relative-path-images-included-as-content-with-wpf [ ^ ]。



此外,使用WPF,我不确定你是否需要使用像素图形(窗口和应用程序菜单除外)。如果图像不是非常小,矢量图形更容易,更好,可以自动缩放,不会降低质量,占用更少的内存,等等。您可以创建矢量图像并将其保存到XAML(我强烈推荐开源InkScape,一个惊人的质量和性能的应用程序)并将其嵌入您的XAML,窗口,资源字典,任何东西。你可以在我最近的文章中找到这样的解释,例如:带声音激活的实用录音机 [ ^ ]。



如果您仍然认为需要保留像素图像,请参阅上面的第一行,还有:

http://stackoverflow.com/questions/ 1444699 / wpf-relative-image-source-path [ ^ ],

HT TP://social.msdn.microsoft.com/Forums/vstudio/en-US/53cda8b8-08c1-4477-937a-e934157b0b1e/how-to-use-relative-path-for-image-in-wpf-when- image-is-not-in-project-directory?forum = wpf [ ^ ]。



-SA
Of course, using the full path is not a solution; the file won't be found on another system and the solution would not be portable even on yours.

First of all, you don't have to have the file in executable directory at all. If this is a permanent file, part of your product, you should better embed it in a resource and then reference in XAML using the URI scheme "pack://". See, for example:
http://www.wpfsharp.com/2012/01/23/using-relative-path-images-included-as-content-with-wpf[^].

Moreover, with WPF, I'm not sure you need to use pixel graphics at all (except the window and application menus). If the image is not extremely small, vector graphics is much easier and better, will be automatically zoomable, without the loss in quality, take up less memory, and so on. You can create vector image and save it to XAML (I highly recommend open-source InkScape, an application of amazing quality and performance) and embed it in your XAML, of window, resource dictionary, anything. You can find such example with the explanation, say, in my recent article: Practical Sound Recorder with Sound Activation[^].

If you still think you need to stay with pixel image, please see first line above, and also:
http://stackoverflow.com/questions/1444699/wpf-relative-image-source-path[^],
http://social.msdn.microsoft.com/Forums/vstudio/en-US/53cda8b8-08c1-4477-937a-e934157b0b1e/how-to-use-relative-path-for-image-in-wpf-when-image-is-not-in-project-directory?forum=wpf[^].

—SA


这篇关于如何在WPF Canvas上渲染图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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