将位图保存到内存流中时,可以将位图转换为jpeg吗? [英] Can I convert a bitmap to a jpeg when saving the bitmap to a memory stream?

查看:108
本文介绍了将位图保存到内存流中时,可以将位图转换为jpeg吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将位图文件另存为jpeg到Azure中,而没有在过程中本地保存,所以我首先将位图另存为jpeg到MemoryStream中.

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.'

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

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

推荐答案

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

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)

请客气,请倒带."

这篇关于将位图保存到内存流中时,可以将位图转换为jpeg吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆