Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone
I'm working on an image processing project these days.
everything seems to be working effectively but suddenly it stopped.
I don't know what the reason is because it was successfully working before.

The idea is that I want the user to open a text file where some information we need is,path chosen by the user also contains 18 jpg photos which program should read and store in a bitmap array previously defined

This is the code:

C#
public partial class Form1 : Form
    {
        Bitmap[] Weapons_imgs = new Bitmap[18];
        double[,] Learning_Vectors = new double[18, 2];

        Rectangle[] Weapons_rects;
        BlobCounter BC = new BlobCounter();
        GrayscaleY Gray_filter = new GrayscaleY();
        Invert negative_filter = new Invert();
        Threshold thre_filter = new Threshold(10);


private void Get_Data_Click(object sender, EventArgs e)
        {
            OpenFileDialog OFD = new OpenFileDialog();
            if (OFD.ShowDialog() == DialogResult.OK)
            {
                string str;
                StreamReader reader = File.OpenText(OFD.FileName);
                string dir = Path.GetDirectoryName(OFD.FileName);
                for (int i = 0; (str = reader.ReadLine()) != null; i++)
                {
                    string[] strs = str.Split(':');
                    Weapons_imgs[i] = new Bitmap(dir+"\\"+strs[0]+".jpg") ;
                    Bitmap img = Gray_filter.Apply(Weapons_imgs[i]);
                    negative_filter.ApplyInPlace(img);
                    thre_filter.ApplyInPlace(img);
                    BC.ProcessImage(img);
                    Weapons_rects = BC.GetObjectsRectangles();

                    double WH = (double)Weapons_rects[0].Width / (double)Weapons_rects[0].Height;
                    Learning_Vectors[i, 0] = WH;
                    Learning_Vectors[i, 1] = double.Parse(strs[1]);

                    Weapon_Features.Series[0].Points.AddXY(Learning_Vectors[i, 0], Learning_Vectors[i, 1]);
                    Weapon_combo.Items.Add(strs[0]);
                }
}
}



when coding it doesn't generate errors but when executing it show me this message:
"
C#
An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll

"

and it points on this line of code:
C#
Weapons_imgs[i] = new Bitmap(dir+"\\"+strs[0]+".jpg") ;




what's the reason of all that?
I don't know what to do because I don't the reason for this

What I have tried:

I've tried to convert the photos to bitmap images and change the line of code to be like:

Weapons_imgs[i] = new Bitmap(dir+"\\"+strs[0]+".bmp");

and I rebuilt the whole project
Posted
Updated 30-Apr-16 21:28pm
v3
Comments
Richard MacCutchan 1-May-16 3:00am    
The chances are that the one of the variables dir or strs[0] does not contain a valid path/file name. Use your debugger to check it.
Mujeeba Haj Najeeb 1-May-16 3:18am    
your hint is really effective because I find that I've edited the text file and never remember that at all
I undo all the changes I made before and it works again
Thanks
Richard MacCutchan 1-May-16 3:28am    
There is a lesson to be learned here: do the debugging first.
BillWoodruff 1-May-16 3:48am    
Virtual vote of #5 here, since actual vote of #5 ain't possible.
Richard MacCutchan 1-May-16 7:31am    
Thanks, you can owe me the 5. :))

1 solution

Start with the debugger: change that slightly so it's easier to debug:
C#
string path = dir + "\\" + strs[0] + ".jpg";
Weapons_imgs[i] = new Bitmap(path);
And place a breakpoint on the second line.
Run your app and when it reaches the Bitmap constructor it will stop.
Look at the value in path
Does it look right? Are there any odd backslashes, or other characters?
Use Windows explorer to go to the folder and confirm that a file with that exact name exists.
Almost certainly, the full path to the file is rubbish - but only you can tell: we can't run your code!
 
Share this answer
 
Comments
Mujeeba Haj Najeeb 1-May-16 3:21am    
I find that I edited the text file and forgot it so I undo all the changes and come back to work again
I appreciate your help .. thanks
OriginalGriff 1-May-16 3:44am    
You're welcome!
manigandan v 1-May-16 5:38am    
can you help me solve bitmap parameter is not valid exception in asp.net?
OriginalGriff 1-May-16 5:47am    
Don't "tout" for specific people to answer you: you have a QA question and it's rude to get impatient and "fish" for responses.
manigandan v 1-May-16 6:10am    
i'm extremely sorry for this

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900