Loading img from url to DataGridView in WinForms

1.8k Views Asked by At

My loading data to grid look like this:

MainForm.cs

public MainForm()
{
 InitializeComponent();
 _mainWindowViewModel = new MainWindowViewModel();
 dgvMovies.DataSource = _mainWindowViewModel.MovieRowsViewModels;    
}

MainWindowViewModel.cs

public List<MovieRowViewModel> MovieRowsViewModels
        {
            get
            {
                return _movieRepository.GetAll().Select(n => new MovieRowViewModel
                {

                    ID = n.ID,
                    Title = n.Title,
                    Year = n.Year,
                    Poster = n.Poster
                }).ToList();
            }

    }

Method GetAll()

 public IQueryable<Model.Movie> GetAll()
        {
            using (var db = new Context())
            {
                return db.Movie.ToList().AsQueryable();
            }
        }

and the MovieRowViewModel.cs

class MovieRowViewModel
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public int Year { get; set; }
        public string Poster { get; set; }
    }

Question is how to load image from the url to this grid?

1

There are 1 best solutions below

1
On BEST ANSWER

That worked for me:

        DataTable t = new DataTable();
        t.Columns.Add("ID");
        t.Columns.Add("Poster");
        t.Columns.Add(new DataColumn("Img", typeof(Bitmap)));

        Bitmap b = new Bitmap(50, 15);
        using (Graphics g = Graphics.FromImage(b))
        {
            g.DrawString("Loading...", this.Font, new SolidBrush(Color.Black), 0f, 0f);
        }
        t.Rows.Add(new object[] { "1", "http://colorvisiontesting.com/images/plate%20with%205.jpg", b });
        dataGridView1.DataSource = t;

        ThreadPool.QueueUserWorkItem(delegate
        {
            foreach (DataRow row in t.Rows)
            {
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(row["Poster"].ToString());
                myRequest.Method = "GET";
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(myResponse.GetResponseStream());
                myResponse.Close();
                row["Img"] = bmp;
            }
        });

I have found it in this answer, so credit to jmservera

EDIT: If you want to specify the size of the loaded image, you need to check out all Bitmap constructors:

There is:

Bitmap b = new Bitmap(30, 10);

when it comes to loading image from URI, you have to change...

            foreach (DataRow row in t.Rows)
            {
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(row["Poster"].ToString());
                myRequest.Method = "GET";
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                Image img = Bitmap.FromStream(myResponse.GetResponseStream());
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(img, 30, 10);
                myResponse.Close();
                row["Img"] = bmp;
            }