in ExampleGallery/Infrastructure/AppIconGenerator.cs [148:224]
async Task GenerateIcon(AppInfo appInfo, IconInfo iconInfo, StorageFolder folder)
{
// Draw the icon image into a command list.
var commandList = new CanvasCommandList(device);
using (var ds = commandList.CreateDrawingSession())
{
appInfo.DrawIconImage(ds, iconInfo);
}
ICanvasImage iconImage = commandList;
// Rasterize into a rendertarget.
var renderTarget = new CanvasRenderTarget(device, iconInfo.Width, iconInfo.Height, 96);
using (var ds = renderTarget.CreateDrawingSession())
{
// Initialize with the appropriate background color.
ds.Clear(iconInfo.TransparentBackground ? Colors.Transparent : appInfo.BackgroundColor);
// Work out where to position the icon image.
var imageBounds = iconImage.GetBounds(ds);
imageBounds.Height *= 1 + iconInfo.BottomPadding;
float scaleUpTheSmallerIcons = Math.Max(1, 1 + (60f - iconInfo.Width) / 50f);
float imageScale = appInfo.ImageScale * scaleUpTheSmallerIcons;
var transform = Matrix3x2.CreateTranslation((float)-imageBounds.X, (float)-imageBounds.Y) *
Utils.GetDisplayTransform(renderTarget.Size.ToVector2(), new Vector2((float)imageBounds.Width, (float)imageBounds.Height)) *
Matrix3x2.CreateScale(imageScale, renderTarget.Size.ToVector2() / 2);
if (iconInfo.Monochrome)
{
// Optionally convert to monochrome.
iconImage = new DiscreteTransferEffect
{
Source = new Transform2DEffect
{
Source = new LuminanceToAlphaEffect { Source = iconImage },
TransformMatrix = transform
},
RedTable = new float[] { 1 },
GreenTable = new float[] { 1 },
BlueTable = new float[] { 1 },
AlphaTable = new float[] { 0, 1 }
};
}
else
{
ds.Transform = transform;
// Optional shadow effect.
if (appInfo.AddShadow)
{
var shadow = new ShadowEffect
{
Source = iconImage,
BlurAmount = 12,
};
ds.DrawImage(shadow);
}
}
// draw the main icon image.
ds.DrawImage(iconImage);
}
// Save to a file.
using (var stream = await folder.OpenStreamForWriteAsync(iconInfo.Filename, CreationCollisionOption.ReplaceExisting))
{
await renderTarget.SaveAsync(stream.AsRandomAccessStream(), CanvasBitmapFileFormat.Png);
}
}