我有一个代码,在其中将picturebox.image设置为下面的filePath,但是当我运行该程序时,它将引发内存异常。图像是.jpg格式。正如您在代码中看到的那样,我没有使用流。我是否需要使用I流以免发生异常?如果是这样,那我该怎么用?
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)
{
}
}
}
这是例外:
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
更新: 我发现实际的图像格式不正确,并且无法在文件资源管理器中打开。有人知道为什么吗?
............................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... .............
答案 0 :(得分:0)
web.DownloadFileAsync(imageUrl, fileName);
在后台下载,因此您必须等待其完成才能尝试打开图片。您可以订阅OnDownloadFileCompleted
事件,以了解下载何时完成。
另一种方法是替换为同步下载:
web.DownloadFile(imageUrl, fileName);
但是,如果从主线程执行该操作,则会在下载图片时冻结UI。因此,请谨慎使用。