C# 将图片文件保存到ram中

C# 将图片文件保存到ram中,c#,file,image,ram,C#,File,Image,Ram,我正在开发一个图像处理程序,我需要从视频中保存一些图片并对其进行处理。 当处理一张图片时,它并不真正需要时间, 但当我处理100张图片时,情况就不同了 我正在把文件保存到硬盘上,这就是为什么要花时间 问题是,我使用的函数是一个现成的函数,它只接受(文件名) 函数非常复杂,因此我无法构建自己的函数(如果您是这么想的话) 我现在想到两件事,想听听你的意见: 更改函数的输入,但如何更改?有没有办法将此输入从(文件名)更改为保存这些图片的数组 将文件保存到ram。但是如何按名称将文件保存到ram中,并

我正在开发一个图像处理程序,我需要从视频中保存一些图片并对其进行处理。 当处理一张图片时,它并不真正需要时间, 但当我处理100张图片时,情况就不同了 我正在把文件保存到硬盘上,这就是为什么要花时间 问题是,我使用的函数是一个现成的函数,它只接受(文件名) 函数非常复杂,因此我无法构建自己的函数(如果您是这么想的话)

我现在想到两件事,想听听你的意见:

  • 更改函数的输入,但如何更改?有没有办法将此输入从(文件名)更改为保存这些图片的数组
  • 将文件保存到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);    
    
    Capture video=新捕获(“c:\\5.avi”);
    图像框;
    Dictionary dict=新字典();
    Imageframe=video.QueryFrame();
    位图bmp=Imageframe.ToBitmap();
    dict.Add(“mypicture.png”,newmemoryStream());
    保存(dict[“mypicture.png”]、imageformat.png);
    
    上下文中不存在其格式

    这是我使用的函数:

    Image<Bgr, byte> result;
                     result = DrawMatches.Draw("box.png", "box_in_scene.png", out matchTime,i); 
    
    图像结果;
    结果=DrawMatches.Draw(“box.png”,“box\u in\u scene.png”,out matchTime,i);
    
    您可以使用
    内存流
    将数据保存到RAM中(在最松散的意义上)。如果您想给它们命名,可以使用
    字典
    ,例如:

    Dictionary<string,MemoryStream> dict = new Dictionary<string,MemoryStream>();
    
    dict.Add("mypicture.png",new MemoryStream());
    
    image.Save(dict["mypicture.png"]);
    
    Dictionary dict=new Dictionary();
    dict.Add(“mypicture.png”,newmemoryStream());
    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);
    
    //内存流全局字典
    Dictionary dict=新字典();
    //将图像内存流添加到字典
    dict.Add(“mypicture.png”,newmemoryStream());
    //从EmguCv获取位图
    位图bmp=image.ToBitmap();
    //将位图保存到图像内存流
    保存(dict[“mypicture.png”]、ImageFormat.png);
    //从内存流获取位图
    dict[“mypicture.png”].Seek(0,SeekOrigin.Begin);
    位图new_bmp=Image.FromStream(dict[“mypicture.png]”);
    //将位图转换为EmguCv图像
    Image new\u img=新图像(new\u bmp);
    
    尝试使用memmory,就像使用file一样,
    并尝试通过

    将文件保存到硬盘上。据我所知,您使用的是一个没有源代码的DLL。 您可以将其加载到反射器中,并对其进行修改,以将图像作为参数而不是文件名。
    我以前用过红门的反射器,它对我有用:

    你能改变功能吗?(给我们看一下你的代码)我可能错了,但在应用程序结束时,内存流的清理不是用垃圾收集器完成的吗?@Tom Yeh你是对的,因为它们被标记为IDisposable,但我认为依赖它是不好的:)看这里,阅读Jon Skeet的答案我非常抱歉,我没有提供我的代码。谢谢劳埃德的快速回复,但我又遇到了一个问题。这是我的代码:图像框架;Dictionary dict=新字典();Imageframe=video.QueryFrame();dict.Add(“mypicture.png”,newmemoryStream());Imageframe.Save(dict[“mypicture.png]”);问题是,Imageframe是字节类型的,当我执行最后一行代码(重载)时,它会给我一个错误,我真的不知道该做什么,我通知您的帮助Image有哪些方法(我假设这不是标准的Image类)?