none
How to display a picture at url? RRS feed

  • Question

  • Below is a snippet of my program to show a picture from url which was verified using web browser:

                        picMatcheds[picId] = new PictureBox();
                        picMatcheds[picId].Width = 100;
                        picMatcheds[picId].Height = 100;
                        picMatcheds[picId].Location = new Point (650, 100);
                        string url = @"http://localhost:8099/image/" + imageDataId;
                        picMatcheds[picId].Load(url);
                        picMatcheds[picId].Show();

    I can't see picture shown at my program. How come?

    • Moved by CoolDadTx Monday, June 17, 2019 1:41 PM Winforms related
    Friday, June 14, 2019 9:16 AM

Answers

  • I followed the way at https://stackoverflow.com/questions/2269607/how-to-programmatically-download-a-large-file-in-c-sharp to download it and then show it. It works. But my question now is how to make it invisible or make it look like just a blank square.

    The code snippet is:

                    picMatcheds[picId] = new PictureBox();
                    picMatcheds[picId].Width = 100;
                    picMatcheds[picId].Height = 100;
                    picMatcheds[picId].Location = new Point (700, 100 * picId);
                    picMatcheds[picId].SizeMode = PictureBoxSizeMode.StretchImage;
                    string url = @"http://localhost:8099/image/" + imageDataId;
                    this.Controls.Add(picMatcheds[picId]);
                    WebClient client0 = new WebClient();
                    Uri url0 = new Uri(url);
                    client0.DownloadFile(url0, @"d:\test\testpicfile");
                    /* Bitmap img2show = new Bitmap(@"d:\test\testpicfile");
                    picMatcheds[picId].Image = (Image)img2show; */
                    picMatcheds[picId].Visible = true;
                    var request = WebRequest.Create(@"d:\test\testpicfile");
                    using (var response = request.GetResponse())
                    using (var stream = response.GetResponseStream())
                    {
                        picMatcheds[picId].Image = Image.FromStream(stream);
                    }

    Monday, June 17, 2019 6:18 AM

All replies

  • First of all, are you sure, that you're PictureBox is visible (Z-index, position on form visible)? Cause you haven't add it to the form's control collection as shown in the ctor example.

    And take a look at your web server logs, whether the request was correctly handled.

    Friday, June 14, 2019 9:26 AM
  • I updated it and found a better result. At least, a small white box was shown. But why my real picture not shown? Below is the updated snippet:

            private void ShowMatchedPic(string matchIdResp)
            {
                RestClient client = new RestClient();
                client.EndPoint = @"http://127.0.0.1:8099/";
                PictureBox[] picMatcheds = new PictureBox[4];
                PictureBox testPicBox = new PictureBox();
                int picId = 0;
                int inner;
                JObject jo = JObject.Parse(matchIdResp);
                JArray ja = jo["value"] as JArray;
                foreach (var matchedId in ja)
                {
                    GetFaceDataFmId infoGetImgId = new GetFaceDataFmId();
                    inner = matchedId["BestFaceId"].Value<int>();
                    matchList.Text += (inner.ToString());
                    // Got the BastBaseId, then to get ImageDataId
                    infoGetImgId.Id = inner;
                    string imgDataRespMsg;
                    string getImgDataUrl;
                    getImgDataUrl = @"odata/Faces(" + inner.ToString() + @")";
                    client.PostData = JsonConvert.SerializeObject(infoGetImgId);
                    var resultPost = client.HttpRequestCreated(getImgDataUrl, out imgDataRespMsg);
                    // To parse ImageDataId
                    jo = JObject.Parse(imgDataRespMsg);
                    var imageDataId = jo["ImageDataId"].Value<string>();
                    if (picId == 0)
                    {
                        picMatcheds[picId] = new PictureBox();
                        picMatcheds[picId].Width = 100;
                        picMatcheds[picId].Height = 100;
                        picMatcheds[picId].Location = new Point (650, 100);
                        string url = @"http://localhost:8099/image/" + imageDataId;
                        picMatcheds[picId].Load(url);
                        this.Controls.Add(picMatcheds[picId]);
                        picMatcheds[picId].Show();
                    }
                }
            }


    Friday, June 14, 2019 9:43 AM
  • Hello,

    Try the following then try with your image.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using static System.Drawing.Bitmap;
    
    namespace WindowsFormsApp2
    {
        public partial class Form1 : Form
        {
            private readonly PictureBox _pictureBox1 = new PictureBox();
            public Form1()
            {
                InitializeComponent();
    
                _pictureBox1.Width = 100;
                _pictureBox1.Height = 100;
                _pictureBox1.Location = new Point(650, 100);
    
                Controls.Add(_pictureBox1);
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var request = WebRequest.Create("http://www.gravatar.com/avatar/6810d91caff032b202c50701dd3af745?d=identicon&r=PG");
    
                using (var response = request.GetResponse())
                using (var stream = response.GetResponseStream())
                {
                    _pictureBox1.Image = Image.FromStream(stream);
                }
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, June 14, 2019 10:56 AM
    Moderator
  • I created a brand new project containing exact codes as you suggested and ran it. The result is the same, only a blank box picture was shown. Surely, I replace the url to mine and I am sure it's valid because I copied it from my browser's address field.
    Friday, June 14, 2019 12:00 PM
  • Then look at your web server logs, whether you hit your server at all.. e.g. AV or firewalls may block access. It may work in the browser, cause an exception on the executable name may exist.

    Friday, June 14, 2019 12:19 PM
  • I created a brand new project containing exact codes as you suggested and ran it. The result is the same, only a blank box picture was shown. Surely, I replace the url to mine and I am sure it's valid because I copied it from my browser's address field.
    So to clarify, you used the web address I provided then follow what Stefan recommended.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, June 14, 2019 12:39 PM
    Moderator
  • So you're running a web app on your local machine that has the picture and then you have a Winforms app that is using a PictureBox to display that image? Paste that URL into the browser window. Does it show up? If so then the problem is on the Winforms side. Note that PictureBox doesn't support all image formats so it could be something as simple as the wrong image. Temporarily use a different image to see if the problem goes away.

    If the image doesn't show up then debug the web app you're hosting. This looks like a REST call so set a breakpoint in your image controller. Walk through the code and confirm it is pulling back the correct image.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, June 14, 2019 1:38 PM
  • I surely changed the web address in your codes to mine.
    Monday, June 17, 2019 12:51 AM
  • I followed the way at https://stackoverflow.com/questions/2269607/how-to-programmatically-download-a-large-file-in-c-sharp to download it and then show it. It works. But my question now is how to make it invisible or make it look like just a blank square.

    The code snippet is:

                    picMatcheds[picId] = new PictureBox();
                    picMatcheds[picId].Width = 100;
                    picMatcheds[picId].Height = 100;
                    picMatcheds[picId].Location = new Point (700, 100 * picId);
                    picMatcheds[picId].SizeMode = PictureBoxSizeMode.StretchImage;
                    string url = @"http://localhost:8099/image/" + imageDataId;
                    this.Controls.Add(picMatcheds[picId]);
                    WebClient client0 = new WebClient();
                    Uri url0 = new Uri(url);
                    client0.DownloadFile(url0, @"d:\test\testpicfile");
                    /* Bitmap img2show = new Bitmap(@"d:\test\testpicfile");
                    picMatcheds[picId].Image = (Image)img2show; */
                    picMatcheds[picId].Visible = true;
                    var request = WebRequest.Create(@"d:\test\testpicfile");
                    using (var response = request.GetResponse())
                    using (var stream = response.GetResponseStream())
                    {
                        picMatcheds[picId].Image = Image.FromStream(stream);
                    }

    Monday, June 17, 2019 6:18 AM