Converting from string to Image in C#

前端 未结 5 2075
生来不讨喜
生来不讨喜 2021-02-20 06:22

I am trying to convert a Unicode string to an image in C#. Each time I run it I get an error on this line

Image image = Image.FromStream(ms, true, true);
         


        
5条回答
  •  梦谈多话
    2021-02-20 07:00

    You're getting an image as a string, out of ldap? I'm pretty sure if that was true, the string would actually be base64 encoded, and in that case contains bytes that represent actual characters, and not image data.

    Could you post a snippit of the string you're getting?

    If it's true, you need to take the string and turn it a byte[] by un-base64'ing it, and then use the byte array to make the image. Combined with @JonBenedicto's code:

    public Image stringToImage(string inputString)
    {
        byte[] imageBytes = Convert.FromBase64String(inputString);
        MemoryStream ms = new MemoryStream(imageBytes);
    
        Image image = Image.FromStream(ms, true, true);
    
        return image;
    }
    

提交回复
热议问题