C
C#β€’15mo ago
Yami

βœ… bitmap bug question

I use bitmap when clicking on a picturebox to change the image. It still runs in debug mode but after downloading the program package and installing it, when I click on it, it gives me this error. How to fix it? (code sample: private void pictureBox1_Click(object sender, EventArgs e) {
pictureBox1.Image = new Bitmap(Application.StartupPath + @"\Resources\Card Visit_Final-03.png", true); })
31 Replies
TheRanger
TheRangerβ€’15mo ago
image probably doesn't exist at that path maybe Application.StartupPath doesnt return what u think it does it Gets the path for the executable file that started the application ur exe is in your project's folder/bin/debug if you're running in debug mode and your file is probably in UrProject/Resources/Card Visit_Final-03.png and its probably trying to look in UrProject/bin/Debug/Resources/Card Visit_Final-03.png also is Card Visit_Final-03.png the file's name or is Card suppose to be a folder?
Yami
YamiOPβ€’15mo ago
it's an image from a file
TheRanger
TheRangerβ€’15mo ago
u can however change Copy to output directory settings like in this image
TheRanger
TheRangerβ€’15mo ago
to copy the files in your project's folder into your exe's folder when u compile/run ur program
Yami
YamiOPβ€’15mo ago
Oh ill try Thank you for the advice ah it still won't work, i tried using full path but it still won't work
Omnissiah
Omnissiahβ€’15mo ago
but curiously it's saying parameter is not valid, not file not found
Yami
YamiOPβ€’15mo ago
OhNo
Iron
Ironβ€’15mo ago
Hey try to right click your image in your solution and copy full path and paste it as path see if you get the same error Also set the bool to false and try It only defines if the picture is readonly or not Try private void pictureBox1_Click(object sender, EventArgs e) { string imagePath = Application.StartupPath + @"\Resources\Card Visit_Final-03.png"; if (File.Exists(imagePath)) { pictureBox1.Image = new Bitmap(imagePath); } } But first replace string imagePath with the path you copied from right clicking the image in solution , should look something like \\file\\etc.png When i copy the path by right clicking it in solution usually works for testing
Yami
YamiOPβ€’15mo ago
oh cool ill try
Yami
YamiOPβ€’15mo ago
oh my bad, I didn't include the image file in the packaging, and it generated an error, and now it shows this
Yami
YamiOPβ€’15mo ago
Smadge
TheRanger
TheRangerβ€’15mo ago
well, wrong path then use Visual Studio's debugger to see the full path $debug
MODiX
MODiXβ€’15mo ago
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
Yami
YamiOPβ€’15mo ago
cool
Iron
Ironβ€’15mo ago
Did you try to copy the path from solution folder Because if you use if(file.exist) etc should not give you that error
Yami
YamiOPβ€’15mo ago
ah ok i will take note of that o7
Iron
Ironβ€’15mo ago
Use if (File.Exists(myFile))
TheRanger
TheRangerβ€’15mo ago
its probably good to let it throw an exception to know if the file path is valid or not
Iron
Ironβ€’15mo ago
True then use try catch
TheRanger
TheRangerβ€’15mo ago
catch catches the exception, it wont crash the program you can just use if (!File.Exists) and display a messagebox to indicate the file isnt found you can even display the path of the file ur trying to access on the MessageBox
Iron
Ironβ€’15mo ago
The code i provided should work tho right ? I mean if the file exists
TheRanger
TheRangerβ€’15mo ago
yes but what if file doesnt exist? no action will be taken
Iron
Ironβ€’15mo ago
Ye but that why he should right click the png file click copy path and see if that path works. Also he needs to use using System.IO; but usually thats done by vs Oh i get your point xD yee
TheRanger
TheRangerβ€’15mo ago
its better to pop up a message box saying no file found on this path if File.Exists returned false displaying the full path might help you whats wrong
Iron
Ironβ€’15mo ago
@Yami add a else statement if (File.Exists(filename)) { // code for file } else { MessageBox.Show("File not found."); } As R mentioned above use the else block to check for whats wrong
TheRanger
TheRangerβ€’15mo ago
MessageBox.Show($"File not found on {filename}");
Iron
Ironβ€’15mo ago
Yes exactly thank you its hard writing on the phone πŸ˜ΆπŸ˜‚
Yami
YamiOPβ€’15mo ago
HmmNoted
Iron
Ironβ€’15mo ago
How did it go
Yami
YamiOPβ€’15mo ago
It worked Thank you everyone for catpog
Accord
Accordβ€’15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server