如何在C#中显示URL中的图像?

时间:2012-07-28 11:21:55

标签: c# image url

我听说框架2.0支持图片网址,但我找不到它。有没有办法直接从C#中的Url显示图像? (桌面应用程序)

通常我遵循的方法是在返回图像后下载图像。这是我的代码..但我不想遵循那种方式。所以我正在寻找一种不使用Httpwebrequest或类似的方法..

  public Image DownloadImage(string _URL)
        {
            Image _tmpImage = null;

            try
            {
                // Open a connection
                System.Net.HttpWebRequest _HttpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(_URL);

                _HttpWebRequest.AllowWriteStreamBuffering = true;

                // You can also specify additional header values like the user agent or the referer: (Optional)
                _HttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
                _HttpWebRequest.Referer = "http://www.google.com/";

                // set timeout for 20 seconds (Optional)
                _HttpWebRequest.Timeout = 20000;

                // Request response:
                System.Net.WebResponse _WebResponse = _HttpWebRequest.GetResponse();

                // Open data stream:
                System.IO.Stream _WebStream = _WebResponse.GetResponseStream();

                // convert webstream to image
                _tmpImage = Image.FromStream(_WebStream);

                // Cleanup
                _WebResponse.Close();
                _WebResponse.Close();
            }
            catch (Exception _Exception)
            {
                // Error
                Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
                return null;
            }

            return _tmpImage;
        } 

我正在寻找另一种方式。我不知道什么可以..?我想学习如何处理......

4 个答案:

答案 0 :(得分:4)

您可以使用此代码

string remoteUri = "http://www.yourSite.com/library/homepage/images/";
string fileName = "YourImagegif", 
myStringWebResource = null;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource,fileName); 

答案 1 :(得分:2)

您想在桌面应用上显示图片网址。 所以你必须先下载图像。 通过调用DownloadFile方法

来使用WebClient

答案 2 :(得分:1)

尝试使用picturebox控件。  用它来加载来自网络的图像

string imageLink="http://where.is/image.tld";
pictureBox1.ImageLocation= imageLink;

使用textbox,datagridview,picturebox和button创建表单; 将datagrid选择模式设置为fullrow选择。 使用此代码:

  private void button1_Click(object sender, EventArgs e)
        {
            string imageLink= textBox1.Text;
           try
                    {
                        int i;
                        i = dataGridView1.Rows.Add(new DataGridViewRow());
                        dataGridView1.Rows[i].Cells["Column1"].Value = imageLink;


                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show("error");
                    }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            string img = dataGridView1.SelectedRows[0].Cells["Column1"].Value.ToString();


            pictureBox1.ImageLocation = img;
        }

答案 3 :(得分:1)