Results 1 to 4 of 4

Thread: image.fromfile query

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    7

    Question image.fromfile query

    I have been using the image.fromfile to set images to a text box but I always have to use a full file path. Like here;
    Code:
      PictureBox1.Image = Image.FromFile("G:\VB.NET\Application1\Resources\Image1.jpg")
    PictureBox2.Image = Image.FromFile("G:\VB.NET\Application1\Resources\Image2.jpg")
    However if I install the application on another computer then surely this won't work as it needs a path relative to where it is installed. I tried "\Resources\Image1.jpg" but this doesn't seem to work. I need to find away so it works on all computers. I don't know if it changes this when the application is published but I don't have access to a second computer to test it. Any one know how to do it?

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: image.fromfile query

    There are numerous ways, but the simple one is to use Application.StartupPath and combine it with IO.Path.Combine.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    7

    Re: image.fromfile query

    I have tried this but it keeps bringing up an error 'FileNotFoundException was unhandled' this is my code. The file defiantly exists, as it works on my computer like above.
    Code:
    Dim StartPath, pic1, f1 As String
            StartPath = Application.StartupPath
            pic1 = "Resources\Image1.jpg"
           f1 = Path.Combine(StartPath, pic1)
            Main.PictureBox1.Image = Image.FromFile(f1)
    Last edited by Uranium Falcoln; May 23rd, 2011 at 11:06 AM. Reason: spelling mistake

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: image.fromfile query

    Whenever you encounter "file not found" exception, always messagebox the file path to inspect it. In your case, I strongly beleive that you didn't copy the "Resources" folder to the "bin" directory of your project.
    When you debug run your application, it runs from the "bin" folder of your project and thus application.startuppath returns the path to the "bin" folder.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width