从memorystream上传到azure存储,返回一个空文件 [英] uploading to azure storage from memorystream returning an empty file
问题描述
我正在使用内存流将System.Drawing.Bitmap写入Azure存储.我具有正确的凭据,并且所有天蓝色的面都正确连接.我已经使用输入流成功地将图像上传到Blob,所以我认为如何使用memorystream对象一定是一个问题.
I'm writing a System.Drawing.Bitmap to Azure storage using a memory stream. I have the correct credentials and everything azure side wires up correctly. I have uploaded an image into a blob successfully using an input stream so I think it must be a problem with how i am using the memorystream object.
经过一会儿,我的问题的一般解决方案似乎是将memorystream位置设置为0,但是这对我没有用,并且一个空文件仍被保存为天蓝色.
After looking about for a while, the general solution to my problem looked to be setting the memorystream position to 0, that hasn't worked for me however and an empty file is still being saved into 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);
}
最后保存的图像元素中包含数据.在调试时保持正确的长度等,因此问题出在上传步骤中
The image element that is saved at the end has data in it. at debug is holds the correct length etc. so the problem is somewhere in the uploading step
推荐答案
在保存图像之后并上传之前,您必须回退内存流:
You have to rewind your memory Stream after saving the image and before uploading :
//...
//Move the pointer to the start of stream.
ms.Position = 0;
image.Save(ms, imgCodec, encoderParams);
//HERE !!!
ms.Position = 0;
blockBlob.UploadFromStream(ms);
}
这篇关于从memorystream上传到azure存储,返回一个空文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!