Converting image to base64

前端 未结 2 1947
失恋的感觉
失恋的感觉 2020-11-28 12:05

I have the following code to convert image to base64:

private void btnSave_Click(object sender, RoutedEventArgs e)
    {
        StreamResourceInfo sri = nul         


        
相关标签:
2条回答
  • 2020-11-28 12:51

    What about trying:

    public static BitmapImage base64image(string base64string)
    {
        byte[] fileBytes = Convert.FromBase64String(base64string);
    
        using (MemoryStream ms = new MemoryStream(fileBytes))
        {
            Image streamImage = Image.FromStream(ms);
            context.Response.ContentType = "image/jpeg";
            streamImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);
            return streamImage;
        }
    }
    

    I agree with Alexei that your code for reading the image in does look a little strange. I've recently written some code for a similar task that I was doing which might point you in the right direction:

           string fileContent = null;
            /* Check the file actually has some content to display to the user */
            if (uploadFile != null && uploadFile.ContentLength > 0)
            {
                byte[] fileBytes = new byte[uploadFile.ContentLength];
                int byteCount = uploadFile.InputStream.Read(fileBytes, 0, (int)uploadFile.ContentLength);
                if (byteCount > 0)
                {
                    fileContent = CreateBase64Image(fileBytes);
                }
            }
    
        private string CreateBase64Image(byte[] fileBytes)
        {
            Image streamImage;
            /* Ensure we've streamed the document out correctly before we commit to the conversion */
            using (MemoryStream ms = new MemoryStream(fileBytes))
            {
                /* Create a new image, saved as a scaled version of the original */
                streamImage = ScaleImage(Image.FromStream(ms));
            }
            using (MemoryStream ms = new MemoryStream())
            {
                /* Convert this image back to a base64 string */
                streamImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                return Convert.ToBase64String(ms.ToArray());
            }
        }
    
    0 讨论(0)
  • 2020-11-28 12:51

    not an answer: more of a long comment ... OP states that decoding code works perfectly fine, also it looks suspicios. Also code assumed to be verified to work on PNG images, but saving code explicitly produces valid JPG with SaveJpeg call...


    Your code that creates stream for reading looks strange - you create stream over existing byte array, than write the same bytes into that stream, and that pass that stream without seeking back to 0 to some method.

    Potential fix (assuming BitampImage can accept JPG stream):

    • don't call Write at all as stream already have the bytes you want
    • set ms.Position = 0 after writing to the stream.

    Note: I'm not sure if it is OK to dispose stream that is a source for BitmapImage, you may need to remove using too.

    0 讨论(0)
提交回复
热议问题