private Control GetImage(Main main, DatabaseRowEntry entry, DatabaseRowEntry.KeyValuePair<string, string> image, int row) { Picture control = null; // First check if we've already downloaded this image. string cache = Path.Combine(Path.GetDirectoryName(Properties.Settings.Default.Collection), @"cache"); if (!File.Exists(cache)) Directory.CreateDirectory(cache); cache = Path.Combine(cache, image.Key); if (File.Exists(cache)) { control = new Picture(entry) { Image = Image.FromFile(cache), Width = 130, Height = 130, SizeMode = PictureBoxSizeMode.Zoom }; } else { control = new Picture() { Image = Properties.Resources.loading, Width = 130, Height = 130, SizeMode = PictureBoxSizeMode.CenterImage}; var imageDownloader = new DownloadImage(main, entry, control, row, image.Value, cache); new Thread(new ThreadStart(imageDownloader.Download)).Start(); } return control; }
// This method that will be called when the thread is started public void Download() { try { var imageArray = main.GoogleDrive.DownloadImage(uri); var picture = new Picture() { Image = Properties.Resources.aboveTradera, Width = 130, Height = 130, SizeMode = PictureBoxSizeMode.CenterImage }; if (imageArray != null) { using (MemoryStream mStream = new MemoryStream(imageArray)) { picture = new Picture(entry) { Image = Image.FromStream(mStream), Width = 130, Height = 130, SizeMode = PictureBoxSizeMode.Zoom }; picture.Image.Save(cache); } } main.TableLayoutPanelCancellations.Invoke((MethodInvoker)(() => { try { int index = main.TableLayoutPanelCancellations.Controls.GetChildIndex(placeHolder, true); main.TableLayoutPanelCancellations.Controls.RemoveAt(index); main.TableLayoutPanelCancellations.Controls.Add(picture, 1, row); } catch { } })); } catch (Exception) { throw; } }
private Control GetImage(ImageList imageList, String cancellation) { var label = new Label() { Text = cancellation }; if (cancellation.Length != 0 && cancellation[0] == 'N') { String key = Cancellation(cancellation); int index = imageList.Images.IndexOfKey(key + ".jpg"); if (index != -1) { var picturebox = new Picture() { Image = imageList.Images[index], Width = 130, Height = 130 }; picturebox.Controls.Add(label); label.BackColor = Color.Transparent; label.BringToFront(); return picturebox; } label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; label.Dock = DockStyle.Fill; return label; } int jndex = imageList.Images.IndexOfKey(cancellation + ".jpg"); if (jndex != -1) { return new Picture() { Image = imageList.Images[jndex], Dock = DockStyle.Fill, Width = 64, Height = 64, SizeMode = PictureBoxSizeMode.Zoom }; } return null; }