访问嵌入式图像并创建System.Windows.Controls.Image [英] Accessing embedded image and creating a System.Windows.Controls.Image
本文介绍了访问嵌入式图像并创建System.Windows.Controls.Image的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经设置访问嵌入式资源并返回System.Drawing.Image,但是我不能使用它来设置画布的背景.
I had set up to access an embedded resource and return an System.Drawing.Image but i cant use this to set the background of a canvas.
有人可以告诉我如何访问嵌入式图像文件并创建System.Windows.Controls.Image.到目前为止,我的代码是:
Could someone please show me how to access an embedded image file and create a System.Windows.Controls.Image. Code I have so far is :
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;
}
如果您需要更多信息,请告诉我
Please let me know if you require anymore information
推荐答案
如果您不需要专门使用`System.Drawing.Image',则可以尝试以下方法:
If you don't specifically need to use a `System.Drawing.Image', then you could try something like:
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;
这篇关于访问嵌入式图像并创建System.Windows.Controls.Image的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文