Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.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#_Memorystream - Fatal编程技术网

访问内存流中的文件?c#

访问内存流中的文件?c#,c#,memorystream,C#,Memorystream,我正在使用此代码将文件保存到内存流 using System.Drawing.Imaging; Dictionary<string,MemoryStream> dict = new Dictionary<string,MemoryStream>(); dict.Add("mypicture.png",new MemoryStream()); bmp.Save(dict["mypicture.png"], ImageFormat.Bmp); 我是否正确地从内存流访问

我正在使用此代码将文件保存到内存流

using System.Drawing.Imaging;

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

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

bmp.Save(dict["mypicture.png"], ImageFormat.Bmp);
我是否正确地从内存流访问文件? 在函数中,它表示

无效参数

我认为我没有从内存流以正确的方式访问文件

using System.Drawing.Imaging;

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

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

bmp.Save(dict["mypicture.png"], ImageFormat.Bmp);
这是Drawmatches。Draw:

public static Image<Bgr, Byte> Draw(String modelImageFileName, String observedImageFileName, out long matchTime,int i)
{
   Image<Gray, Byte> modelImage = new Image<Gray, byte>(modelImageFileName);
   Image<Gray, Byte> observedImage = new Image<Gray, byte>(observedImageFileName);
}
公共静态图像绘制(字符串modelImageFileName、字符串observedImageFileName、out long matchTime、int i)
{
图像模型图像=新图像(模型图像文件名);
图像observedImage=新图像(observedImageFileName);
}
错误不在编译中,而是在运行时发生的


它表示“参数无效”,并指向ObserveImage

我不确定Image类是什么以及它是如何实现的,但您正在将一个
字符串
传递给它的构造函数-您确定这是正确的吗?(EDIT:刚刚发现它是实现的一部分)。我将向您展示通常如何从流创建图像:

MemoryStream ms = dict["mypicture.png"]; // This gives you the memory stream from the dictionary
ms.Seek(0, SeekOrigin.Begin); // Go to the beginning of the stream
Bitmap bmp = new Bitmap(ms); // Create image from the stream
现在,我怀疑您想使用以下构造函数从您拥有的流创建一个新的“emgu映像”:

Image<Bgr, Byte> img1 = new Image<Bgr, Byte>("MyImage.jpg");
编辑
在下面的评论中,您表示希望通过将位图保存到内存流而不是写入磁盘来减少处理时间

我需要问你以下几个问题:你为什么要这么做?我从您上面发布的代码中注意到,显然您已经有了一个
位图
,并将其保存到内存流中。你为什么不把位图本身放到字典里呢

Dictionary<string, Bitmap> dict = new Dictionary<string, Bitmap>();
dict.Add("mypicture.png", bmp);

请发布实现
DrawMatches.Draw
方法的代码,以及您收到的确切错误消息。还要注意它是编译时错误还是运行时错误。另外,我注意到您实际上并没有将图形保存到文件中。你打算这么做吗?你的意图是有点迷惑…你从来没有把文件保存到磁盘,所以如果你需要在自己的函数中使用图像,考虑直接接受<代码>图像< /C>对象或包含图像的字节数组。尝试使用文件名是行不通的。这与此有关,我记得他问过-我把它保存给了ram,我知道,非常感谢thorsten,但你知道,我的函数只接受文件名,我想直接从内存流调用该文件我不想将其变成文件bcoz我正在进行图像处理,将其转换为文件并保存到磁盘将花费我不想花费的时间,因此如何直接从ram调用?通过文件名,请问我如何与您交谈?您不能“直接从内存流调用文件”。文件是文件,内存流是计算机内存中的一个区域。如果函数只接受硬盘上的文件名,则需要在硬盘上创建文件。此外,我还向您展示了可用于从内存流创建EMGU图像的代码。这不管用吗?事实上,如果没有内存流,我也可以这样做。我会保存到磁盘,然后将文件输入函数,但在处理100个图像时,将100个图像保存到磁盘需要花费很多时间,将100个图像保存到ram所需的时间更少(我想是吧?)这就是为什么我尝试使用内存流的原因我的目标是减少处理时间,保存100张图像需要很多时间,时间在我的应用程序中很重要我如何解决这个问题?我已经编辑了我的答案。事实是:你所要求的不能用你实现它的方式来完成。时期
图像
的构造函数需要一个
位图
或图像文件的文件名-一个文件名,而不是字典中流的字符串键。构造函数应该如何知道它应该访问您的字典(顺便说一句,它不能访问字典)并获取流?
private Bitmap GetBitmapFromStream(String bitmapKeyName)
{
    MemoryStream ms = dict[bitmapKeyName];
    ms.Seek(0, SeekOrigin.Begin);
    return new Bitmap(ms);
}

public Image<Bgr, Byte> Draw(String modelImageFileName, String observedImageFileName, out long matchTime,int i)
{
   Image<Gray, Byte> modelImage = new Image<Gray, byte>(GetBitmapFromStream(modelImageFileName));
   Image<Gray, Byte> observedImage = new Image<Gray, byte>(GetBitmapFromStream(observedImageFileName));
}
Dictionary<string, Bitmap> dict = new Dictionary<string, Bitmap>();
dict.Add("mypicture.png", bmp);
public Image<Bgr, Byte> Draw(String modelImageFileName, String observedImageFileName, out long matchTime,int i)
{
   Image<Gray, Byte> modelImage = new Image<Gray, byte>(dict[modelImageFileName);
   Image<Gray, Byte> observedImage = new Image<Gray, byte>(dict[observedImageFileName]);
}