简体   繁体   中英

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. 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; When the base64 string is converted to bytes I use the File.WriteAllBytes() method which saves the file. Once the file is then saved I used the Image.FromFile() method which I then used to set the picturebox image.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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