-1

フレームワーク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;
        } 

別の方法を探しています。何ができるのかわからない。どうすればそれを処理できるか知りたいです。

4

4 に答える 4

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); 
于 2012-07-28T11:35:51.067 に答える
2

デスクトップアプリケーションに画像のURLを表示したい。したがって、前に画像をダウンロードする必要があります。DownloadFileメソッドを呼び出してWebClientを使用する

于 2012-07-28T11:31:16.897 に答える
1

ピクチャーボックスコントロールを使用してみてください。Webから画像をロードするためにこれを使用します

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

textbox、datagridview、picturebox、およびボタンを使用してフォームを作成します。データグリッド選択モードを全行選択に設定します。このコードを使用します:

  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;
        }
于 2012-07-28T11:30:48.810 に答える
1

このトピックを見て くださいhttp://social.msdn.microsoft.com/Forums/en/winforms/thread/312a7fb2-9411-450a-8032-ee169397fd96 それはあなたが探しているものかもしれません

于 2012-07-28T11:36:29.007 に答える