Search code examples
vb.netvisual-studiopictureboxgdi

A generic error occurred in GDI+ (VB.NET)


A Generic error occurred in GDI+ (Vb.NET)

Code for Save Button:

If MessageBox.Show("Before continuing, make sure you profile picture doesn't contain any material that is sensitive or controversial. Do you want to change your profile picture?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = vbYes Then

    If PbProfilePicture.Image IsNot Nothing Then
        Dim ImagePath As String = Application.StartupPath & "\frmUserProfilePictures"
        Dim NewFile As String = LblUniqueID_MainMenu.Text & ".jpg"

        PbProfilePicture.Image.Save(ImagePath & "\" & NewFile, System.Drawing.Imaging.ImageFormat.Jpeg)

        MessageBox.Show("Your profile picture has been saved!", "Profile picture changed successfully", MessageBoxButtons.OK, MessageBoxIcon.Information)

        Modify.CheckProfilePicture_IfExist(LblUniqueID_MainMenu.Text)
        BtnSaveImage.Visible = False
        BtnChangePicture.Text = "Change"
        BtnChangePicture.BackColor = Color.PaleTurquoise
    End If
End If

Code for the open dialog (change pic):

    Dim OFD As New OpenFileDialog
    Dim DuplicateImage As Image

    Dim UserImage As String = UniqueID & ".jpg"
    Dim ImagePath As String = Application.StartupPath & "\frmUserProfilePictures\"

    If frmUserMainMenu.BtnChangePicture.Text = "Change" Then
        With OFD
            .CheckFileExists = True
            .ShowReadOnly = False
            .Filter = "JPEG files (*.jpg)|*.jpg|BMP files (*.bmp)|*.bmp|PNG files (*.png)|*.png|ALL files (*.*)|*.*"
            .FilterIndex = 1

            If .ShowDialog = DialogResult.OK Then
                DuplicateImage = Image.FromFile(.FileName)
                Picture.Image = DuplicateImage

                frmUserMainMenu.BtnChangePicture.BackColor = Color.IndianRed
                frmUserMainMenu.BtnChangePicture.Text = "Remove"
                frmUserMainMenu.BtnSaveImage.Visible = True
            Else
                If System.IO.File.Exists(ImagePath & UserImage) Then
                    frmUserMainMenu.PbProfilePicture.Image = Image.FromFile(ImagePath & UserImage)
                End If
                .Dispose()
            End If
        End With
    End If

I have a system that has a feature of changing profile pictures but if i save the profile picture it always has an error called A Generic error occurred in GDI+ (Vb.NET)


Solution

  • You can refer to the following code, just as Jimi's example.

    PbProfilePicture.Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(ImagePath & UserImage))

    Replace the Image.FromFile( ImagePath & UserImage ) part with Image.FromStream(New MemoryStream(File.ReadAllBytes( ImagePath & UserImage ))