Can I convert a bitmap to a jpeg when saving the bitmap to a memory stream?

拟墨画扇 提交于 2020-01-24 21:27:05

问题


I'm attempting to save a bitmap file as a jpeg into Azure without saving it locally in the process, so I am first saving the bitmap as a jpeg in a MemoryStream.

But when I execute the following code, the file uploads but it did not convert the bitmap correctly. If I view the file, the viewer displays 'Invalid Image.'

I read somewhere that bitmaps cannot be converted to jpegs in memory. Could that be what is happening here?

        // Retrieve reference to a blob
        var blobContainer = GetBlobContainer(Properties.Settings.Default.BlobContainerName);
        var blob = blobContainer.GetBlockBlobReference(blobFilePath);

        // Save bitmap to jpeg in MemoryStream, then upload to Azure blob
        //var writer = new StreamWriter(blob.OpenWrite());
        MemoryStream memStr = new MemoryStream();
        bitmap.Save(memStr, System.Drawing.Imaging.ImageFormat.Jpeg);
        blob.UploadFromStream(memStr);

回答1:


After writing to the MemoryStream you need to "rewind" it by setting memStr.Position = 0 before any attempts to read it (in your case, uploading it to Azure)

"Be kind, please rewind."



来源:https://stackoverflow.com/questions/52211782/can-i-convert-a-bitmap-to-a-jpeg-when-saving-the-bitmap-to-a-memory-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!