By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,347 Members | 2,091 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,347 IT Pros & Developers. It's quick & easy.

Problem saving image after editing

P: 7
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?

Expand|Select|Wrap|Line Numbers
  1. ImageBitmap = New Bitmap(filename)
  2.  
  3. For x = 0 To ImageBitmap.Width - 1
  4.     For y = 0 To ImageBitmap.Height - 1
  5.         Rbyte = ImageBitmap.GetPixel(x, y).R
  6.         Gbyte = ImageBitmap.GetPixel(x, y).G
  7.         Bbyte = ImageBitmap.GetPixel(x, y).B
  8.  
  9.         '####  some byte manipulation
  10.  
  11.         ImageBitmap.SetPixel(x, y, Color.FromArgb(Rbyte, Gbyte, Bbyte))
  12.     Next
  13. Next
  14. ImageBitmap.Save(filename, Imaging.ImageFormat.Png)  '### This throws an exception
  15.  
  16.  
  17. #### if I change the filename and save as below - everything OK
  18. filename2 = Mid(filename, 1, Len(filename) - 4) & "2.png"
  19. ImageBitmap.Save(filename2, Imaging.ImageFormat.Png)  '### This works perfectly
Nov 10 '16 #1

✓ answered by IronRazer

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.

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private filename As String = "C:\TestFolder\MyLogo.png"
  3.  
  4.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  5.         Dim Rbyte, Gbyte, Bbyte As Byte
  6.  
  7.         Dim ImageBitmap As Bitmap = Nothing
  8.         Using bm As New Bitmap(filename)
  9.             ImageBitmap = New Bitmap(bm)
  10.         End Using
  11.  
  12.         For x As Integer = 0 To ImageBitmap.Width - 1
  13.             For y As Integer = 0 To ImageBitmap.Height - 1
  14.                 Rbyte = ImageBitmap.GetPixel(x, y).R
  15.                 Gbyte = ImageBitmap.GetPixel(x, y).G
  16.                 Bbyte = ImageBitmap.GetPixel(x, y).B
  17.  
  18.                 '####  some byte manipulation
  19.  
  20.                 ImageBitmap.SetPixel(x, y, Color.FromArgb(Rbyte, Gbyte, Bbyte))
  21.             Next
  22.         Next
  23.         ImageBitmap.Save(filename, Imaging.ImageFormat.Png)
  24.         ImageBitmap.Dispose() 'dispose this new Bitmap when it is not needed anymore
  25.  
  26.     End Sub
  27. End Class
  28.  

Share this Question
Share on Google+
5 Replies


Frinavale
Expert Mod 5K+
P: 9,731
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.
Nov 10 '16 #2

P: 7
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........??
Nov 10 '16 #3

IronRazer
P: 82
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.

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private filename As String = "C:\TestFolder\MyLogo.png"
  3.  
  4.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  5.         Dim Rbyte, Gbyte, Bbyte As Byte
  6.  
  7.         Dim ImageBitmap As Bitmap = Nothing
  8.         Using bm As New Bitmap(filename)
  9.             ImageBitmap = New Bitmap(bm)
  10.         End Using
  11.  
  12.         For x As Integer = 0 To ImageBitmap.Width - 1
  13.             For y As Integer = 0 To ImageBitmap.Height - 1
  14.                 Rbyte = ImageBitmap.GetPixel(x, y).R
  15.                 Gbyte = ImageBitmap.GetPixel(x, y).G
  16.                 Bbyte = ImageBitmap.GetPixel(x, y).B
  17.  
  18.                 '####  some byte manipulation
  19.  
  20.                 ImageBitmap.SetPixel(x, y, Color.FromArgb(Rbyte, Gbyte, Bbyte))
  21.             Next
  22.         Next
  23.         ImageBitmap.Save(filename, Imaging.ImageFormat.Png)
  24.         ImageBitmap.Dispose() 'dispose this new Bitmap when it is not needed anymore
  25.  
  26.     End Sub
  27. End Class
  28.  
Nov 10 '16 #4

P: 7
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"?
Nov 11 '16 #5

P: 7
Iron Razor

Using the temp bitmap didn't work BUT io.filestream example DID
Expand|Select|Wrap|Line Numbers
  1.  Using fs As New IO.FileStream(filename, IO.FileMode.Open)
  2.             NormalisedImage = New Bitmap(fs)
  3.  End Using
This worked perfectly
Problem solved - Many Thanks for the advice
Peter
Nov 11 '16 #6

Post your reply

Sign in to post your reply or Sign up for a free account.