Download images from relative path

I am doing a blackjack game for a project in college (semester), and I hit the road block, which apparently cannot end.

I'm trying to upload map images so that they appear on the screen, but I had very little luck with that. I have absolute links; I can download from them, but any attempt to download from the relative path failed. The project must be autonomous; I cannot instruct my professor to copy these images to the root.

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

+4
source share
3 answers

You can use:

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

Do not forget to set the properties of the image file (otherwise the compiler will skip them) you can look at the bin / debug folder after assembly and make sure that the file is where it should be:

  • Build Action: Content
  • Copy to output directory: Always copy

Another option is to make the file an embedded resource , after which you can access it as follows:

 Bitmap bitmap = Properties.Resources.FileName; 
+5
source

I like to declare images as resources in my XAML. I assume that you can still play with the code structure at the moment, and I hope there are not many new concepts for you. Here is how I do it:

Launch y by creating a folder in your project called Images. Add images of your cards by dragging them to a folder in Visual Studio. Make sure Build Action is set to Resource.

Create a new “resource dictionary” by pressing CTRL-SHIFT-A. Name it CardImages.xaml.

Link this file in App.xaml like this (it’s shown how to link several XAML files at once, but for this, of course, delete the line “AnyDictionary”!)

app.xaml:

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

Add this code to CardImages.xaml. Rename "MyBlackJackAssembly" to the name of your project assembly.

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

Then you can find them in your code as follows:

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

This will give you a WPF Label object that should show your map. This method works with everything that supports the ContentTemplate. Then you can add your shortcut to the grid of your user interface, I'm not sure how you display your maps on the screen.

For a blackjack application, I probably create a UserControl called "Card" to be able to generate it sort of. This encapsulates the map-forming logic into its own control, so the rest of the code can simply focus on working with cards.

XAML:

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

Or as in C #:

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

The method of loading some background images imported into the project resources:

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

Where "Project" is the name of the project. So basically you should add images to resources and call them relative uri.

+1
source

Source: https://habr.com/ru/post/1442206/


All Articles