Using Canvas.DrawBitmap
you can draw a Bitmap
on top of another mutable Bitmap
. Bitmap.CompressAsync
provides an overload that allows saving to stream (a FileStream in this case).
var filename = System.IO.Path.Combine(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads).ToString(), "filename.png");
Bitmap newBitmap;
using (var aBitmapToApplyWaterMarkTo = await BitmapFactory.DecodeResourceAsync(Resources, Resource.Drawable.Alexina))
using (var waterMarkBitmap = await BitmapFactory.DecodeResourceAsync(Resources, Resource.Drawable.watermark))
{
newBitmap = aBitmapToApplyWaterMarkTo.Copy(aBitmapToApplyWaterMarkTo.GetConfig(), true);
using (var canvas = new Canvas(newBitmap))
{
canvas.DrawBitmap(waterMarkBitmap, newBitmap.Width - 100, newBitmap.Height - 100, null);
}
}
using (var fileStream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write))
{
await newBitmap.CompressAsync(Bitmap.CompressFormat.Png, 100, fileStream);
}
newBitmap.Dispose();
Note: Using
statements are broken into smaller groups to allow disposing of resources as we are finished with them to reduce the total memory consumption of this process...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…