1

我已设置访问嵌入式资源并返回 System.Drawing.Image 但我不能使用它来设置画布的背景。

有人可以告诉我如何访问嵌入式图像文件并创建 System.Windows.Controls.Image。我到目前为止的代码是:

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;
        }

如果您需要更多信息,请告诉我

4

2 回答 2

2

如果您不需要特别使用“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;
于 2012-06-28T13:52:57.063 回答
1

位图 bm = new Bitmap(@"...."); img = bm;

于 2012-06-28T11:12:59.487 回答