在 Xamarin Android C# 中从 Android.Graphics.Drawables 转换为字节 []
Convert From Android.Graphics.Drawables To Byte[] in Xamarin Android C#
我找不到这个问题的任何解决方案。
这是我的代码:
// 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[] 到 return 到 Xamarin.Forms PCL Project.
任何人都可以建议我应该使用什么来在 Xamarin 跨平台应用程序中实现文本到图像。
使用下面的代码
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
,所以 vaule 是 null ,你可以从 [=12= 创建一个 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();
参考.
我找不到这个问题的任何解决方案。
这是我的代码:
// 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[] 到 return 到 Xamarin.Forms PCL Project.
任何人都可以建议我应该使用什么来在 Xamarin 跨平台应用程序中实现文本到图像。
使用下面的代码
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
,所以 vaule 是 null ,你可以从 [=12= 创建一个 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();
参考