如何在 Windows Phone 上压缩图像
我的应用程序使用相机拍摄图像并将其上传到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 SaveJpeg 是我这样做的方式。你可能可以用其他方法来做,但我认为这是最简单、最自然的。错误“此流不支持写入”可能是因为无论您传递给 SaveJpeg 的流是什么,它都是不可写的。我不太确定您要写入的内容,请尝试仅使用普通的旧内存流,看看它是否像这样工作
您可以调整最终参数中的质量。 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
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.