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

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

我正在尝试将位图文件作为 jpeg 文件保存到 Azure 中,而不是在此过程中将其保存在本地,因此我首先将位图文件作为 jpeg 文件保存在 MemoryStream 中。

但是当我执行下面的代码时,文件上传了但是没有正确转换位图。如果我查看文件,查看器会显示 'Invalid Image.'

我在某处读到位图无法在内存中转换为 jpeg。这可能是这里发生的事情吗?

        // 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);

写入 MemoryStream 后,您需要通过设置 memStr.Position = 0 来 "rewind" 它,然后再尝试读取它(在您的情况下,将其上传到 Azure)

"Be kind, please rewind."