内存与Image.FromFile没有流

问题描述 投票:-1回答:1

我有一个代码,我将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

更新:我发现实际图像格式不正确,并且无法在文件资源管理器中打开。有人知道为什么吗?

................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................

c# windows forms file out-of-memory
1个回答
0
投票

web.DownloadFileAsync(imageUrl, fileName);在后台下载,因此在尝试打开图片之前必须等待它完成。您可以订阅OnDownloadFileCompleted事件以了解下载完成的时间。

另一种方法是用同步下载替换:

web.DownloadFile(imageUrl, fileName);

但是,如果从主线程执行该操作,则会在下载图片时冻结UI。所以,要小心使用。

© www.soinside.com 2019 - 2024. All rights reserved.