Hello! I am trying to create an application that displays random images from Reddit every X seconds in new Form. Everything seems to be working fine except the PictureBox itself. In order to add the image to the PictureBox I am using PictureBox.LoadAsync() method. The whole process is being run using BackgroundWorker. Here is the whole code:
Here's what I am facing every time running the application:
Form1.cs:
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
namespace VRDCT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
InitializeComponent();
backgroundWorker1.RunWorkerAsync();
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.WorkerReportsProgress = true;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Stopwatch timer = new Stopwatch();
timer.Start();
while (true)
{
int seconds = int.Parse($"{timer.Elapsed.TotalSeconds:F0}");
if (seconds > 0 && seconds % 1 == 0)
{
Form _newForm = new Form();
FormCreation FC = new FormCreation();
FC.CreateForm(ref _newForm);
this.BeginInvoke((MethodInvoker)_newForm.Show);
Thread.Sleep(1000);
}
}
}
}
}
FormCreation.cs:
using System;
using System.Net.Http;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;
using System.Net;
using Newtonsoft.Json;
namespace VRDCT
{
public class FormCreation
{
public class ImageInformation
{
WebClient wc = new WebClient();
public int X { get; set; }
public int Y { get; set; }
public string URL { get; set; }
public void DownloadImageAndGetDimensions(string url)
{
Random rnd = new Random();
string chars = "abcdefghijklmnopqrstuvwxyz";
string fileName = new string(Enumerable.Repeat(chars, 6)
.Select(s => s[rnd.Next(s.Length)]).ToArray());
string path = $"A:\\{fileName}.jpeg";
wc.DownloadFile(url, path);
GetImageDimensionX(path);
GetImageDimensionY(path);
}
public int GetImageDimensionX(string path)
{
Image imageInfo = Image.FromFile(path);
return this.X = imageInfo.Width;
}
public int GetImageDimensionY(string path)
{
Image imageInfo = Image.FromFile(path);
return this.Y = imageInfo.Height;
}
}
private string GetImageUrl(string url)
{
using (HttpClient hp = new HttpClient())
{
var response = hp.GetStringAsync(url).Result;
return response;
}
}
public void CreateForm(ref Form _form)
{
var desirializedObject = JsonConvert.DeserializeObject<ImageInformation>(GetImageUrl("https://meme-api.herokuapp.com/gimme"));
ImageInformation imageDimensions = new ImageInformation();
imageDimensions.DownloadImageAndGetDimensions(desirializedObject.URL);
_form.Width = imageDimensions.X;
_form.Height = imageDimensions.Y;
PictureBox PB = new PictureBox
{
Name = "catPictureBox",
Size = new Size(_form.Width, _form.Height),
Location = new Point(0, 0),
Visible = true
};
PB.LoadAsync(desirializedObject.URL);
_form.Controls.Add(PB);
}
}
}
Here's what I am facing every time running the application: