【发布时间】:2018-07-19 12:54:58
【问题描述】:
我正在使用 BotFramework 在 PNG 图像上绘制字符串。然后我将它从内存中的字节数组转换为 base64 字符串。我将 base64 字符串发布到服务并获得正确的响应。一切正常,但在处理完后我收到“抱歉,我的机器人代码有问题”消息。
case 5:
{
try
{
...
graphics.DrawString(text, fonti, brush, drawRect, stringFormat);
using (MemoryStream m = new MemoryStream())
{
image.Save(m, image.RawFormat);
// I've tried changing this line to String or StringBuilder but
// nothing changed
IMAGE = $"data:image/png;base64,{Convert.ToBase64String(m.ToArray())}";
m.Close();
}
}
catch { await context.PostAsync("ERR1"); }
string json = null;
try
{
string FormStuff = string.Format($"somecontent");
StringContent content = new StringContent(
FormStuff,
Encoding.UTF8,
"application/x-www-form-urlencoded");
string url = string.Format("http://www.example.com/");
var response = await client.PostAsync(url, content);
json = (await response.Content.ReadAsStringAsync()).ToString();
}
catch { await context.PostAsync("ERR2"); }
...
}
break;
IMAGE 变量是一个字符串。
每当我删除或更改 Convert.ToBase64String() 时,问题就消失了,但是我无法按我的意愿使用该服务,并且该过程被破坏了。
这里唯一的问题是抛出异常并显示给最终用户。
抱歉,我的机器人代码有问题
编辑:我发现如果我的帖子请求中的内容太长,我会收到错误消息。我试过使用FormUrlEncodedContent,但它抛出了这个:
无效的 URI:Uri 字符串太长。
我怎样才能以其他方式发布它?
【问题讨论】:
-
问题是什么?
-
旁注:您可能只是在使用 PNG 图像,但既然您肯定声称输出是 PNG,为什么不使用
ImageFormat.Png而不是使用 随便image.RawFormat返回。 -
@Damien_The_Unbeliever 没有解决问题
-
这就是为什么我在它前面加上“旁注”并将其作为评论发布,不是答案。它不会解决问题(或者至少看起来不到 1% 的可能性),但是一旦你的问题得到解决,你应该考虑清理代码。
标签: c# string image base64 botframework