Converting image to base64

前端 未结 2 1982
失恋的感觉
失恋的感觉 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

    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.

提交回复
热议问题