[英]C# Bitmap Invalid Parameter
Here is what I am trying to do: Take a folder full of images, perform an optimization on them, and store them in the same file. 这是我要尝试的操作:取一个装满图像的文件夹,对它们进行优化,然后将它们存储在同一文件中。
I get the error: 我收到错误:
System.ArgumentException
Message = Parameter is not valid.
From line: 从行:
using (var bitmap = new Bitmap(image))
I think this has something to do with the file being open, and blocking access but I'm not sure. 我认为这与打开文件和阻止访问有关,但是我不确定。 I know for a fact that the filepath I'm using is correct, and that it is a folder filled with images.
我知道一个事实,我使用的文件路径是正确的,并且它是一个装有图像的文件夹。 Can anyone help me?
谁能帮我?
string[] folder = Directory.GetFiles(GetSourceDirectory());
Parallel.ForEach(folder, (file) =>
{
using (var fileStream = File.Open(file, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
var image = fileStream.Name;
Console.WriteLine(image);
Console.ReadLine();
using (var bitmap = new Bitmap(image))
{
using (var quantized = quantizer.QuantizeImage(bitmap, 1, 1))
{
var blob = container.GetBlockBlobReference(fileStream.Name);
try
{
quantized.Save(fileStream, ImageFormat.Png);
}
catch (ArgumentException e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
Console.WriteLine();
throw;
}
blob.UploadFromStreamAsync(fileStream).Wait();
}
}
}
});
This is because you are using the filestream to read and write at the same time. 这是因为您正在使用文件流同时读取和写入。
If you change the File.Open to 如果您将File.Open更改为
using (var fileStream = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) 使用(var fileStream = File.Open(file,FileMode.Open,FileAccess.Read,FileShare.ReadWrite))
it will allow you to do 它会让你做
using (var bitmap = new Bitmap(image)) 使用(var bitmap = new Bitmap(image))
But then obviously you will come unstuck later when you try to write using the fileStream variable. 但是很显然,当您尝试使用fileStream变量进行编写时,您稍后会陷入僵局。
So I think you will have to change it so you don't have the nested using statements. 因此,我认为您将不得不对其进行更改,以便您不必使用嵌套的using语句。
FileStream.Name仅提供文件名,例如“ image.jpg”,您是否尝试过使用(var bitmap = new Bitmap(fileStream)) ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.