如何创建一个base64字符串从画布Windows Phone 8.1(通用应用程序)
本文关键字:Phone Windows 应用程序 字符串 创建 何创建 base64 一个 | 更新日期: 2023-09-27 18:03:01
我有一个捕获签名的应用程序,我已经绘制了签名,现在我想捕获信息并创建一个base64字符串
对不起,这里是为我工作的代码,并不完美。请随时审查,如果你发现任何东西,请提供反馈。由于
在这个例子中,画布的名称是Display
private async Task<string> generateBase64Bitmap()
{
// Initialization
try
{
// Initialization.
Size canvasSize = this.Display.RenderSize;
Point defaultPoint = this.Display.RenderTransformOrigin;
// Sezing to output image dimension.
this.Display.Measure(canvasSize);
this.Display.UpdateLayout();
this.Display.Arrange(new Rect(defaultPoint, canvasSize));
// Convert canvas to bmp.
var bmp = new RenderTargetBitmap();
await bmp.RenderAsync(this.Display, (int)(this.Display.ActualWidth / 4), (int)(this.Display.ActualHeight / 4));
// Setting.
var bitmap = (RenderTargetBitmap) bmp;
//var bytes = (await bitmap.GetPixelsAsync()).ToArray();
return await toBase64(bitmap);
}
catch (Exception ex)
{
//this.ShowMessage(ex.ToString(), "Error");
}
return string.Empty;
}
private async Task<string> toBase64(RenderTargetBitmap bmp)
{
var img = (await bmp.GetPixelsAsync()).ToArray();
var encoded = new InMemoryRandomAccessStream();
var encoder = await BitmapEncoder.CreateAsync(
BitmapEncoder.PngEncoderId, encoded);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight,
(uint)bmp.PixelWidth, (uint)bmp.PixelHeight, 96, 96, img);
await encoder.FlushAsync();
encoded.Seek(0);
//read bytes
var bytes = new byte[encoded.Size];
await encoded.AsStream().ReadAsync(bytes, 0, bytes.Length);
return Convert.ToBase64String(bytes);
}
调用方法使用(将async添加到你的方法中):
string encodedImgBase64 = await generateBase64Bitmap();