我正在为一个大学项目(学期长)做一个二十一点游戏,我遇到了一个我似乎无法克服的路障.
我试图加载卡中的图像,使他们能够在屏幕上显示出来,但我一直有非常这样做一点运气.我有绝对的参考资料; 我可以从那些加载,但任何从相对路径加载的尝试都会失败.该项目必须是独立的; 我无法指示我的教授将这些图像复制到根目录上.
#if FROMDISK
Uri myUri = new Uri(@"C:\cards\" + getFilename(r, s, "png"), UriKind.Absolute);
PngBitmapDecoder decoder2 = new PngBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bmp = decoder2.Frames[0];
#else
System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
Stream myStream = myAssembly.GetManifestResourceStream("Blackjack." + getFilename(r, s, "png"));
BitmapImage bmp = new BitmapImage();
bmp.StreamSource = myStream;
#endif
// Draw the Image
im.Source = bmp;
im.Stretch = Stretch.Uniform;
im.Width = CARD_WIDTH;
Run Code Online (Sandbox Code Playgroud)
您可以使用:
Uri uri = new Uri(@"FolderName\FileName.png",UriKind.Relative);
PngBitmapDecoder decoder2 = new PngBitmapDecoder(uri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
Run Code Online (Sandbox Code Playgroud)
请记住将图像文件的属性设置为(否则编译器将跳过它们)您可以在构建后查看bin/debug文件夹并验证该文件应该位于何处:
另一种选择是使文件成为嵌入式资源,然后您可以像这样访问它:
Bitmap bitmap = Properties.Resources.FileName;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25333 次 |
| 最近记录: |