简体   繁体   English

将位图保存到内存流时,可以将位图转换为jpeg吗?

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

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. 我试图将位图文件另存为jpeg到Azure中,而不在过程中本地保存,因此我首先将位图另存为jpeg到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. 我读到某处位图无法在内存中转换为jpeg。 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);

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) 写入MemoryStream后,您需要通过在尝试读取任何内容之前将memStr.Position = 0设置为“快退”(在您的情况下,将其上传到Azure)

"Be kind, please rewind." “请客气,请倒带。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM