c# save a picture file to ram

浪子不回头ぞ 提交于 2019-12-11 08:15:51

问题


I am developing a program for image processing, and I need to save some pictures from a video and do some processing on them. When dealing with 1 picture it doesn't really take time, But when I'm dealing with 100 pictures it makes difference I'm saving the files to my hard disk, and that's why it takes time the thing is, the function I'm using is a ready made function and it only accepts (file name) the function is really complicated so i cannot build my own function ( if that's what you are thinking )

I'm thinking of 2 things right now and would like to have your opinions about them:

  1. change the input of the function, but how ? is there a way to change this input from a ( file name ) to an array which holds these pictures ?
  2. save the file to ram. but how to save files to ram by names, and be able to use them as ( file name ) in the function ?

I appreciate your help , thanks so much

this is my code but i still have problems:

            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);    

its saying imageformat does not exist in the context

and this is the function im using :

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

回答1:


You could save to RAM (in the loosest of senses) using a MemoryStream. If you wanted to name them you could use a Dictionary<string,MemoryStream>, such as:

Dictionary<string,MemoryStream> dict = new Dictionary<string,MemoryStream>();

dict.Add("mypicture.png",new MemoryStream());

image.Save(dict["mypicture.png"]);

However you'll need to write cleanup code for these streams and this doesn't take into account that eventually you could use up all the physical RAM and then start going into the paging file, which is disk based anyhow.

As an aside, if this is on a server you control and you're not releasing this software to end users you could get a RAM disk drive (plenty around just Google) that uses physical RAM as an actual disk available to Windows. Then you could just load/save to that.

Very very rough EmguCv variant:

// 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);



回答2:


Try to use MemoryStream for work with memmory like with file, and try to save files on harddisk by thread




回答3:


I understand you are using a DLL you dont have the source for. You might be able to load it into a reflector, and modify it to take the image as an argument instead of the file name. I used Red Gate's reflector in the past and it worked for me: http://www.reflector.net/



来源:https://stackoverflow.com/questions/14562094/c-sharp-save-a-picture-file-to-ram

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!