locked
JPG in PictureBox RRS feed

  • Question

  • The following is my code snippet:

    Image img = Image.FromFile(@"E:\Party\IMG_9882.JPG");
    pictureBox1.Image = img;

    While executing, FileNotFoundException is raised, inorder to verify, I wrote small console application to list the  images from directory "E:\Party", I found IMG_9882.JPG in the out list, why am I getting this Exception?

    Thanks in advance
    • Edited by Sri Hari Wednesday, June 4, 2008 3:08 AM modified path name
    • Moved by CoolDadTx Wednesday, June 4, 2008 1:06 PM Unrelated to the C# IDE, moving to the General forum until WinForms is available.
    • Moved by Peter Ritchie Friday, June 6, 2008 9:08 PM off-topic
    Wednesday, June 4, 2008 2:31 AM

Answers

  • Hi Sri Hari,

    To code a security program, maybe you can try to check the file was existed or not before you do some IO operation, please check the code snippet below.

    string filename = "E:\\Party\\IMG_9882.JPG";

    if (File.Exists(filename))

                {

                    Image img = Image.FromFile(filename);

                    pictureBox1.Image = img;

                }

                else

                {

                    MessageBox.Show("file found failed");

                }

    Regards,

    Xun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by jack 321 Monday, June 9, 2008 2:24 AM
    Friday, June 6, 2008 7:11 AM

All replies

  • Hi Sri Hari,

    To code a security program, maybe you can try to check the file was existed or not before you do some IO operation, please check the code snippet below.

    string filename = "E:\\Party\\IMG_9882.JPG";

    if (File.Exists(filename))

                {

                    Image img = Image.FromFile(filename);

                    pictureBox1.Image = img;

                }

                else

                {

                    MessageBox.Show("file found failed");

                }

    Regards,

    Xun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by jack 321 Monday, June 9, 2008 2:24 AM
    Friday, June 6, 2008 7:11 AM
  • Sri Hari said:

    The following is my code snippet:

    Image img = Image.FromFile(@"E:\Party\IMG_9882.JPG");
    pictureBox1.Image = img;

    While executing, FileNotFoundException is raised, inorder to verify, I wrote small console application to list the  images from directory "E:\Party", I found IMG_9882.JPG in the out list, why am I getting this Exception?

    Thanks in advance



    According to this MSDN documentation, if you code does not have PathDiscovery permission, there might be an exception too..

    So please add the following code above you class and see if it works:

    using System.Security.Permissions;

    [FileIOPermissionAttribute(SecurityAction.PermitOnly, PathDiscovery = "E:\\Party")]

    Hope this helps !!
    Always mark the post that helped you as "Answer". That helps future users to locate answers quickly.
    • Edited by Somnath Paul Friday, June 6, 2008 8:53 AM code edit
    • Proposed as answer by Somnath Paul Friday, June 6, 2008 8:55 AM
    Friday, June 6, 2008 8:50 AM
  •  For questions and discussions regarding client application development using Windows Forms controls and features, please see http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=8&SiteID=1

    http://www.peterRitchie.com/blog
    Friday, June 6, 2008 9:06 PM