Home > Net >  Unable To Populate Pictureboxes
Unable To Populate Pictureboxes

Time:12-13

I am very stuck. I have been trying different ways to get 6 results of avatar strings into the Load methods of a picturebox control array.

This is what I have to iterate the (mysql result set) value string to the pictureboxes

PictureBox[] avatars = { peer1, peer2, peer3, peer4, peer5, peer6 };

foreach (DataRow row in dt.Rows)
{
   string url = "https://example.com/"   row["avatar"];

   for (int i = 0; i < avatars.Length; i  )
   {
       avatars[i].Load(url);
   }
}

But every picturebox shows up white (blank). If I set the SQL query to LIMIT 1, I get the same first avatar for every picturebox. But It wont allocate accordingly, or at all.

enter image description here

How can I loop the urls into my 6 pictureboxes correctly? Thank you

EDIT

I've also tried this to no luck

List<string> AvatarList = new List<string>();

PictureBox[] avatars = { peer1, peer2, peer3, peer4, peer5, peer6 };

foreach (DataRow row in dt.Rows)
{
    AvatarList.Add("https://example.com/"   row["avatar"]);
}

for (int i = 0; i < AvatarList.Count; i  )
{
    for (int x = 0; x < avatars.Length; x  )
    {
         avatars[x].Load(AvatarList[i]);
     }
}

PictureBox[] avatars = { peer1, peer2, peer3, peer4, peer5, peer6 };

foreach (var pb in avatars)
{
   foreach (DataRow row in dt.Rows)
   {
        string url = "https://example.com/"   row["avatar"];
        pb.Load(url);
    }
}

SOLVED

Those reading in this similar situation, the answer is below

private bool CacheReadAvatars = true; // Image caching?
PictureBox[] avatars = { peer1, peer2, peer3, peer4, peer5, peer6 };

            foreach (DataRow row in dt.Rows)
            {
                if (CacheReadAvatars != false)
                {
                    SaveImage("https://example.com/"   row["avatar"], CachePath   "/peer_"   row["user_id"]   ".jpg");
                    AvatarList.Add(CachePath   "/peer_"   row["user_id"]   ".jpg");
                }
                else
                {
                    AvatarList.Add("https://example.com/"   row["avatar"]);
                }
            }

            for (int i = 0; i < AvatarList.Count; i  )
            {
                if (CacheReadAvatars != false)
                {
                    avatars[i].Image = Image.FromFile(AvatarList[i]);
                }
                else
                {
                    avatars[i].Load(AvatarList[i]);
                }
            }
public void SaveImage(string imageUrl, string filename)
        {
            WebClient client = new WebClient();
            Stream stream = client.OpenRead(imageUrl);
            Bitmap bitmap; bitmap = new Bitmap(stream);

            if (bitmap != null)
            {
                bitmap.Save(filename, ImageFormat.Jpeg);
            }

            stream.Flush();
            stream.Close();
            client.Dispose();
        }

I took the liberty of allowing image caching in case I want to limit HTTP requests, data usage etc.

CodePudding user response:

Why did you use two loop? If the URLs of the images are correct, the following code should work.

PictureBox[] avatars = { peer1, peer2, peer3, peer4, peer5, peer6 };

int i = 0;
foreach (DataRow row in dt.Rows)
{
    string url = "https://example.com/"   row["avatar"];
    avatars[i].Load(url);
    i  ;
}
  • Related