简体   繁体   English

将base64字符串转换为图片框图像时,应用程序挂起

[英]Application hangs when converting base64 string to picturebox image

When my form is displayed it is fed a base64 string which contains an image, but when I attempt to display this image in a picture box the application hangs and I have to force kill the app. 当显示我的表单时,它会被喂入包含图像的base64字符串,但是当我尝试在图片框中显示该图像时,应用程序挂起,我不得不强制终止该应用程序。 My question is how do I convert the string and then display it in a picture box without it hanging and crashing. 我的问题是如何转换字符串,然后将其显示在图片框中而不会挂起和崩溃。

public partial class DisplayPic : Form
{
    string base64String;
    public DisplayPic(string img)
    {
        base64String = img;
        InitializeComponent();
    }

    private void DisplayPic_Load(object sender, EventArgs e)
    {
        // Convert base 64 string to byte[]
        byte[] imageBytes = Convert.FromBase64String(base64String);
        // Convert byte[] to Image
        using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
        {
            pictureBox1.Image = Image.FromStream(ms, true);
        }
    }
}

Okay, so I have created a bodge; 好吧,所以我创建了一个bodge。 When the base64 string is converted to bytes I use the File.WriteAllBytes() method which saves the file. 当base64字符串转换为字节时,我使用File.WriteAllBytes()方法保存文件。 Once the file is then saved I used the Image.FromFile() method which I then used to set the picturebox image. 保存文件后,我使用了Image.FromFile()方法,该方法随后用于设置图片框图像。

I know it is a bad way to do it but as I cannot find another way which works this is the option I have gone with. 我知道这是一种不好的方法,但是由于找不到其他可行的方法,因此我选择了这种方法。

Thank you for all the help and suggestions. 感谢您提供的所有帮助和建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM