C
C#ā€¢11mo ago
!Rushaan

āœ… Image source not being set with C#, WPF

I have a very basic C# statement but whenever it gets executed the image is not set and the image element disappears.
qrcode.Source = new BitmapImage(new Uri(@"/Images/scannn.png", UriKind.Relative));
qrcode.Source = new BitmapImage(new Uri(@"/Images/scannn.png", UriKind.Relative));
I have no clue why its not being set, the image is present in that directory, build action of the image is set to content and copy to output is set to copy always
15 Replies
Sir Rufo
Sir Rufoā€¢11mo ago
Pack URIs - WPF .NET Framework
Learn about the many ways to use uniform resource identifiers (URIs) to identify and load files in Windows Presentation Foundation (WPF).
!Rushaan
!RushaanOPā€¢11mo ago
Im setting the URI kind to relative and the qrcode.Source returns pack://application:,,,/Images/scannn.png man what should i do
Sir Rufo
Sir Rufoā€¢11mo ago
I cannot reproduce it ... works here as expected
Sir Rufo
Sir Rufoā€¢11mo ago
GitHub
GitHub - SirRufo/dc-ImageResourceAndContent
Contribute to SirRufo/dc-ImageResourceAndContent development by creating an account on GitHub.
!Rushaan
!RushaanOPā€¢11mo ago
I have no clue why it isnt working
Sir Rufo
Sir Rufoā€¢11mo ago
me too as you can see it is working in my project
!Rushaan
!RushaanOPā€¢11mo ago
yea since im creating a qr code its present in Bitmap format so i even converted it into Bitmap Image and tried to set it without any path stil didnt work
!Rushaan
!RushaanOPā€¢11mo ago
Sir Rufo
Sir Rufoā€¢11mo ago
I did not download your files, but I guess you create the image file from your app and then try to load it with the Uri path?
!Rushaan
!RushaanOPā€¢11mo ago
at first I create the image file and successfully save it and it gets saved i checked and then i set the saved image's location in the image source but that didnt work so then i tried something very straightforward which is setting the image source to an existing image in the Images folder but that didnt work either, so Ik i had the Bitmap so then i converted the Bitmap to BitmapImage and then set it but didnt work and finally rn I tried creating a new Image from code-behind itself and setting its source but didnt work either. In all of this i got the same result which is that the image is not visible
Sir Rufo
Sir Rufoā€¢11mo ago
The key problem is, that if you want to use thoses relative "resources" they have to know at compile time You can add a dummy png file and set the properties to content and copy always/copy if newer
!Rushaan
!RushaanOPā€¢11mo ago
ive tried that
Sir Rufo
Sir Rufoā€¢11mo ago
me too otherwise you have to use the full path and UriKind.Absolute
!Rushaan
!RushaanOPā€¢11mo ago
tried that too ig tried something very similar using System.IO.Path.Combine() and AppDomain.currentdomain base directory I also checked using File.Exists() and it was true . just found out that setting image source is working perfectly in a different XAML Page JUST MADE A PROGRESS there was a logic flaw in my code, when the function to set the image was called just after that there was a line of code that changed the image source but theres still some problem NVM I FIXED IT šŸ„¹
Sir Rufo
Sir Rufoā€¢11mo ago
you can close this topic when you post /close
Want results from more Discord servers?
Add your server