我正在使用内存流将 System.Drawing.Bitmap 写入 Azure 存储。我有正确的凭据,并且 azure 的所有内容都正确连接。我已使用输入流成功将图像上传到 blob,因此我认为这一定是我使用内存流对象的方式存在问题。
经过一段时间的研究,我的问题的一般解决方案似乎是将内存流位置设置为 0,但这对我来说不起作用,并且空文件仍然被保存到 azure 中。
我的代码是:
using (image)
{
System.IO.MemoryStream ms = new MemoryStream();
//create an encoder parameter for the image quality
EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
//get the jpeg codec
ImageCodecInfo imgCodec = ImageUtilities.GetEncoderInfo(CodecInfo);
//create a collection of all parameters that we will pass to the encoder
EncoderParameters encoderParams = new EncoderParameters(1);
//set the quality parameter for the codec
encoderParams.Param[0] = qualityParam;
//Move the pointer to the start of stream.
ms.Position = 0;
image.Save(ms, imgCodec, encoderParams);
blockBlob.UploadFromStream(ms);
}
最后保存的图像元素中有数据。在调试时保持正确的长度等,因此问题出在上传步骤中的某个地方
最佳答案
保存图像后和上传之前,您必须倒回内存流:
//...
//Move the pointer to the start of stream.
ms.Position = 0;
image.Save(ms, imgCodec, encoderParams);
//HERE !!!
ms.Position = 0;
blockBlob.UploadFromStream(ms);
}
关于c# - 从内存流上传到 azure 存储返回空文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27418053/