none
Image to Base64 works but with some strange things. RRS feed

  • Question

  • Hi guys,

    I've been working on converting Images to Base64.

    I got this working but, as in the code below shows there is a MsgBox that tells me the pbPreview.ImageLocation.
    If i remove this MsgBox the code won't work and tells me that its not assigned to an object.

    Private Sub GegevensToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles GegevensToolStripMenuItem.Click
            CollectAndConvertPictures()
    End Sub
    
    Private Sub CollectAndConvertPictures()
            For Each Pictures As String In System.IO.Directory.GetDirectories(Dashboard.Werkmap.Text + "\Pictures")           
    
                    PathField.Text = Dashboard.Werkmap.Text + "\Pictures\" + Path.GetFileName(Pictures) + "\" + Path.GetFileName(Pictures) + ".jpg"
    
                    pbPreview.ImageLocation = PathField.Text
                
                    MsgBox(pbPreview.ImageLocation) 'THIS IS THE MSGBOX THAT IS NOT NEEDED BUT WHEN REMOVED CODE WONT WORK
                    ConvertBase64()
            Next
    End Sub
    
      Private Sub ConvertBase64()
            Dim EncodingTypeString As String = String.Empty
            If pbPreview.ImageLocation.ToLower.EndsWith(".jpg") Then
                EncodeType = ImageFormat.Jpeg
                EncodingTypeString = "data:image/jpeg;base64"
            ElseIf pbPreview.ImageLocation.ToLower.EndsWith(".png") Then
                EncodeType = ImageFormat.Png
                EncodingTypeString = "data:image/png;base64"
            ElseIf pbPreview.ImageLocation.ToLower.EndsWith(".gif") Then
                EncodeType = ImageFormat.Gif
                EncodingTypeString = "data:image/gif;base64"
            ElseIf pbPreview.ImageLocation.ToLower.EndsWith(".bmp") Then
                EncodeType = ImageFormat.Bmp
                EncodingTypeString = "data:image/bmp;base64"
            End If
               
            DataFile.Text = DataFile.Text & EncodingTypeString & ImageToBase64(pbPreview.Image, EncodeType)
            pbPreview.Image = Nothing
        End Sub
    
     Public Function ImageToBase64(ByVal image As Image, ByVal format As ImageFormat) As String
            Dim ms As New MemoryStream
                image.Save(ms, format)
                Dim ImageBytes As Byte() = ms.ToArray
                Dim Base64string As String = Convert.ToBase64String(ImageBytes)
                Return Base64string
    
        End Function


    Someone can tell me why this MsgBox is needed?

    Greetings :)

    EDIT:

    Okay So if i replace Msgbox(pbPreview.ImageLocation) with pbPreview.Load() my problem is solved.
    But, i still don't know why this is needed.

    Wednesday, July 11, 2018 11:21 PM

Answers

  • I guess this is because the image is loaded on demand. Within your loop without a messagebox, there is no time for Windows to tell the picturebox to paint itself, and consequently the picturebox doesn't load the image. Hence there is no image to save -> exception.

    However, if you insert a mesagebox, the thread get's idle and WM_PAINT is received by the picturebox causing itself to paint and load the image. Consequently, the image property is not empty anymore -> no exception.


    Armin

    Thursday, July 12, 2018 11:54 PM

All replies

  • Hi Kevin

    Is this an error?

    If there is not (pbPreview.ImageLocation),The first parameter of the ImageToBase method is empty.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 12, 2018 3:34 AM
  • I guess this is because the image is loaded on demand. Within your loop without a messagebox, there is no time for Windows to tell the picturebox to paint itself, and consequently the picturebox doesn't load the image. Hence there is no image to save -> exception.

    However, if you insert a mesagebox, the thread get's idle and WM_PAINT is received by the picturebox causing itself to paint and load the image. Consequently, the image property is not empty anymore -> no exception.


    Armin

    Thursday, July 12, 2018 11:54 PM
  • Thanks :)
    Saturday, July 14, 2018 7:49 PM