Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
778 views
in Technique[技术] by (71.8m points)

xamarin - Xamrin.Android add image watermark

How can I add another smaller image as a watermark of a larger image using Xamarin.Android c# and save the output (JPEG/JPG) image to either internal/external storage of an android device.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...