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M6tapt0+4kOTsuLRcJFvbA861EboSXdZsBK9vC57Tdo4uHJ/09p3cb6fFBu6W/wt2vqcVXL5LWi01/LJ2f37en4s//2Q==`
这是我的代码
byte[] decodedByte = Android.Util.Base64.Decode(Config.Base64Photo, Android.Util.Base64Flags.NoPadding);
var _bitmap = Android.Graphics.BitmapFactory.DecodeByteArray(decodedByte, 0, decodedByte.Length);
var _drawable = new Android.Graphics.Drawables.BitmapDrawable(_bitmap );
avatarView.SetImageDrawable(_drawable);
如果我使用
byte[] decodedByte = Android.Util.Base64.Decode(Config.Base64Photo, Android.Util.Base64Flags.NoPadding
我的基础变坏了
如果我使用
byte[] decodedByte = Android.Util.Base64.Decode(Config.Base64Photo, Android.Util.Base64Flags.UrlSafe
我得到_bitmap变量为null
如何将此base64解码为位图?
答案 0 :(得分:1)
你的base64字符串在data:image/jpg;base64,
之后开始。所以你的代码应该类似于这个
var buf = Convert.FromBase64String(base64.Substring(base64.IndexOf(',') + 1));
c#中的完整代码
var buf = Convert.FromBase64String(base64.Substring(base64.IndexOf(',') + 1));
var bmp = Bitmap.FromStream(new MemoryStream(buf));
bmp.Save(@"a.bmp", System.Drawing.Imaging.ImageFormat.Bmp);