I have a code where I set the picturebox.image to the filePath below but when I run the program it throws a memory exeption. The image is a .jpg format. and as you can see in the code, I am not using a stream. Do I need to use I stream so that the exeption doesn't happen? If so then how do I use it?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;
using System.Web;
using System.Runtime.InteropServices;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public WebClient web;
public String html;
public Form1()
{
InitializeComponent();
List<string> plants = new List<string>();
web = new WebClient();
html = web.DownloadString("https://bonnieplants.com/how-to-grow/");
MatchCollection m1 = Regex.Matches(html, "<a href=\"https://bonnieplants.com/product-category/.+?\">(.+?)</a>", RegexOptions.Singleline);
foreach(Match m in m1)
{
string plant = m.Groups[1].Value;
plants.Add(plant);
}
plants.Sort();
comboBox1.Items.AddRange(plants.ToArray());
}
private void button1_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Uri imageUrl = new Uri("https://edge.bonnieplants.com/www/img/products/artichokes-400px-30.jpg");
string fileName = System.IO.Path.GetFileName(imageUrl.LocalPath);
fileName = fileName.Replace("-400px-30", "");
web.DownloadFileAsync(imageUrl, fileName);
string filePath = Application.StartupPath.ToString() + @"\" + fileName;
if(@"C:\Users\user\source\repos\WindowsFormsApp1\WindowsFormsApp1\bin\Debug\artichokes.jpg" == filePath)
{
Console.WriteLine(filePath);
}
pictureBox1.Image = Image.FromFile(Application.StartupPath.ToString() + @"\" + fileName);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
and here is the exeption :
System.OutOfMemoryException
HResult=0x8007000E
Message=Out of memory.
Source=System.Drawing
StackTrace:
at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
at System.Drawing.Image.FromFile(String filename)
at WindowsFormsApp1.Form1.comboBox1_SelectedIndexChanged(Object sender, EventArgs e) in C:\Users\user\source\repos\WindowsFormsApp1\WindowsFormsApp1\Form1.cs:line 56
at System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e)
at System.Windows.Forms.ComboBox.WmReflectCommand(Message& m)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsFormsApp1.Program.Main() in C:\Users\user\source\repos\WindowsFormsApp1\WindowsFormsApp1\Program.cs:line 19
UPDATE: I figured out that the actual image is not formatted correctly and does not open in the file explorer. Does anybody know why?
................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
web.DownloadFileAsync(imageUrl, fileName);
downloads in the background, so you have to wait for it to finish before trying to open the picture. You can subscribe to the OnDownloadFileCompleted
event to know when the download is finished.
An alternative is just to replace with a synchronous download:
web.DownloadFile(imageUrl, fileName);
However, if you execute that from the main thread, you will freeze the UI while the picture is downloading. So, use with care.
User contributions licensed under CC BY-SA 3.0