none
display image from the internet in a PictureBox (C#)

    Question

  • Hi all. Im trying to display images from the web into a picturebox control. when the form loads, i'd like for the pictureBox control to read a URL from a textbox control and load the image. i found the VB equivalent code to do this but im a newbie and cant translate it into C#:

    PictureBox1.Image = New System.Drawing.Bitmap(New IO.MemoryStream(New System.Net.WebClient().DownloadData(TextBox1.Text)))  // Assumes there is a textbox called textbox1 and a picturebox called picturebox1 on a form. Textbox1.text should be the URL of the picture.

    Is there an easier way to go about this? Any help is appreciated. Thanks in advance

    Jason

    Thursday, July 13, 2006 2:48 PM

All replies

  • Hi,

    how about

    pictureBox1.ImageLocation = textBox1.Text;

    or

    pictureBox1.Load(textBox1.Text); ?

    Andrej

    Thursday, July 13, 2006 2:53 PM
  • Hi Andrej. Thanks for the quick replay. for some reason, the ImageLoaction and Load property arent available to me. they dont appear for me =(
    Thursday, July 13, 2006 2:59 PM
  • Hi,

    you're probably using framework 1.1... That method and property are available with 2.0 only. So, to translate your code:

    pictureBox1.Image = new System.Drawing.Bitmap(new System.IO.MemoryStream(new System.Net.WebClient().DownloadData(textBox1.Text)));

    Andrej

    Thursday, July 13, 2006 3:08 PM
  • thanks again, im getting an error now when i try to build the solution:

    The type or namespace name 'IO' could not be found (are you missing a using directive or an assembly reference?)

    i dont understand how im getting this error seeing as how i used the using directive for the IO namespace  :

    using System;

    using System.Drawing;

    using System.Collections;

    using System.ComponentModel;

    using System.Windows.Forms;

    using System.Data;

    using System.Data.OleDb;

    using System.IO;

    have any idea what coule be the problem? thanks in advance

    Thursday, July 13, 2006 3:18 PM
  • Either use System.IO.MemoryStream or just MemoryStream...

    Andrej

    Thursday, July 13, 2006 3:37 PM
  • Hi Andrej. I have some similar problem. I want to get a webpage and show some it's data in a winApp. There is a captcha in this page and each time we call it's url(somesite/captcha) it regenerate another pic. I wanna to show other filed + captcha ,so my costumer can fill them and post to server. Pleas help me.
    Thursday, August 13, 2009 10:06 AM
  • WebRequest req = WebRequest.Create("http://www.google.co.il/Picture.jpg");
    Stream stream = req.GetResponse().GetResponseStream(); Image img = Image.FromStream(stream); this.pictureBox1.Image = img;

    Please mark the post as answer if it is helpfull to you because it boosts the members to answer more and more.
    • Proposed as answer by _SuDhiR_ Thursday, August 13, 2009 10:25 AM
    Thursday, August 13, 2009 10:25 AM
  • Worked for me :)
    Saturday, December 21, 2013 1:39 AM