Общая ошибка в GDI+ (Vb.NET)
Код для кнопки «Сохранить»:
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
Код для открытого диалога (изменить картинку):
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
У меня есть система, в которой есть функция изменения изображений профиля, но если я сохраняю изображение профиля, в нем всегда возникает ошибка, называемая Общая ошибка в GDI + (Vb.NET)
В дополнение к комментарию Джими, когда вы используете Image.FromFile (), в ЗАМЕЧАНИЯХ говорится, что «Файл остается заблокированным до тех пор, пока изображение не будет удалено». Когда вы меняете код Джими, файл не блокируется...
@ Джими, что именно я должен изменить в PbProfilePicture.Image?
По сути, везде, где вы используете Image.FromFile(), вам нужно заменить его на Image.FromStream(), как показано в комментарии Джими.
@Idle_Mind, ну типа... PbProfilePicture.Image = Image.FromStream(???). Есть ли что-то, что я должен добавить или отредактировать, чтобы работать? Извините за глупый вопрос, я впервые использую эту функцию, так что да





Вы можете обратиться к следующему коду, как к примеру Джими.
PbProfilePicture.Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(ImagePath & UserImage))
Замените часть Image.FromFile(ImagePath & UserImage) на Image.FromStream(New MemoryStream(File.ReadAllBytes(ImagePath & UserImage))
Первое, что вы должны изменить, это:
DuplicateImage = Image.FromFile(.FileName). Замените наDuplicateImage = Image.FromStream(New MemoryStream(File.ReadAllBytes(.FileName)), true, false). То же самое касаетсяPbProfilePicture.Image