0

I can't found any solution of this issue.
Here is my code:

// Xamarin Android 
// Call via Dependency Service  
Drawable drawable = TextDrawable.Android.Ui.TextDrawable.TextDrawable.TextDrwableBuilder
                    .BeginConfig()
                    .FontSize(70)
                    .WithBorder(2)
                    .EndConfig().BuildRound("A", Color.Black);

var img = new ImageView(Application.Context);
img.SetImageDrawable(drawable);

I looked through many answer and I found one but it is not working:

BitmapDrawable bitmapDrawable = (img.Drawable as BitmapDrawable); // this is also null every time
Bitmap bitmap;
if (bitmapDrawable == null)
{
    img.BuildDrawingCache();
    bitmap = img.DrawingCache; **// Here is Null Every Time**
    img.BuildDrawingCache(false);
}
else
{
    bitmap = bitmapDrawable.Bitmap;
}

byte[] bitmapData;
using (var stream = new MemoryStream())
{
     bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
     bitmapData = stream.ToArray();
}

When I try this I am getting a null ref exception.

I have a NuGet package that make a Drawable object using text and it is also convert into ImageView. I want to convert that Drawable to byte[] to return to Xamarin.Forms PCL Project.

Can anyone suggest what I should use to achieve text to image in Xamarin Cross Platform Application.

Snostorp
  • 544
  • 1
  • 8
  • 14

1 Answers1

0

Use the following code

 Bitmap bitmap = ((BitmapDrawable)img.Drawable).Bitmap as Bitmap;
 MemoryStream baos = new MemoryStream();

 bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, baos);
 byte[] imageInByte = baos.ToArray();

Update

drawable is TextDrawable which inherits from ShapeDrawable , but here you use it as BitmapDrawable , so the vaule is null , you could create a Bitmap object from drawable instance first .

  Drawable drawable = TextDrawable.TextDrwableBuilder
            .BeginConfig()
            .FontSize(70)
            .WithBorder(2)
            .EndConfig().BuildRound("A", Color.Black);


        ImageView img = v1.FindViewById<ImageView>(Resource.Id.button1) as ImageView;
        img.SetImageDrawable(drawable);


        TextDrawable bitmapDrawable = drawable as TextDrawable;


        Bitmap bitmap = null;
        if (bitmapDrawable.IntrinsicWidth<=0 || bitmapDrawable.IntrinsicHeight <= 0)
        {
            bitmap = Bitmap.CreateBitmap(1, 1, Bitmap.Config.Argb8888);
        }
        else
        {
            bitmap = Bitmap.CreateBitmap(bitmapDrawable.IntrinsicWidth, bitmapDrawable.IntrinsicHeight, Bitmap.Config.Argb8888);
        }

        Canvas canvas = new Canvas(bitmap);
        bitmapDrawable.SetBounds(0, 0, canvas.Width, canvas.Height);
        bitmapDrawable.Draw(canvas);



        MemoryStream baos = new MemoryStream();

        bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, baos);
        byte[] imageInByte = baos.ToArray();

Refer https://stackoverflow.com/a/46531354/8187800.

ColeX
  • 14,062
  • 5
  • 43
  • 240
  • i found the code is identical as yours , could you provide the plugin name so that i can test on my side ? – ColeX Jun 12 '20 at 08:26
  • https://github.com/jagadeesh1492/Android-Ui/blob/master/TextDrawable/TextDrawable.cs https://github.com/jagadeesh1492/Android-Ui/blob/master/TextDrawable/Util.cs the Nugut Package not installing so i add this two files from nuget and start work and its working fine – Muhammad Usama Alam Jun 12 '20 at 15:30
  • ((BitmapDrawable)img.Drawable) this return null every time – Muhammad Usama Alam Jun 12 '20 at 15:31