|
-
Oct 30th, 2010, 07:02 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] getting error when sing image
I am getting error when I tried to save image from PictureBox
Saying "generic error occurred in GDI+", that was appear when a file already exist. but working if file not already exist. It can't override the image inside the folder. How can trap that to override the image. Thanks.
Here is my code
Code:
Private Sub opneIMG_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles opneIMG.Click
Dim openFD As New OpenFileDialog
Dim clientImg As New PictureBox
Dim img As Bitmap
With openFD
.Title = "Select Picture of Client" & lvClients.Items(lvClients.FocusedItem.Index).SubItems(1).Text
.DefaultExt = "*.jpg"
.Filter = "*.jpg | *.jpg"
.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures
.Multiselect = False
.ShowDialog()
img = Bitmap.FromFile(.FileName)
End With
Dim bmp As Bitmap = New Bitmap(img, 150, 150)
Dim grp As Graphics = Graphics.FromImage(bmp)
clientImg.Image = bmp
clientImg.Image.Save(Application.StartupPath & "\PROFILES\" & IDtoSearch & "\" & IDtoSearch & ".jpeg", Imaging.ImageFormat.Jpeg)
End Sub
VB 6.0 = "Self-Study" Then
vb.NET = "Self-Study" Then
C# = 'on going study.....
-
Oct 30th, 2010, 07:47 PM
#2
Re: getting error when sing image
try this:
vb Code:
Private Sub opneIMG_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles opneIMG.Click
Dim openFD As New OpenFileDialog
Dim clientImg As New PictureBox
Dim img As Bitmap
With openFD
.Title = "Select Picture of Client" & lvClients.Items(lvClients.FocusedItem.Index).SubItems(1).Text
.DefaultExt = "*.jpg"
.Filter = "*.jpg | *.jpg"
.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures
.Multiselect = False
if .ShowDialog() = dialogresult.ok then
img = New Bitmap(.FileName)
end if
End With
Dim bmp As New Bitmap(img, 150, 150)
Dim grp As Graphics = Graphics.FromImage(bmp)
clientImg.Image = bmp
bmp.Save(Application.StartupPath & "\PROFILES\" & IDtoSearch & "\" & IDtoSearch & ".jpeg", Imaging.ImageFormat.Jpeg)
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 31st, 2010, 04:48 AM
#3
Thread Starter
Hyperactive Member
Re: getting error when sing image
still getting same error sir.
VB 6.0 = "Self-Study" Then
vb.NET = "Self-Study" Then
C# = 'on going study.....
-
Oct 31st, 2010, 06:35 AM
#4
Re: getting error when sing image
try replacing line 19 in .Paul's code by:
Code:
clientImg.Load(FD.FileName)
Explanation: you were not allowed to overwrite the image because it is the same object as the PictureBox.Image. PictureBox.Load gets a copy of the original image instead of the image itself.
BB
-
Oct 31st, 2010, 08:58 AM
#5
Thread Starter
Hyperactive Member
Re: getting error when sing image
It seems that working now.
Here is my revised codes.
I added delete file if exist function.
vbCode Code:
Private Sub opneIMG_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles opneIMG.Click
Dim openFD As New OpenFileDialog
Dim clientImg As New PictureBox
Dim img As Bitmap
Dim fileExist As String = Application.StartupPath & "\PROFILES\" & IDtoSearch & "\" & IDtoSearch & ".jpeg"
With openFD
.Title = "Select Picture of Client" & lvClients.Items(lvClients.FocusedItem.Index).SubItems(1).Text
.DefaultExt = "*.jpg"
.Filter = "*.jpg | *.jpg"
.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures
.Multiselect = False
If .ShowDialog() = DialogResult.OK Then
img = Bitmap.FromFile(.FileName)
picProfile.Image.Dispose()
If File.Exists(fileExist) = True Then
File.Delete(fileExist)
End If
Dim bmp As Bitmap = New Bitmap(img, 150, 150)
Dim grp As Graphics = Graphics.FromImage(bmp)
clientImg.Image = bmp
clientImg.Image.Save(Application.StartupPath & "\PROFILES\" & IDtoSearch & "\" & IDtoSearch & ".jpeg", Imaging.ImageFormat.Jpeg)
ElseIf DialogResult.Cancel Then
MsgBox("Old profile picture remain!", vbOKOnly, "Cancel")
End If
End With
End Sub
VB 6.0 = "Self-Study" Then
vb.NET = "Self-Study" Then
C# = 'on going study.....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|