Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 读取多个图像文件时的内存峰值_C#_Image_Winforms_Memory_Thumbnails - Fatal编程技术网

C# 读取多个图像文件时的内存峰值

C# 读取多个图像文件时的内存峰值,c#,image,winforms,memory,thumbnails,C#,Image,Winforms,Memory,Thumbnails,我正在尝试创建一个文件资源管理器,其中一个问题一直困扰着我,那就是试图修复从图像加载多个缩略图时出现的内存峰值。以下是我认为它所在的部分(全部用后台工作人员包装): static string[]imageType=新字符串[]{“jpeg”、“jpg”、“png”、“bmp”}; List filesList=新列表(); foreach(文件列表中的文件信息文件) filesList.Add(file.FullName); string[]files=filesList.ToArray();

我正在尝试创建一个文件资源管理器,其中一个问题一直困扰着我,那就是试图修复从图像加载多个缩略图时出现的内存峰值。以下是我认为它所在的部分(全部用后台工作人员包装):

static string[]imageType=新字符串[]{“jpeg”、“jpg”、“png”、“bmp”};
List filesList=新列表();
foreach(文件列表中的文件信息文件)
filesList.Add(file.FullName);
string[]files=filesList.ToArray();
NumericComparer nc=新的NumericComparer();
数组.排序(文件,nc);
foreach(文件中的字符串文件)
{
Application.OpenForms[“MDC_Explorer”].Invoke(新的MethodInvoker(()=>
{
Application.DoEvents();
}));
面板d=新面板();
d、 大小=新大小(parent.Width/5-10100);//图像高度:70px
d、 位置=新点(左侧,顶部);
d、 标签=文件;
d、 MouseEnter+=新的事件处理程序(项\u MouseEnter);
d、 MouseLeave+=新的事件处理程序(项_MouseLeave);
d、 BackgroundImageLayout=ImageLayout.Stretch;
标签l=新标签();
FileInfo FileInfo=新的FileInfo(文件);
l、 Text=Path.GetFileNameWithoutExtension(fileInfo.FullName);
l、 尺寸=新尺寸(d.宽度,25);
l、 位置=新点(0,75);
l、 TextAlign=ContentAlignment.TopCenter;
l、 前景色=颜色。来自argb(255,90,90,90);
l、 Font=新字体(parent.Font.fontflash,10,FontStyle.Regular);
l、 背景色=颜色。透明;
l、 MouseEnter+=新的事件处理程序(项\u MouseEnter);
l、 MouseLeave+=新的事件处理程序(项_MouseLeave);
l、 鼠标点击+=新鼠标点击器(项目\鼠标点击);
d、 添加(l);
PictureBox pb=新PictureBox();
字符串路径=文件;
if(Path.GetExtension(file.Replace)(“.”,“)=”lnk”)
{
iwshurantimelibrary.WshShell shell=新的iwshurantimelibrary.WshShell();
iwshurantimelibrary.IWshShortcut链接=(iwshurantimelibrary.IWshShortcut)shell.CreateShortcut(文件);
路径=link.TargetPath;
}
如果(!File.Exists(path)&&path.Contains(“程序文件(x86)”)
path=path.Replace(“程序文件(x86)”,“程序文件”);
位图bmp=Path.GetExtension(Path).Replace(“.”,”“)==“exe”?Icon.ExtractAssociatedIcon(Path.ToBitmap():(imageType.Contains(Path.GetExtension(Path).Replace(“.”,”)?新位图(Image.FromStream(newmemoryStream(File.ReadAllBytes(Path))),新大小(d.Width-20,80)):新位图(GetLargeIconForExtension(Path.GetExtension(Path)).ToBitmap());
添加(bmp);
int fp=(parent.Width/4)*3-50;
添加(新位图(bmp,新大小((fp/5),80));
pb.BackgroundImage=bmp;
pb.BackgroundImageLayout=ImageLayout.Center;
pb.尺寸=新尺寸(d.宽度-20,80);
pb.BackColor=颜色。透明;
pb.位置=新点(10,10);
pb.MouseEnter+=新的事件处理程序(项\u MouseEnter);
pb.MouseLeave+=新的事件处理程序(项目_MouseLeave);
pb.MouseClick+=新的MouseEventHandler(项目_MouseClick);
d、 添加(pb);
如果(左+(d.Width*2)+5>=父级宽度)
{
顶部+=d.高度+5;
左=5;
}
其他的
左+=d.宽度+5;
Application.OpenForms[“MDC_Explorer”].Invoke(新的MethodInvoker(()=>
{
父控件。添加(d);
}));
}

抱歉,如果我提供了很多代码,但我相信错误在于它获取缩略图并将其放入位图中,但我不确定如何降低内存使用率。

您正在创建图像和位图,而没有处理它们,因此在垃圾收集器销毁这些bmp之前,它们都在内存中

此外,在某些情况下,您读取内存中的所有图像文件,将其加载到流中,然后将其传递给image.FromStream,只读取image.FromFile要好得多

而不是:

Bitmap bmp = Path.GetExtension(path).Replace(".", "") == "exe" ?     
Icon.ExtractAssociatedIcon(path).ToBitmap() : 
(imageType.Contains(Path.GetExtension(path).Replace(".", "")) ? new 
Bitmap(Image.FromStream(new MemoryStream(File.ReadAllBytes(path))), 
new Size(d.Width - 20, 80)) : new Bitmap(GetLargeIconForExtension(Path.GetExtension(path))
.ToBitmap()));
这样做:

        Bitmap bmp;

        string ext = Path.GetExtension(path);

        if (ext == ".exe")
        {

            Icon ico = Icon.ExtractAssociatedIcon(path);
            bmp = ico.ToBitmap();
            ico.Dispose();

        }
        else
        {

            if (imageType.Contains(ext.Replace(".", "")))
            {

                Image img = Image.FromFile(path);
                bmp = new Bitmap(img, new Size(d.Width - 20, 80));
                img.Dispose();
            }
            else
            {

                Icon ico = GetLargeIconForExtension(ext);
                bmp = ico.ToBitmap();
                ico.Dispose();

            }

        }

您的最终目标(或者更好的设计目标)是什么?您预计内存使用量将增加多少?最终目标:减少内存使用量,以便在较低的系统规格下运行。我不明白你的意思,但当抓取缩略图时,它从30k到200k。这可能更适合codereview,除非它导致内存不足异常…(或者更好,设计目标)?“。。。
        Bitmap bmp;

        string ext = Path.GetExtension(path);

        if (ext == ".exe")
        {

            Icon ico = Icon.ExtractAssociatedIcon(path);
            bmp = ico.ToBitmap();
            ico.Dispose();

        }
        else
        {

            if (imageType.Contains(ext.Replace(".", "")))
            {

                Image img = Image.FromFile(path);
                bmp = new Bitmap(img, new Size(d.Width - 20, 80));
                img.Dispose();
            }
            else
            {

                Icon ico = GetLargeIconForExtension(ext);
                bmp = ico.ToBitmap();
                ico.Dispose();

            }

        }