【问题标题】:Loading images from a relative path从相对路径加载图像
【发布时间】:2012-10-25 23:01:08
【问题描述】:

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

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

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

【问题讨论】:

    标签: c# wpf resx


    【解决方案1】:

    你可以使用:

    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;
    

    【讨论】:

    • 我应该为哪些文件设置这些属性?为代码文件设置它们似乎是错误的举动(老实说:我不知道它们到底在做什么)
    • 我不知道为什么 SO 没有通知我这一切,但我回家后会试试看!
    【解决方案2】:

    我喜欢在我的 XAML 中将图像声明为资源。我假设您此时仍然可以使用代码结构,并且希望没有太多新概念适合您。这是我的做法:

    开始在您的项目中创建一个名为“Images”的文件夹。通过将卡片的图像拖放到 Visual Studio 中的文件夹中来添加它们。确保他们的“构建操作”设置为资源。

    按 CTRL-SHIFT-A 创建一个新的“资源字典”。将其命名为 CardImages.xaml。

    像这样在 App.xaml 中链接此文件(展示如何一次链接多个 XAML 文件,但为此当然要删除“AnyDictionary”行!)

    App.XAML:

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Controls/CardImages.xaml" />
                <ResourceDictionary Source="Controls/AnyDictionaryYouWant.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    

    将此代码添加到 CardImages.xaml。将“MyBlackJackAssembly”重命名为您项目的程序集名称。

    <Style TargetType="Image">
        <Setter Property="RenderOptions.BitmapScalingMode" Value="HighQuality" />
    </Style>
    
    <DataTemplate x:Key="Spade1">
        <Image Source="MyBlackJackAssembly;component/Images/Spade1.png" />
    </DataTemplate>
    
    <DataTemplate x:Key="Spade2">
        <Image Source="MyBlackJackAssembly;component/Images/Spade2.png" />
    </DataTemplate>
    

    然后,您可以像这样在代码中找到它们:

    Label label = new Label();
    label.ContentTemplate = (DataTemplate)label.FindResource("Spade1");
    

    这将为您提供一个 WPF Label 对象,该对象应显示您的卡片。这种技术适用于任何支持 ContentTemplate 的东西。然后您可以将标签添加到 UI 上的网格中,我不确定您如何在屏幕上显示卡片。

    对于二十一点应用程序,我可能会创建一个名为“Card”的 UserControl,以便能够像这样生成它。这将“卡片生成”逻辑封装到它自己的 Control 中,因此其余代码可以只专注于处理卡片。

    XAML:

    <myControls:CardImage Kind="Spades" Digit="1" />
    

    或者像这样在c#中:

    CardImage aCard = new CardImage();
    aCard.Kind = CardImage.Kinds.Spades; //enum
    aCard.Digit = 1;
    

    【讨论】:

    • 我不知道为什么 SO 没有通知我这一切,但我想说我非常兴奋能回家试试这个! C# 是一种非常方便的语言。
    【解决方案3】:

    我在加载项目资源中导入的一些背景图片的方式:

    this.staticBg.Source = new BitmapImage(
        new Uri(@"/Project;component/Images/" + ((App)App.Current).bgImage
                                              + ".jpg", UriKind.Relative));
    

    其中“项目”是项目名称。 所以基本上你应该将图像添加到资源中并使用相对 uri 调用它们。

    【讨论】:

    • 我因此修改了我的代码:im.Source = new BitmapImage(new Uri(@"/Project;component/Images/" + getFilename(r, s, "png"), UriKind.Relative)) - 这是正确的吗?因为它似乎不起作用。它没有抛出错误,只是什么都没有出现。 'Images' 是你创建的某个文件夹吗?
    • @vermiculus 要将图像保存在您的应用程序中(并且可能不需要#FROMDISK?),您必须将图像拖到Visual Studio 中的项目中。然后确保他们的 Build Action 设置为“Resource”(但通常这已经可以了)。然后您可以像 Clone 所说的那样引用它,或者您可以通过创建一个充当窗口可视​​对象的 UserControl 对象来尝试更“WPF”的方法。
    • 嗨,我创建了图像文件夹,方法是右键单击项目名称并选择添加->新建文件夹。然后通过右键单击图像文件夹并选择添加->现有项目来导入实际图像。构建操作设置为内容,并将复制到输出设置为始终复制(如果您想加快构建速度,也可以是如果更新则复制)。
    猜你喜欢
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 2017-08-15
    • 2016-08-12
    相关资源
    最近更新 更多