如何在 Windows Phone 上压缩图像

发布于 2025-01-05 23:25:56 字数 1761 浏览 0 评论 0原文

我的应用程序使用相机拍摄图像并将其上传到 flickr。我想压缩图像,以便上传时间不会像现在那么长。我尝试了 BitmapSource 和 WriteableBitmap 的“SaveJpeg”方法来完成此任务,但失败了。位图源在 Silverlight/WP 中没有与完整 .NET Framework 版本中相同的可用成员,并且 WriteableBitmap 的 SaveJpeg 方法一直给我一个“此流不支持写入”错误。

这就是我当前在 CameraCaptureTask 已完成事件处理程序中所做的事情:

private void CameraCaptureCompleted(object sender, PhotoResult e)
    {
        if (e == null || e.TaskResult != TaskResult.OK)
        {
            return;
        }                                                             
        BitmapImage bitmap = new BitmapImage {CreateOptions = BitmapCreateOptions.None};                        
        bitmap.SetSource(AppHelper.LoadImage(e.ChosenPhoto));
        WriteableBitmap writeableBitmap = new WriteableBitmap(bitmap);

        // Encode the WriteableBitmap object to a JPEG stream.
        writeableBitmap.SaveJpeg(e.ChosenPhoto, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 85);
    }

此代码给了我一个:“Stream 不支持写入”错误。

是否有其他方法可以压缩图像或者我必须编写压缩算法?

更新已修复!

private void CameraCaptureCompleted(object sender, PhotoResult e)
    {
        if (e == null || e.TaskResult != TaskResult.OK)
        {
            return;
        }                                                             
        BitmapImage bitmap = new BitmapImage {CreateOptions = BitmapCreateOptions.None};                        
        bitmap.SetSource(AppHelper.LoadImage(e.ChosenPhoto));
        WriteableBitmap writeableBitmap = new WriteableBitmap(bitmap);

        // Encode the WriteableBitmap object to a JPEG stream.
        writeableBitmap.SaveJpeg(new MemoryStream(), writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 85);
    }

我试图写入源流。哎哟!

谢谢。

My app uses the camera to take an image and upload it to flickr. I'd like to compress the image down so that the upload doesn't take as long as it does currently. I tried both the BitmapSource and the WriteableBitmap's 'SaveJpeg' method to accomplish this but failed. Bitmap source doesn't have the same members available in Silverlight/WP as it does in the full .NET framework version and the SaveJpeg method the WriteableBitmap has kept giving me an 'This stream does not support writing to it' error.

This is what I am currently doing in my CameraCaptureTask completed event handler:

private void CameraCaptureCompleted(object sender, PhotoResult e)
    {
        if (e == null || e.TaskResult != TaskResult.OK)
        {
            return;
        }                                                             
        BitmapImage bitmap = new BitmapImage {CreateOptions = BitmapCreateOptions.None};                        
        bitmap.SetSource(AppHelper.LoadImage(e.ChosenPhoto));
        WriteableBitmap writeableBitmap = new WriteableBitmap(bitmap);

        // Encode the WriteableBitmap object to a JPEG stream.
        writeableBitmap.SaveJpeg(e.ChosenPhoto, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 85);
    }

This code gives me a : "Stream does not support writing" error.

Is there some other way I could compress an image or would I have to write a compression algorithm?

UPDATE FIXED!!

private void CameraCaptureCompleted(object sender, PhotoResult e)
    {
        if (e == null || e.TaskResult != TaskResult.OK)
        {
            return;
        }                                                             
        BitmapImage bitmap = new BitmapImage {CreateOptions = BitmapCreateOptions.None};                        
        bitmap.SetSource(AppHelper.LoadImage(e.ChosenPhoto));
        WriteableBitmap writeableBitmap = new WriteableBitmap(bitmap);

        // Encode the WriteableBitmap object to a JPEG stream.
        writeableBitmap.SaveJpeg(new MemoryStream(), writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 85);
    }

I was trying to write to the source stream. Doh!

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独患者 2025-01-12 23:25:56

我认为 SaveJpeg 是我这样做的方式。你可能可以用其他方法来做,但我认为这是最简单、最自然的。错误“此流不支持写入”可能是因为无论您传递给 SaveJpeg 的流是什么,它都是不可写的。我不太确定您要写入的内容,请尝试仅使用普通的旧内存流,看看它是否像这样工作

using System.IO;

// ...

MemoryStream ms = new MemoryStream();
pic.SaveJpeg(ms, pic.PixelWidth, pic.PixelHeight, 0, 0, 50);

您可以调整最终参数中的质量。 PixelWidth/Height 来自 WriteableBitmap 因此,如果您有其他来源,您可能必须使用其他方法/属性来获取宽度/高度。您可能需要缩放它们,因为相机拍摄的图片可能非常大。这取决于您上传这些图片的目的,但缩放它们也可以使文件大小更小。

SaveJpeg is the way I would do this I think. You could probably do it some other ways but I think that would be the easiest and most natural. The error 'This stream does not support writing to it' is likely because whatever stream you are passing to SaveJpeg it isn't writable. I'm not exactly sure what you are trying to write to, try using just a plain old memory stream and see if that works like such

using System.IO;

// ...

MemoryStream ms = new MemoryStream();
pic.SaveJpeg(ms, pic.PixelWidth, pic.PixelHeight, 0, 0, 50);

You can adjust the quality in the final parameter. PixelWidth/Height is from WriteableBitmap so if you have some other source you might have to use another method/property to get the width/height. You might want to scale these since the pictures from the camera can be pretty big. It depends on what you're uploading these pictures for but scaling them you can make the file size smaller as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文