C# 将图片文件保存到 RAM

本文关键字:保存 RAM 文件 | 更新日期: 2023-09-27 17:56:32

我正在开发一个用于图像处理的程序,我需要从视频中保存一些图片并对它们进行一些处理。在处理 1 张图片时,它并不需要时间,但是当我处理 100 张图片时,它会有所不同我正在将文件保存到硬盘,这就是为什么需要时间的原因问题是,我正在使用的函数是一个现成的函数,它只接受(文件名)该函数真的很复杂,所以我无法构建自己的函数(如果这就是你的想法)

我现在正在考虑两件事,并希望您对它们发表意见:

  1. 更改函数的输入,但是如何更改? 有没有办法将此输入从(文件名)更改为保存这些图片的数组?
  2. 将文件保存到 RAM。但是如何按名称将文件保存到RAM,并能够在函数中将它们用作(文件名)?

感谢您的帮助,非常感谢

这是我的代码,但我仍然有问题:

            Capture video = new Capture("c:''5.avi");
            Image<Bgr, Byte> Imageframe ;
            Dictionary<string, MemoryStream> dict = new Dictionary<string, MemoryStream>();
                Imageframe = video.QueryFrame();
                Bitmap bmp = Imageframe.ToBitmap();
                dict.Add("mypicture.png", new MemoryStream());
                bmp.Save(dict["mypicture.png"],imageformat.png);    

它说图像格式在上下文中不存在

这是IM使用的函数:

Image<Bgr, byte> result;
                 result = DrawMatches.Draw("box.png", "box_in_scene.png", out matchTime,i); 

C# 将图片文件保存到 RAM

您可以使用

MemoryStream保存到RAM(在最松散的意义上)。如果你想命名它们,你可以使用Dictionary<string,MemoryStream>,例如:

Dictionary<string,MemoryStream> dict = new Dictionary<string,MemoryStream>();
dict.Add("mypicture.png",new MemoryStream());
image.Save(dict["mypicture.png"]);

但是,您需要为这些流编写清理代码,这并没有考虑到最终您可能会用完所有物理 RAM,然后开始进入分页文件,无论如何,分页文件都是基于磁盘的。

顺便说一句,如果这是在您控制的服务器上,并且您没有向最终用户发布此软件,则可以获得一个RAM磁盘驱动器(仅Google周围很多),该驱动器使用物理RAM作为Windows可用的实际磁盘。然后你可以加载/保存到那个。

非常

非常粗糙的EmguCv变体:

// Global dictionary of memory streams
Dictionary<string,MemoryStream> dict = new Dictionary<string,MemoryStream>();
// Add image memory stream to dictionary
dict.Add("mypicture.png",new MemoryStream());
// Get bitmap from EmguCv
Bitmap bmp = image.ToBitmap();
// Save bitmap to image memory stream
bmp.Save(dict["mypicture.png"],ImageFormat.Png);
// Get bitmap from memory stream
dict["mypicture.png"].Seek(0,SeekOrigin.Begin);
Bitmap new_bmp = Image.FromStream(dict["mypicture.png"]);
// Convert bitmap to EmguCv image
Image<Bgr,Byte> new_img = new Image<Bgr,Byte>(new_bmp);

尝试使用 MemoryStream 来处理记忆,就像处理文件一样,并尝试按线程将文件保存在硬盘上

我知道

您正在使用没有源代码的DLL。您也许能够将其加载到反射器中,并对其进行修改以将图像作为参数而不是文件名。我过去使用过Red Gate的反光板,它对我有用:http://www.reflector.net/