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.
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.
my code is:
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
Upload contents of a folder to Data Box Blob storage To get your account key, in the Azure portal, go to your storage account. Go to Settings > Access keys, select a key, and paste it into the AzCopy command. If the specified destination container does not exist, AzCopy creates it and uploads the file into it.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With