private async static Task DarkenImageBottom(string filename, string outfilename)
{
var file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(filename);
BitmapDecoder decoder = null;
byte[] sourcePixels = null;
using (IRandomAccessStream fileStream = await file.OpenReadAsync())
{
decoder = await BitmapDecoder.CreateAsync(fileStream);
// Scale image to appropriate size
BitmapTransform transform = new BitmapTransform();
PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
BitmapPixelFormat.Bgra8, // WriteableBitmap uses BGRA format
BitmapAlphaMode.Straight,
transform,
ExifOrientationMode.IgnoreExifOrientation, // This sample ignores Exif orientation
ColorManagementMode.DoNotColorManage
);
// An array containing the decoded image data, which could be modified before being displayed
sourcePixels = pixelData.DetachPixelData();
fileStream.Dispose();
}
for (uint col = 0; col < decoder.PixelWidth; col++)
{
for (uint row = 0; row < decoder.PixelHeight; row++)
{
if (row < decoder.PixelHeight * .6) continue;
uint idx = (row * decoder.PixelWidth + col) * 4;
if (decoder.BitmapPixelFormat == BitmapPixelFormat.Bgra8 ||
decoder.BitmapPixelFormat == BitmapPixelFormat.Rgba8)
{
var frac = 1 - Math.Sin(((row / (double)decoder.PixelHeight) - .6) * (1 / .4));
byte b = sourcePixels[idx];
byte g = sourcePixels[idx + 1];
byte r = sourcePixels[idx + 2];
sourcePixels[idx] = (byte)(b * frac);
sourcePixels[idx + 1] = (byte)(g * frac);
sourcePixels[idx + 2] = (byte)(r * frac);
}
}
}
var file2 = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(outfilename, CreationCollisionOption.ReplaceExisting);
var str = await file2.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
BitmapEncoder enc = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, str);
enc.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, decoder.PixelWidth, decoder.PixelHeight,
decoder.DpiX, decoder.DpiY, sourcePixels);
await enc.FlushAsync();
str.Dispose();
}
}