locked
"Parameter is not valid" - new Bitmap() RRS feed

  • Question

  • Hello, I'm trying to make an Image from a file...
    I am positive that this code worked a few compiles earlier, and having not changed this code I cannot see why it would suddenly stop working... suffice to say it has...

    private void ofdImage_FileOk(object sender, CancelEventArgs e)
            {
                foreach (string fn in ofdImage.FileNames){
                    if ((fn.EndsWith("jpg")) || (fn.EndsWith("jpeg")))
                    {
                        addImgToRepo(fn);
                    }
                }
            }

            private void addImgToRepo(string fn)
            {

                Image img = new System.Drawing.Bitmap(fn);
    ....


    }

    The code breaks on Image img = ... saying "The Parameter is not valid.". 
    For example, I open C:\000.jpg and this gives the string fn = "C:\\000.jpg". This file exists!

    If anyone can shed any light on this I'd be most appreciative! It makes no sense to me!
    Monday, April 21, 2008 9:45 AM

Answers

  • Hi,

     

    The "Parameter is not valid" comes out because the file is and empty file that have 0 byte. 

     

    You can use Image.Save() to save a picture into the file.

     

    Code Snippet

                 string imgPath = @"C:\test3.jpg";

                Image img = new Bitmap(imgPath);

                img.Save(@"C:\test3_copy.jpg", ImageFormat.Jpeg);

     

     

     If you wanna use File.copy(), just do as following.

     

    Code Snippet

                string path = @"C:\test3.jpg";

                string path2 = @"C:\test4.jpg";

                try

                {

                    // Ensure that the target does not exist.

                    File.Delete(path2);

                    // Copy the file.

                    File.Copy(path, path2);

                    MessageBox.Show(path+" has copied to "+ path2);

                }

     

     

     

    For more about File.Copy(), please read the msdn File Class.

     

    Wishes.

     

    Tuesday, April 22, 2008 8:31 AM

All replies

  • Turns out I had somehow written over the original files that I was using to test with a file of 0 bytes... clever!

    Incidentally, that was because I was trying to copy an image using File.Copy()... from an example piece of code... it just wrote 0 bytes! Odd?

    Monday, April 21, 2008 9:54 AM
  • Hi,

     

    The "Parameter is not valid" comes out because the file is and empty file that have 0 byte. 

     

    You can use Image.Save() to save a picture into the file.

     

    Code Snippet

                 string imgPath = @"C:\test3.jpg";

                Image img = new Bitmap(imgPath);

                img.Save(@"C:\test3_copy.jpg", ImageFormat.Jpeg);

     

     

     If you wanna use File.copy(), just do as following.

     

    Code Snippet

                string path = @"C:\test3.jpg";

                string path2 = @"C:\test4.jpg";

                try

                {

                    // Ensure that the target does not exist.

                    File.Delete(path2);

                    // Copy the file.

                    File.Copy(path, path2);

                    MessageBox.Show(path+" has copied to "+ path2);

                }

     

     

     

    For more about File.Copy(), please read the msdn File Class.

     

    Wishes.

     

    Tuesday, April 22, 2008 8:31 AM
  • Hello,

    I have a very similar issue, except that the file is 250KB.  It exists, the path is fine, but for some reason it also crashes on the


    Bitmap bmp = new Bitmap(fileName); 


    What could be the reason?  This same line of code worked for hundreds of other pictures?! :s


    Thanks,

    Alain
    Monday, September 15, 2008 4:59 PM

  • I've tried using an "Image" object instead and it crashes again, for the same file, but this time I get an "Out of Memory" exception.  Here is the full code involving my img object (part of huge foreach loop):

    // After x amount of successful passes, it finally crashes, with a valid file that can be viewed
    without any issues in IrfanView (Windows Picture and Fax viewer cannot show it, but does
    not crash; just says "Preview not available").

            Image img = System.Drawing.Bitmap.FromFile(fi.FullName);
            string resize400x300; 
            string resize150x113; 
     
            if (img.Height >= img.Width) 
                    resize400x300 = "300,400"
             resize150x113 = "113,150"
            } 
            else 
                    resize400x300 = "400,300"
             resize150x113 = "150,113"
    }

    img.Dispose(); // Release the memory.


    Any Ideas?


    Thanks,

    Alain
    Monday, September 15, 2008 5:27 PM