Reputation: 7424
I'm using the WP7 Camera sample and I had a question when capturing the image. Right now It takes a picture using the standard 480(w) x 800(h). What I'm trying to do is take the picture as a square as my app requires the image to be a perfect square.
I adjusted the VideoBrush to show only 480x480, and the image that is originally being taken appears to have taken a square picture, but when you check inside the Pictures hub its an ordinary 480x800 portrait.
Does anyone know how to set the camera to take a square picture or maybe crop the top and bottom?
Upvotes: 2
Views: 1190
Reputation: 2891
You need to manually copy the pixels to a new bitmap. So if the camera is horizontal and you want the left part of the image cropped so that the new width equals the height, then something like this would work (I did not test this code, but even if it is not 100% correct, it should give you the basic idea):
WriteableBitmap SquareImage(WriteableBitmap srcBitmap)
{
int[] srcData = srcBitmap.Pixels;
int[] destData = new int[srcBitmap.PixelHeight * srcBitmap.PixelHeight];
for (int row = 0; row < srcBitmap.PixelHeight; ++row)
{
for (int col = 0; col < srcBitmap.PixelHeight; ++col)
{
destData[(row * srcBitmap.PixelHeight) + col] = srcData[(row * srcBitmap.PixelWidth) + col];
}
}
WriteableBitmap squareBitmap = new WriteableBitmap(srcBitmap.PixelHeight, srcBitmap.PixelHeight);
destData.CopyTo(squareBitmap.Pixels, 0);
return squareBitmap;
}
Upvotes: 4