托管 WPF 控件中的嵌入式图像资源

Embedded image resource in hosted WPF control

我的设置有点不正统 - CLR 托管在本机 Win32 应用程序中。应用程序加载并调用托管程序集(用 C++/CLI 编写)。 Assembly 使用 WPF ElementHost 创建一个 Windows Forms 表单。 ElementHost 填充了一个包含图像控件和标签控件的堆栈面板。

托管程序集包含嵌入的图像资源。资源文件(在项目中)的名称是 'Images',所讨论的特定资源的名称是 'ISIconDB'。

如何使用上述资源作为图像控件的来源?

想通了。

[DllImport("gdi32.dll")] int DeleteObject(IntPtr hObject);

//Drawing::Bitmap^ OrgResource; 

Windows::Media::Imaging::BitmapSource^ Marshalled = nullptr;

try {
Marshalled = System::Windows::Interop::Imaging::CreateBitmapSourceFromHBitmap(OrgResource->GetHbitmap(),
IntPtr::Zero, Windows::Int32Rect::Empty, Windows::Media::Imaging::BitmapSizeOptions::FromEmptyOptions()); 
}
catch (...) { 
Marshalled = nullptr;
} 
finally { 
DeleteObject(OrgResource->GetHbitmap());
}