Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How to assign an image to a PictureBox dynamically ?
 

How to assign an image to a PictureBox dynamically ?

Hi,

I have a Windows PictureBox which needs to render its picture dynamically. I have the URL from which the picture is to be downloaded. How do i accomplish this ?

Thanks

Kulvinder  Friday, August 24, 2007 2:12 PM

You can use a WebClient object to download the image data from the URL into memory stream, and convert it to image; through this approach you’re able to set credential for the connection.

Code Snippet

void Form9_Load(object sender, EventArgs e)

{

string url = "your url here";

WebClient webClient = new WebClient();

CredentialCache myCache = new CredentialCache();

myCache.Add(new Uri(url), "Basic",

new NetworkCredential("your username", "your password "));

webClient.Credentials = myCache;

MemoryStream imgStream = new MemoryStream( webClient.DownloadData(url));

pictureBox1.Image = new System.Drawing.Bitmap(imgStream);

}

Dear Kulvinder,

Try this

string url = "your image path";

pBox.ImageLocation = url;

HTH,

Suprotim Agarwal

Suprotim Agarwal  Friday, August 24, 2007 2:49 PM

Thanks for your help. I actually find the solution the time i raised it in the forums but i have one question yet.

Suppose, i am using an authenticated proxy to access any internet resource. Will this solution work ?

There must be some way i have to tell picturebox that "Look, i am using an authenticated proxy. So download the image using these credentials"

How do i do this ?

Thanks

Kulvinder  Monday, August 27, 2007 5:36 AM

You can use a WebClient object to download the image data from the URL into memory stream, and convert it to image; through this approach you’re able to set credential for the connection.

Code Snippet

void Form9_Load(object sender, EventArgs e)

{

string url = "your url here";

WebClient webClient = new WebClient();

CredentialCache myCache = new CredentialCache();

myCache.Add(new Uri(url), "Basic",

new NetworkCredential("your username", "your password "));

webClient.Credentials = myCache;

MemoryStream imgStream = new MemoryStream( webClient.DownloadData(url));

pictureBox1.Image = new System.Drawing.Bitmap(imgStream);

}

I'm trying to use similar code to load an image from a site that requires a login.
Even though I'm using the proper credentials, I still get a 401 error when calling DownloadData.

tjproffitt  Thursday, November 15, 2007 8:11 PM

You can use google to search for other answers

Custom Search

More Threads

• Filter Data in DataGridView CellClick
• GridView using stored procedure for Select
• WMI data to database
• Bound Forms w/Untyped DataSets
• Graphic artifacts while scrolling DataGridView without scroll
• csv displayed in a datagridview
• How can i access the controls inside a spesific cell in DataGridView?
• Having problems nesting a datagridview....is it even possible.. I am complete newbiew to winforms as I have always devloped in asp.net
• HELP - Ultragrid ComboBox columns
• BindingSource refresh after row insert