in ExampleGallery/Infrastructure/ThumbnailGenerator.xaml.cs [401:435]
static Rect CropCapturedBitmap(CanvasBitmap capturedBitmap, Color[] pixelColors)
{
Debug.Assert(capturedBitmap.Dpi == 96, "The following code mixes up DIPs and pixels in ways that are only valid if these units are the same.");
Rect rect = capturedBitmap.Bounds;
uint stride = capturedBitmap.SizeInPixels.Width;
// Crop transparent pixels from the left of the bitmap.
while (rect.Width > 1 && ArePixelsTransparent(pixelColors, stride, (int)rect.X, (int)rect.Y, 1, (int)rect.Height))
{
rect.X++;
rect.Width--;
}
// Crop transparent pixels from the top of the bitmap.
while (rect.Height > 1 && ArePixelsTransparent(pixelColors, stride, (int)rect.X, (int)rect.Y, (int)rect.Width, 1))
{
rect.Y++;
rect.Height--;
}
// Crop transparent pixels from the right of the bitmap.
while (rect.Width > 1 && ArePixelsTransparent(pixelColors, stride, (int)rect.X + (int)rect.Width - 1, (int)rect.Y, 1, (int)rect.Height))
{
rect.Width--;
}
// Crop transparent pixels from the bottom of the bitmap.
while (rect.Height > 1 && ArePixelsTransparent(pixelColors, stride, (int)rect.X, (int)rect.Y + (int)rect.Height - 1, (int)rect.Width, 1))
{
rect.Height--;
}
return rect;
}