我正在创建一个项目,我想在其中压缩图像,以便可以轻松地将其上传到windows azure上,之后可以从windows azure轻松检索到我的应用程序。所以你能帮我解决一下我该怎么办。我现在正在使用BitmapImage。 Follwoing是我用来将图像上传到azure的代码
void photoChooserTask_Completed(对象发送者,PhotoResult e) {
if (e.TaskResult == TaskResult.OK)
{
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(e.ChosenPhoto);
WriteableBitmap wb = new WriteableBitmap(bitmap);
using (MemoryStream stream = new MemoryStream())
{
wb.SaveJpeg(stream, wb.PixelWidth, wb.PixelHeight, 0, 0);
AzureStorage storage = new AzureStorage();
storage.Account = **azure account**;
storage.BlobEndPoint = **azure end point**;
storage.Key = **azure key**;
string fileName = uid;
bool error = false;
if (!error)
{
storage.PutBlob("workerimages", fileName, imageBytes, error);
}
else
{
MessageBox.Show("Error uploading the new image.");
}
}
}
}
答案 0 :(得分:1)
请注意使用WriteableBitmap,因为如果调整大量图像,可能会耗尽内存。如果只有几个,则将要保存的大小传递给SaveJpeg方法。另外,请确保使用高于0的值作为质量(SaveJpeg的最后一个参数)
var width = wb.PixelWidth/4;
var height = wb.PixelHeight/4;
using (MemoryStream stream = new MemoryStream())
{
wb.SaveJpeg(stream, width, height, 0, 100);
...
...
}
您还可以使用JpegRenderer from the Nokia Imaging SDK调整图片大小。
var width = wb.PixelWidth/4;
var height = wb.PixelHeight/4;
using (var imageProvider = new StreamImageSource(e.ChosenPhoto))
{
IFilterEffect effect = new FilterEffect(imageProvider);
// Get the resize dimensions
Windows.Foundation.Size desiredSize = new Windows.Foundation.Size(width, height);
using (var renderer = new JpegRenderer(effect))
{
renderer.OutputOption = OutputOption.PreserveAspectRatio;
// set the new size of the image
renderer.Size = desiredSize;
IBuffer buffer = await renderer.RenderAsync();
return buffer;
}
}