在 C# 中将 DataUrl 转换为图像并使用字节写入文件

问题描述 投票:0回答:2

你好,我有这样的签名:

enter image description here

它被编码为数据 URL,特别是这个字符串:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAADICAYAAADGFbfiAAAYlElEQVR4Xu2dC8w1R1nHQSCIgIKVGLmoiLciFwUs... (long string)

我想要做的是将这个数据Url转换为PNG图像,并将图像保存到设备,这就是我到目前为止所做的:

if (newItem.FieldType == FormFieldType.Signature)
{
     if (newItem.ItemValue != null)
     {
           //string completeImageName = Auth.host + "/" + li[i];
           string path;
           string filename;
           string stringName = newItem.ItemValue;

           var base64Data = Regex.Match(stringName, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;
           var binData = Convert.FromBase64String(base64Data);

           path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

           filename = Path.Combine(path, base64Data);

           if (!File.Exists(filename))
           {
                 using (var stream = new MemoryStream(binData))
                 {
//Code crashing here--------------------------
                      File.WriteAllBytes(filename, binData);
                  }
            }

        newItem.ItemValue = filename;

    }
}

         App.Database.SaveReportItem(newItem);

但是我的代码使我的应用程序特别在这一行崩溃:

File.WriteAllBytes(filename, binData);

我用作参考的示例(链接)使用了 PictureBox,但在 Xamarin 中没有使用 pictureBox。

有什么想法吗?

c# .net xamarin memorystream xamarin.forms
2个回答
23
投票

正如@SLaks提到的,我不需要MemoryStream,我的代码的问题是路径和文件名以获得进一步的帮助,这是工作代码:

if (newItem.FieldType == FormFieldType.Signature)
{
    if (newItem.ItemValue != null)
    {
        //string completeImageName = Auth.host + "/" + li[i];
        string path;
        string filename;
        string stringName = newItem.ItemValue;

        var base64Data = Regex.Match(stringName, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;
        var binData = Convert.FromBase64String(base64Data);

        path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

        //filename = Path.Combine(path, base64Data.Replace(@"/", string.Empty));

        long milliseconds = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
        string fileName = "Sn" + milliseconds.ToString() + ".PNG";
        filename = Path.Combine(path, fileName);

        if (!File.Exists(filename))
        {
            //using (var stream = new MemoryStream(binData))
            //{
                File.WriteAllBytes(filename, binData);
            //}
        }

        newItem.ItemValue = filename;

    }
}

App.Database.SaveReportItem(newItem);

图像显示:

enter image description here


17
投票

我刚刚清理了 Mario 的代码并微调了正则表达式:

public string SaveDataUrlToFile(string dataUrl, string savePath)
{
    var matchGroups = Regex.Match(dataUrl, @"^data:((?<type>[\w\/]+))?;base64,(?<data>.+)$").Groups;
    var base64Data = matchGroups["data"].Value;
    var binData = Convert.FromBase64String(base64Data);
    System.IO.File.WriteAllBytes(savePath, binData);
    return savePath;
}
© www.soinside.com 2019 - 2024. All rights reserved.