gpt4 book ai didi

c# - 从相对路径加载图像

转载 作者:太空狗 更新时间:2023-10-29 22:23:21 24 4
gpt4 key购买 nike

我正在为一个大学项目(一个学期)制作一个二十一点游戏,但我遇到了一个我似乎无法克服的障碍。

我正在尝试加载卡片图像,以便它们可以显示在屏幕上,但我一直非常不走运。我有绝对引用资料;我可以从那些加载,但任何从相对路径加载的尝试都会失败。该项目必须是独立的;我无法指示我的教授将这些图像复制到根目录中。

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

(Context)

最佳答案

您可以使用:

Uri uri = new Uri(@"FolderName\FileName.png",UriKind.Relative);
PngBitmapDecoder decoder2 = new PngBitmapDecoder(uri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);

记得将图像文件的属性设置为(否则编译器会跳过它们)你可以在构建后查看 bin/debug 文件夹并验证文件是否在它应该在的位置:

  • 构建操作:content
  • 复制到输出目录:总是复制

另一种选择是将文件设为 embedded resource然后你可以像这样访问它:

Bitmap bitmap = Properties.Resources.FileName;

关于c# - 从相对路径加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13078613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com