Hello,
Welcome to our Microsoft Q&A platform!
I used the following code to write text to image. You can make a test(I do not save the image to output path, I set it to imageview directly).
public class MainActivity : AppCompatActivity
{
Android.Graphics.Bitmap bitmap;
Android.Graphics.Bitmap mutableBitmap;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
Watermark();
ImageView imageView1 = FindViewById<ImageView>(Resource.Id.imageView1);
imageView1.SetImageBitmap(mutableBitmap);
}
public void Watermark()
{
bitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.hamburger); // Load your bitmap here
mutableBitmap = bitmap.Copy(Android.Graphics.Bitmap.Config.Argb8888, true);
Canvas canvas = new Canvas(mutableBitmap);
// Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.Color= Android.Graphics.Color.Black;
paint.TextSize=50;
canvas.DrawText("Some", 50, 50, paint);
}
Here is running screenshot.
======================
Update========================
I need to save picture after watermark, Is there any way?
If you want to write picture to external path,
If you want to support Android 11 or later devices, t you can var sdCardPath = GetExternalFilesDir(string.Empty).Path;
path
If you want to support Android 10 or before, you can use "/storage/emulated/0/Download/"
this path, and add android:requestLegacyExternalStorage="true"
to <application>
tag in your AndroidManifest.xml
For these details, you can refer to the following code.
Here is my test code.
private void Button1_Click(object sender, System.EventArgs e)
{
var sdCardPath = GetExternalFilesDir(string.Empty).Path;
//"/storage/emulated/0/Download/"
var filePath = System.IO.Path.Combine(sdCardPath, "test.png");
var stream = new FileStream(filePath, FileMode.Create);
mutableBitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, stream);
stream.Close();
}
Here is running screenshot.
for `` var sdCardPath = GetExternalFilesDir(string.Empty).Path;
for "/storage/emulated/0/Download/"
Best Regards,
Leon Lu
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.