locked
PictureBox Path to Image RRS feed

  • Question

  • Hello.

    This is my first attempt at creating an application, so I'm having trouble finding answers, probably due to poor vocabulary.  My application is a simple SQL database interface form to my product database, which is working great.

    Now, I would like to be able to show a photo of each product in the form as users access a record.  Unfortunately the photo is not in my SQL database, but on my webserver, so I need to set the ImageLocation of my PictureBox to a path that includes the ProductID as the image name.  So, for a product 1234, my images url is "http://mywebsite.com/images/1234 .jpg".  I am trying to concatenate http://mywebsite.com/images/ + ProductID + .jpg in various ways, but having no luck.

    Is there a way to do this?  I appreciate any help. 
    Tuesday, August 11, 2009 8:27 PM

Answers

  • I'm not sure how your set up is but there are several ways to concatenate strings in .NET such as

    string URL = String.Format("http://mywebsite.com/images/{0}.jpg",ProductID);
     or
    string URL = "http://mywebsite.com/images/"+ProductID+".jpg";


    In cases where you have a lot of strings to concatenate you can use StringBuilder.

    Once you have your URL you will then need to read the image from the URL and create an ImageStream to load into your PictureBox. Here is an example of how to get the image from a URL:


    //Get image from URL
    System.Net.WebRequest request = System.Net.WebRequest.Create(URL);
    System.Net.WebResponse response = request.GetResponse();
    System.IO.Stream responseStream = response.GetResponseStream();

    Image myImage=Image.FromStream(responseStream);


    From here you would set your PictureBox.Image property equal to th myImage. i.e. PictureBox1.Image = myImage;

    You will need to add the following references:

    using System.Net;
    using System.IO;
    Windows Live Developer MVP - http://rbrundritt.spaces.live.com
    • Marked as answer by Kira Qian Tuesday, August 18, 2009 8:01 AM
    Tuesday, August 11, 2009 10:24 PM