简体   繁体   English

FileStream到位图-参数无效

[英]FileStream to Bitmap - Parameter is not valid

I have read the posts on this subject but none of them explains it to me clearly enough to be able to fix the problem. 我已经阅读了有关此主题的帖子,但是没有一个向我清楚地解释它,从而能够解决问题。

I am trying to upload a file from a local directory to the server. 我正在尝试将文件从本地目录上传到服务器。

Here is my code: 这是我的代码:

string fullPath = Path.Combine(
    AppDomain.CurrentDomain.BaseDirectory + @"Images\Readings", PhotoFileName);

Stream s = System.IO.File.OpenRead(fileUpload);

byte[] buffer = new byte[s.Length];

s.Read(buffer, 0, Convert.ToInt32(s.Length));

using (FileStream fs = new FileStream(fullPath, FileMode.Create))
{
    fs.Write(buffer, 0, Convert.ToInt32(fs.Length));

    Bitmap bmp = new Bitmap((Stream)fs);

    bmp.Save(fs, ImageFormat.Jpeg);
}

I keep on getting an Argument Exception: "Parameter is not valid" on line: 我不断收到一个参数异常:“参数无效”:

Bitmap bmp = new Bitmap((Stream)fs);

Can anyone explain this to me please 谁能向我解释一下

There are at least two problems, probably three. 至少有两个问题,可能是三个。 First, your copying code is broken: 首先,您的复制代码已损坏:

byte[] buffer = new byte[s.Length];
s.Read(buffer, 0, Convert.ToInt32(s.Length));

You've assumed that this will read all of the data in a single Read call, and ignored the return value for Read . 您已经假设这将在单个Read调用中读取所有数据,并且忽略了Read的返回值。 Generally, you'd need to loop round, reading data and writing it (the amount you've just read) to the output stream, until you read the end. 通常,您需要进行循环处理,读取数据并将其(已读取的数量)写入输出流,直到读取完为止。 However, as of .NET 4, Stream.CopyTo makes this much simpler. 但是,从.NET 4开始, Stream.CopyTo使这一过程变得更加简单。

Next is how you're creating the bitmap: 接下来是如何创建位图:

using (FileStream fs = new FileStream(fullPath, FileMode.Create))
{
    fs.Write(buffer, 0, Convert.ToInt32(fs.Length));
    Bitmap bmp = new Bitmap((Stream)fs);    
    bmp.Save(fs, ImageFormat.Jpeg);
}

You're trying to read from the stream when you've just written to it - but without "rewinding"... so there's no more data left to read. 刚写入流时,您正在尝试从流中读取数据 -但没有“倒带” ...,因此没有更多数据可读取。

Finally, I would strongly advise against using Bitmap.Save to write to the same stream that you're loading the bitmap from. 最后,我强烈建议您不要使用Bitmap.Save来写入从中加载位图的同一流。 Bitmap will keep a stream open, and read from it when it needs to - if you're trying to write to it at the same time, that could be very confusing. Bitmap将保持流打开,并在需要时从其读取-如果您尝试同时写入它,那可能会非常令人困惑。

It's not clear why you're using Bitmap at all, to be honest - if you're just trying to save the file that was uploaded, without any changes, just use: 坦白地说,目前还不清楚为什么要使用Bitmap -如果您只是想保存上传的文件,而没有任何更改,请使用:

using (Stream input = File.OpenRead(fileUpload),
              output = File.Create(fullPath))
{
    input.CopyTo(output);
}

This is assuming that fileUpload really is an appropriate filename - it's not clear why you haven't just written the file to the place you want to write it to straight away, to be honest. 这是假设fileUpload确实是一个适当的文件名-坦白说,不清楚为什么您不只是将文件写到立即写入的位置。 Or use File.Copy to copy the file. 或使用File.Copy复制文件。 The above code should work with any stream, so you can change it to save the stream straight from the request... 上面的代码适用于任何流,因此您可以对其进行更改以直接从请求中保存流...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM