【问题标题】:Out Of Memory with Image.FromFile without stream没有流的 Image.FromFile 内存不足
【发布时间】:2019-07-31 08:40:27
【问题描述】:

我有一个代码,我将 picturebox.image 设置为下面的 filePath,但是当我运行该程序时,它会引发内存异常。图像为 .jpg 格式。正如您在代码中看到的那样,我没有使用流。我是否需要使用我的流以便不会发生异常?如果是这样,我该如何使用它?

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:\Users\user\source\repos\WindowsFormsApp1\WindowsFormsApp1\bin\Debug\artichokes.jpg 是一张有效图片?
  • 是的,我打开过不止一次
  • 哦不,它没有打开
  • 为什么不是有效图片
  • 尝试使用web.DownloadFile 而不是web.DownloadFileAsync

标签: c# windows forms file out-of-memory


【解决方案1】:

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

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

web.DownloadFile(imageUrl, fileName);

但是,如果您从主线程执行该操作,您将在图片下载时冻结 UI。所以,请谨慎使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    • 1970-01-01
    • 2015-12-02
    • 2016-10-10
    • 2019-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多