Problem saving image after editing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PeterJGibbins
    New Member
    • Oct 2016
    • 7

    Problem saving image after editing

    I have a program which edits image colour values.

    Program works perfectly except that I can only save image to a new filename - I cannot overwrite the original file

    Can anyone help?

    Code:
    ImageBitmap = New Bitmap(filename)
    
    For x = 0 To ImageBitmap.Width - 1
        For y = 0 To ImageBitmap.Height - 1
            Rbyte = ImageBitmap.GetPixel(x, y).R
            Gbyte = ImageBitmap.GetPixel(x, y).G
            Bbyte = ImageBitmap.GetPixel(x, y).B
    
            '####  some byte manipulation
    
            ImageBitmap.SetPixel(x, y, Color.FromArgb(Rbyte, Gbyte, Bbyte))
        Next
    Next
    ImageBitmap.Save(filename, Imaging.ImageFormat.Png)  '### This throws an exception
    
    
    #### if I change the filename and save as below - everything OK
    filename2 = Mid(filename, 1, Len(filename) - 4) & "2.png"
    ImageBitmap.Save(filename2, Imaging.ImageFormat.Png)  '### This works perfectly
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Have you tried adding a check to see if the file exists first (which it should because you read from it)?

    If it exists, try to delete it and recreate it.

    Comment

    • PeterJGibbins
      New Member
      • Oct 2016
      • 7

      #3
      I know the file exists because I have edited it...

      However - you made me think...
      I have tried to delete the file while my program is running and got
      "Action cannot be completed because file is open in vhost32.exe"

      Obviously I can't create a file which already exists and is open..
      but bitmap does not give the option to close a file and save won't overwrite...... ..??

      Comment

      • IronRazer
        New Member
        • Jan 2013
        • 83

        #4
        This happens when you create a new bitmap from an image file because, it does not released the reference to the file until you call the Dispose method of the new Bitmap.

        You can get around it in several ways. One is to create a temporary Bitmap with a Using/End Using statement to open the image file and create the new Bitmap from that Bitmap. The temporary Bitmap will be disposed when the End Using statement is executed which will release all references to the image file.

        Then you can overwrite the original image with the modified Bitmap.

        Code:
        Public Class Form1
            Private filename As String = "C:\TestFolder\MyLogo.png"
        
            Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
                Dim Rbyte, Gbyte, Bbyte As Byte
        
                Dim ImageBitmap As Bitmap = Nothing
                Using bm As New Bitmap(filename)
                    ImageBitmap = New Bitmap(bm)
                End Using
        
                For x As Integer = 0 To ImageBitmap.Width - 1
                    For y As Integer = 0 To ImageBitmap.Height - 1
                        Rbyte = ImageBitmap.GetPixel(x, y).R
                        Gbyte = ImageBitmap.GetPixel(x, y).G
                        Bbyte = ImageBitmap.GetPixel(x, y).B
        
                        '####  some byte manipulation
        
                        ImageBitmap.SetPixel(x, y, Color.FromArgb(Rbyte, Gbyte, Bbyte))
                    Next
                Next
                ImageBitmap.Save(filename, Imaging.ImageFormat.Png)
                ImageBitmap.Dispose() 'dispose this new Bitmap when it is not needed anymore
        
            End Sub
        End Class

        Comment

        • PeterJGibbins
          New Member
          • Oct 2016
          • 7

          #5
          Strange....
          Still no luck.....
          I'd already tried copying to a temp bitmap and using dispose -
          but have tried <Using> <End using> copied from you example - same error

          Checked by trying to access file using windows explorer after image loaded and end using had run (used msgbox as marker)
          file was still open in host32.exe - so not released.

          Any idea on how to force a "Flush"?

          Comment

          • PeterJGibbins
            New Member
            • Oct 2016
            • 7

            #6
            Iron Razor

            Using the temp bitmap didn't work BUT io.filestream example DID
            Code:
             Using fs As New IO.FileStream(filename, IO.FileMode.Open)
                        NormalisedImage = New Bitmap(fs)
             End Using
            This worked perfectly
            Problem solved - Many Thanks for the advice
            Peter
            Last edited by Frinavale; Nov 11 '16, 01:57 PM. Reason: added code tags

            Comment

            Working...