【问题标题】:How do I display an image from URL in C#? [closed]如何在 C# 中显示来自 URL 的图像? [关闭]
【发布时间】:2012-07-26 21:14:39
【问题描述】:

听说framework 2.0 支持图片url,但是找不到。有没有办法在 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;
        } 

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

【问题讨论】:

  • 你说了你不想想做的事,但你到底想用什么
  • 我说我正在寻找另一种方式..那是什么不重要..我只是不想使用http。
  • 另一种不使用网络从网络获取图像的方法?您究竟在寻找什么替代方案?你目前的方法有什么不好?
  • 如我所说,我听说有一种方法支持通过框架 2.0 或更高版本从 url 获取图像。但我不知道那是什么……我在某处读过。
  • 你说的是WebClient吗?你从哪里听到的?为什么不看那里?

标签: c# image url


【解决方案1】:

您可以使用此代码

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); 

【讨论】:

  • 它看起来不错,但它仍然可以下载 :) 我知道没有任何方法可以做到这一点.. 谢谢
【解决方案2】:

您想在桌面应用程序上显示图像 url。 所以你必须先下载图像。 通过调用 DownloadFile 方法使用 WebClient

【讨论】:

  • 谢谢你的回答..但是我知道我正在寻找另一种方式..
【解决方案3】:

【讨论】:

  • 是的,这是一个不错的方法,谢谢 :)
【解决方案4】:

尝试使用图片框控件。 使用它从网络加载图像

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

创建一个带有文本框、数据网格视图、图片框和按钮的表单; 将数据网格选择模式设置为全行选择。 使用此代码:

  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;
        }

【讨论】:

  • 我知道谢谢,但我会在 gridview 中使用图像,我将列添加为图像
猜你喜欢
  • 2021-06-26
  • 2020-06-25
  • 1970-01-01
  • 2017-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多