Results 1 to 5 of 5

Thread: [RESOLVED] getting error when sing image

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Resolved [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.....

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: getting error when sing image

    try this:

    vb Code:
    1. Private Sub opneIMG_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles opneIMG.Click
    2.     Dim openFD As New OpenFileDialog
    3.     Dim clientImg As New PictureBox
    4.     Dim img As Bitmap
    5.     With openFD
    6.         .Title = "Select Picture of Client" & lvClients.Items(lvClients.FocusedItem.Index).SubItems(1).Text
    7.         .DefaultExt = "*.jpg"
    8.         .Filter = "*.jpg | *.jpg"
    9.         .InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures
    10.         .Multiselect = False
    11.         if .ShowDialog() = dialogresult.ok then
    12.             img = New Bitmap(.FileName)
    13.         end if
    14.     End With
    15.     Dim bmp As New Bitmap(img, 150, 150)
    16.  
    17.     Dim grp As Graphics = Graphics.FromImage(bmp)
    18.  
    19.     clientImg.Image = bmp
    20.     bmp.Save(Application.StartupPath & "\PROFILES\" & IDtoSearch & "\" & IDtoSearch & ".jpeg", Imaging.ImageFormat.Jpeg)
    21. End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    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.....

  4. #4
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    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:
    1. Private Sub opneIMG_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles opneIMG.Click
    2.         Dim openFD As New OpenFileDialog
    3.         Dim clientImg As New PictureBox
    4.         Dim img As Bitmap
    5.         Dim fileExist As String = Application.StartupPath & "\PROFILES\" & IDtoSearch & "\" & IDtoSearch & ".jpeg"
    6.         With openFD
    7.             .Title = "Select Picture of Client" & lvClients.Items(lvClients.FocusedItem.Index).SubItems(1).Text
    8.             .DefaultExt = "*.jpg"
    9.             .Filter = "*.jpg | *.jpg"
    10.             .InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures
    11.             .Multiselect = False
    12.             If .ShowDialog() = DialogResult.OK Then
    13.                 img = Bitmap.FromFile(.FileName)
    14.                 picProfile.Image.Dispose()
    15.                 If File.Exists(fileExist) = True Then
    16.                     File.Delete(fileExist)
    17.                 End If
    18.  
    19.                 Dim bmp As Bitmap = New Bitmap(img, 150, 150)
    20.                 Dim grp As Graphics = Graphics.FromImage(bmp)
    21.                 clientImg.Image = bmp
    22.                 clientImg.Image.Save(Application.StartupPath & "\PROFILES\" & IDtoSearch & "\" & IDtoSearch & ".jpeg", Imaging.ImageFormat.Jpeg)
    23.             ElseIf DialogResult.Cancel Then
    24.                 MsgBox("Old profile picture remain!", vbOKOnly, "Cancel")
    25.             End If
    26.         End With
    27.     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
  •  



Click Here to Expand Forum to Full Width