【问题标题】:Convert From Android.Graphics.Drawables To Byte[] in Xamarin Android C#在 Xamarin Android C# 中从 Android.Graphics.Drawables 转换为字节 []
【发布时间】:2020-06-12 06:15:05
【问题描述】:

我找不到此问题的任何解决方案。
这是我的代码:

// 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);

我浏览了很多答案,我找到了一个,但它不起作用:

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();
}

当我尝试这个时,我得到一个 null ref 异常。

我有一个 NuGet 包,它使用文本制作一个 Drawable 对象,它也可以转换为 ImageView。我想将该 Drawable 转换为 byte[] 以返回 Xamarin.Forms PCL 项目。

谁能建议我应该使用什么来在 Xamarin 跨平台应用程序中实现文本到图像。

【问题讨论】:

    标签: c# xamarin.forms xamarin.android android-drawable


    【解决方案1】:

    使用以下代码

     Bitmap bitmap = ((BitmapDrawable)img.Drawable).Bitmap as Bitmap;
     MemoryStream baos = new MemoryStream();
    
     bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, baos);
     byte[] imageInByte = baos.ToArray();
    

    更新

    drawable 是继承自 ShapeDrawable 的 TextDrawable ,但在这里您将其用作 BitmapDrawable ,因此值是 null ,您可以先从 drawable 实例创建 Bitmap 对象。

      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();
    

    请参考https://stackoverflow.com/a/46531354/8187800

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 2013-11-29
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多