C
C#2y 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
TheRanger2y 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
YamiOP2y ago
it's an image from a file
TheRanger
TheRanger2y ago
u can however change Copy to output directory settings like in this image
TheRanger
TheRanger2y ago
to copy the files in your project's folder into your exe's folder when u compile/run ur program
Yami
YamiOP2y 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
Ꜳåąɐȁặⱥᴀᴬ
but curiously it's saying parameter is not valid, not file not found
Yami
YamiOP2y ago
OhNo
Iron
Iron2y 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
YamiOP2y ago
oh cool ill try
Yami
YamiOP2y 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
YamiOP2y ago
Smadge
TheRanger
TheRanger2y ago
well, wrong path then use Visual Studio's debugger to see the full path $debug
MODiX
MODiX2y 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
YamiOP2y ago
cool
Iron
Iron2y 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
YamiOP2y ago
ah ok i will take note of that o7
Iron
Iron2y ago
Use if (File.Exists(myFile))
TheRanger
TheRanger2y ago
its probably good to let it throw an exception to know if the file path is valid or not
Iron
Iron2y ago
True then use try catch
TheRanger
TheRanger2y 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
Iron2y ago
The code i provided should work tho right ? I mean if the file exists
TheRanger
TheRanger2y ago
yes but what if file doesnt exist? no action will be taken
Iron
Iron2y 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
TheRanger2y 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
Iron2y 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
TheRanger2y ago
MessageBox.Show($"File not found on {filename}");
Iron
Iron2y ago
Yes exactly thank you its hard writing on the phone 😶😂
Yami
YamiOP2y ago
HmmNoted
Iron
Iron2y ago
How did it go
Yami
YamiOP2y ago
It worked Thank you everyone for catpog
Accord
Accord2y 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.

Did you find this page helpful?