[WP8] Redimensionar con PhotoChooserTask

Hay 2 formas de redimensionar una imagen, ya sea usando el crop pre-construido o haciéndolo por programación. A continuación las 2 formas en que se puede hacer:

Primera

Programándolo como buen Geek.

            PhotoChooserTask photoChooserTask = new PhotoChooserTask();
photoChooserTask.ShowCamera = true;
photoChooserTask.Completed += (s, a) =>
{
if (null == a.Error && a.TaskResult == TaskResult.OK)
{
if (a.ChosenPhoto.Length != 0)
{
try
{
BitmapImage img = new BitmapImage();
img.SetSource(a.ChosenPhoto);
WriteableBitmap writeableBmp = new WriteableBitmap(img);

int targetHeight = writeableBmp.PixelHeight;
int targetWidth = writeableBmp.PixelWidth;

while (true)
{
if (180 > targetHeight * 0.9 || 180 > targetWidth * 0.9)
break;

targetHeight = (int)(targetHeight * 0.9);
targetWidth = (int)(targetWidth * 0.9);
}

MemoryStream ms = new MemoryStream();
writeableBmp.SaveJpeg(ms, targetWidth, targetHeight, 0, 100);
ms.Seek(0, SeekOrigin.Begin);

//Ya esta todo en el MemoryStream
}
catch (Exception)
{
throw new ArgumentException("La foto que ha elegido no se puede redimensionar, por favor eliga otra.");
}
}
else
{
throw new ArgumentException("La foto que ha elegido no se puede redimensionar, por favor eliga otra.");
}
}
};
photoChooserTask.Show();

Segunda

PhotoChooserTask, lo hacemos juntos.

            PhotoChooserTask photoChooserTask = new PhotoChooserTask();
photoChooserTask.PixelHeight = 180;
photoChooserTask.PixelWidth = 180;
photoChooserTask.ShowCamera = true;
photoChooserTask.Completed += (s, a) =>
{
if (null == a.Error && a.TaskResult == TaskResult.OK)
{
if (a.ChosenPhoto.Length != 0)
{
try
{
//Ya esta todo en el a.ChosenFoto;
}
catch (Exception)
{
throw new ArgumentException("La foto que ha elegido no se puede redimensionar, por favor eliga otra.");
}
}
else
{
throw new ArgumentException("La foto que ha elegido no se puede redimensionar, por favor eliga otra.");
}
}
};
photoChooserTask.Show();

Agregue un comentario

Su dirección de correo no se hará público. Los campos requeridos están marcados *