C# 访问嵌入式映像并创建System.Windows.Controls.image
我已设置为访问嵌入式资源并返回System.Drawing.Image,但我无法使用此设置画布的背景 有人能告诉我如何访问嵌入式图像文件并创建System.Windows.Controls.image吗。到目前为止,我掌握的代码是:C# 访问嵌入式映像并创建System.Windows.Controls.image,c#,wpf,image,C#,Wpf,Image,我已设置为访问嵌入式资源并返回System.Drawing.Image,但我无法使用此设置画布的背景 有人能告诉我如何访问嵌入式图像文件并创建System.Windows.Controls.image吗。到目前为止,我掌握的代码是: public static Image Load(Type classType, string resourcePath) { Assembly asm = Assembly.GetAssembly(classType);
public static Image Load(Type classType, string resourcePath)
{
Assembly asm = Assembly.GetAssembly(classType);
Stream imgStream = asm.GetManifestResourceStream(resourcePath);
Image img = Image.FromStream(imgStream);
imgStream.Close();
return img;
}
如果您需要更多信息,请告诉我位图bm=新位图(@“…”);
img=bm 如果您不特别需要使用“System.Drawing.Image”,那么您可以尝试以下方法:
Assembly asm = Assembly.GetCallingAssembly();
var res = asm.GetManifestResourceNames();
Stream imgStream = asm.GetManifestResourceStream("path.to.resource");
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = imgStream;
image.EndInit();
ImageBrush brush = new ImageBrush();
brush.ImageSource = image;
imageCanvas.Background = brush;
你试过这个吗?我很难将其与我的情况联系起来,因为资源不在同一个dll中,而是嵌入在类类型参数中。一个代码示例对我很有帮助。我可以问你为什么需要
System.Drawing.Image
如果你只想设置wpf画布的背景,那么有更简单的方法我不需要。。。我拥有的代码是我从包含嵌入式资源的dll中获取图像所必须的。我希望它返回System.Windows.Controls.Image,而不是返回System.Drawing.Image的方法,但我不知道如何返回。Paul这与我想要的完全一样。我会试着告诉你事情的进展:)