Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > PictureBox Path to Image
 

PictureBox Path to Image

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.
Pressed Rat  Tuesday, August 11, 2009 8:27 PM
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
Richard_Brundritt  Tuesday, August 11, 2009 10:24 PM
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
Richard_Brundritt  Tuesday, August 11, 2009 10:24 PM

You can use google to search for other answers

Custom Search

More Threads

• c# getting crazy with a simple insert into....
• Generating Update/Insert/Delete Commands with JOIN Query
• Trimming leading and trailing spaces from SQL Server 2005 Express using VC# 2005 to view in Datagridview.
• how to change a user entered value in a datagrid
• datagridview column header dropdown selector (not a filter)
• No Spacing Custom Validation Code
• Eliminar um elemento da DataGridView com o evento UserDeletingRow e UserDeletedRow
• AUTO FILTERING WHEN USING JUNCTION TABLE
• How to get the valuemember from a DataGridViewComboBoxCell
• Error updating datatable"Dynamic sql generation not supported against Multiple Tables"