签名板发送空
我正在使用Signa Pad和SkiaSharp的Canvas,但是当我在用户绘制后发送并编码为base 64时,服务器上仅显示一个空画布
async void OnSaveButtonClicked(object sender, EventArgs args)
{
using (SKImage image = SKImage.FromBitmap(saveBitmap))
{
try
{
SKData data = image.Encode(SKEncodedImageFormat.Png, 100);
var bytesImg = data.ToArray();
string imageBase64 = Convert.ToBase64String(bytesImg);
var respuesta = await this.ApiService.PostSignature(
this.url,
this.Id,
imageBase64
);
发送服务的方法... 公共异步任务PostSignature( 字符串urlBase, 弦作品集, 字符串imageBase64) { 尝试 { var client = new HttpClient(); var response = await client.PostAsync(urlBase, 新的StringContent(string.Format( “ idReporte = {0}&imgFirma = {1}”, 作品集,图像编码), Encoding.UTF8,“ application / x-www-form-urlencoded”));
if (!response.IsSuccessStatusCode)
{
return response.ToString();
}
else
{
var result = await response.Content.ReadAsStringAsync();
return result;
}
}
catch
{
return null;
}
}
结束请求...
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert(
"Error",
"Image Is not Send, error: " + ex.Message,
"OK"
);
}
finally
{
completedPaths.Clear();
inProgressPaths.Clear();
UpdateBitmap();
canvasView.InvalidateSurface();
}
图像为空,已解码正常并加载到文件夹路径中。
答案 0 :(得分:0)
根据您的描述,您想从签名板上获取图像并将其转换为base 64,我做了一个简单的操作,您可以看一下,只需将图像流投射到内存流中即可。
<StackLayout>
<forms:SignaturePadView
x:Name="signaturepad"
BackgroundColor="Black"
HeightRequest="350"
StrokeColor="White"
StrokeWidth="3"
WidthRequest="250" />
<Button
x:Name="save"
Clicked="Save_Clicked"
HeightRequest="50"
Text="save"
WidthRequest="200" />
</StackLayout>
private async void Save_Clicked(object sender, EventArgs e)
{
string base64String;
using (var memoryStream = new MemoryStream())
{
var signature = await signaturepad.GetImageStreamAsync(SignatureImageFormat.Png);
signature.CopyTo(memoryStream);
var byteArray = memoryStream.ToArray();
base64String = Convert.ToBase64String(byteArray);
}
}