CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2008
    Posts
    10

    pictures in the program

    I use following line to get an image file.

    Code:
    pictureBox2.Image = System.Drawing.Image.FromFile("C:\\...")
    But when I try to use the program on another computer, I have to change the path of the picture?

    Can anyone, please, offer another way to acquire image files?

    Many Thanks...

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: pictures in the program

    1) Embed the picture as a resource.
    or
    2) Change the path to a relative one
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Dec 2008
    Posts
    10

    Re: pictures in the program

    How about this

    Code:
    System.IO.Path.GetFullPath(path)
    ??

  4. #4
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: pictures in the program

    use the app.config to hold a file path to the image directory

  5. #5
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: pictures in the program

    if you don't embed the image (which is what I'd suggest you do. vs 2005+ has a resource editor that makes doing this a brainless operation), then you can include an image folder relative to the exe folder.

    Code:
    string exeLocation = Assembly.GetExecutingAssembly().Location;
    string exeDir = Path.GetDirectoryName(exeLocation);
    string imageDir = Path.Combine(exeDir, "images");
    string imageLocation = Path.Combine(imageDir, "mypic.png");

  6. #6
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: pictures in the program

    it is easier to do this:

    Code:
    pictureBox1.Image = System.Drawing.Image.FromFile("images/test.jpg");
    images folder should be in the same folder that your exe file resides.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  7. #7
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: pictures in the program

    If I recall correctly (and I could be wrong), if you specify a path like that, it will use the working directory which isn't always the same as the executable path.

  8. #8
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: pictures in the program

    Quote Originally Posted by MadHatter View Post
    If I recall correctly (and I could be wrong), if you specify a path like that, it will use the working directory which isn't always the same as the executable path.
    That's correct, you are not wrong

  9. #9
    Join Date
    Dec 2008
    Posts
    2

    Re: pictures in the program

    You may also place the picture in the same folder as the exe & use this

    string picPath = Application.StartupPath + "\\Pic.jpg"

    Kind Regards,
    Ali Bilal Yousaf

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

Featured