在WPF和Winforms之间共享图形
我想在Winforms项目和WPF项目中使用一些png、位图和图标 我对在winforms中使用资源很满意,对WPF中的资源也很满意 在双方共享资源的情况下,人们在做什么?我目前的感觉是有一个WPF资源程序集和一个Winforms资源程序集,但我所看到的方法似乎都不稳定在WPF和Winforms之间共享图形,wpf,winforms,Wpf,Winforms,我想在Winforms项目和WPF项目中使用一些png、位图和图标 我对在winforms中使用资源很满意,对WPF中的资源也很满意 在双方共享资源的情况下,人们在做什么?我目前的感觉是有一个WPF资源程序集和一个Winforms资源程序集,但我所看到的方法似乎都不稳定 谢谢我有一个包含WPF和Winforms控件的项目。此项目具有资源文件夹,其中包含所有图像。然后使用相对路径从磁盘加载它们。 假设项目名称为“MyProject”,图像路径为项目文件夹内名为“MyImages”的文件夹,图像名称
谢谢我有一个包含WPF和Winforms控件的项目。此项目具有资源文件夹,其中包含所有图像。然后使用相对路径从磁盘加载它们。
假设项目名称为“MyProject”,图像路径为项目文件夹内名为“MyImages”的文件夹,图像名称为“MyImage.bmp”,则可以按如下方式加载它们: 在Winforms中:
new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("MyProject.MyImages.MyImage.bmp")
Bitmap photo;
var picUri = new Uri(@"pack://application:,,,/MyProject;component/MyImages/MyImage.bmp", UriKind.Absolute);
var resourceInfo = Application.GetResourceStream(picUri);
if (resourceInfo != null)
{
photo = new Bitmap(resourceInfo.Stream);
}
<Image Source="MyImages/MyImage.bmp"/>
在WPF中
<Image Source="pack://application:,,,/MyProject;Component/MyImages/MyImage.bmp">
希望对您有所帮助…我有一个项目,它同时包含WPF和Winforms控件。此项目具有资源文件夹,其中包含所有图像。然后使用相对路径从磁盘加载它们。
假设项目名称为“MyProject”,图像路径为项目文件夹内名为“MyImages”的文件夹,图像名称为“MyImage.bmp”,则可以按如下方式加载它们: 在Winforms中:
new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("MyProject.MyImages.MyImage.bmp")
Bitmap photo;
var picUri = new Uri(@"pack://application:,,,/MyProject;component/MyImages/MyImage.bmp", UriKind.Absolute);
var resourceInfo = Application.GetResourceStream(picUri);
if (resourceInfo != null)
{
photo = new Bitmap(resourceInfo.Stream);
}
<Image Source="MyImages/MyImage.bmp"/>
在WPF中
<Image Source="pack://application:,,,/MyProject;Component/MyImages/MyImage.bmp">
希望它能帮助…Amittai Shapira的回答是,如果构建操作设置为“嵌入式资源”,那么它就可以工作。如果将生成操作设置为“资源”,则可以使用: WinForms:
new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("MyProject.MyImages.MyImage.bmp")
Bitmap photo;
var picUri = new Uri(@"pack://application:,,,/MyProject;component/MyImages/MyImage.bmp", UriKind.Absolute);
var resourceInfo = Application.GetResourceStream(picUri);
if (resourceInfo != null)
{
photo = new Bitmap(resourceInfo.Stream);
}
<Image Source="MyImages/MyImage.bmp"/>
WPF:
new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("MyProject.MyImages.MyImage.bmp")
Bitmap photo;
var picUri = new Uri(@"pack://application:,,,/MyProject;component/MyImages/MyImage.bmp", UriKind.Absolute);
var resourceInfo = Application.GetResourceStream(picUri);
if (resourceInfo != null)
{
photo = new Bitmap(resourceInfo.Stream);
}
<Image Source="MyImages/MyImage.bmp"/>
如果构建操作设置为“嵌入式资源”,Amittai Shapira的回答将有效。如果将生成操作设置为“资源”,则可以使用: WinForms:
new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("MyProject.MyImages.MyImage.bmp")
Bitmap photo;
var picUri = new Uri(@"pack://application:,,,/MyProject;component/MyImages/MyImage.bmp", UriKind.Absolute);
var resourceInfo = Application.GetResourceStream(picUri);
if (resourceInfo != null)
{
photo = new Bitmap(resourceInfo.Stream);
}
<Image Source="MyImages/MyImage.bmp"/>
WPF:
new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("MyProject.MyImages.MyImage.bmp")
Bitmap photo;
var picUri = new Uri(@"pack://application:,,,/MyProject;component/MyImages/MyImage.bmp", UriKind.Absolute);
var resourceInfo = Application.GetResourceStream(picUri);
if (resourceInfo != null)
{
photo = new Bitmap(resourceInfo.Stream);
}
<Image Source="MyImages/MyImage.bmp"/>
是的,我想是的。我可能会在两者中添加一个助手,以自动确定可用的资源,但您的回答让我放心了。谢谢:)是的,我想是的。我可能会在两者中添加一个助手,以自动确定可用的资源,但您的回答让我放心了。谢谢:)