如何在源代码为Build Action = Resource的图像时以编程方式创建位图图像?

and*_*cci 1 silverlight uri bitmapimage

如何在源代码为Build Action = Resource的图像时以编程方式创建位图图像?

当我尝试以下操作时,我得到一个无效的URI异常:(

BitmapImage image = new BitmapImage(new Uri("/MyAssembly;component/Images/MyImage.png"));
Run Code Online (Sandbox Code Playgroud)

我正在使用silverlight 4,这个代码位于我的解决方案的许多项目之一(它是一个silverlight类库而不是silverlight应用程序).

Dav*_*vid 8

BitmapImage image = new BitmapImage(new Uri("/MyAssembly;component/Images/MyImage.png"));
Run Code Online (Sandbox Code Playgroud)

应该:

BitmapImage image = new BitmapImage(new Uri("/MyAssembly;component/Images/MyImage.png", UriKind.Relative));
Run Code Online (Sandbox Code Playgroud)

Images/MyImage.png还假设您的图像位于名为的文件夹中Images.