我正在使用内存流将 System.Drawing.Bitmap 写入 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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)