473,387 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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?

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.  

5 1032
Frinavale
9,735 Expert Mod 8TB
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
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
83 64KB
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
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
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

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

Similar topics

0
by: mchl gdbt | last post by:
Hi, I have several thousand tiffs generated by application A which are read by application B. I need to remove a few colours from the tiffs and I decided to try with the python imaging library....
10
by: David T. Ashley | last post by:
Hi, I'd like to do some basic graphics. For example, if you look at: http://www.marshallaviationcenter.com the banner graphics at the top that apparently used an image editor with some...
1
by: Dev | last post by:
Dear Friends, I am passing the image name, size (in bytes) and imgaeformat (like jpg or bmp or pdf) through the network. I want display the image into picturebox without saving image...
2
by: Peder Y | last post by:
My code is something like this: --------------- Image img = Image.FromFile("somefile.bmp"); FileStream fStream = new FileStream("someBinaryFile.dat"); BinaryWriter bw = new...
1
by: Martin Horn | last post by:
Hi all, I'm trying to save a picturebox image to a database. This is part of the code I'm using:- <snip> Private drv As DataRowView Private dr As MainDataSet.OrdersRow If...
3
by: Laserson | last post by:
Hi all! I want to write an image editing application and can you give me names of .NET components to edit images, something like ImgX.
2
by: pdh12783 | last post by:
HI All, I am having a problem saving the image in when using this code. The Language i am using is VB.NET 2005 Dim dlgImage As New OpenFileDialog dlgImage.Filter = "Image...
4
by: nma | last post by:
<td width="65" valign="top" width="65" height="44" border="1" vspace="2"><img src="<?php echo $row;?>"></td> <td width="195" class="eventEntryText" ><b><? echo " SHOT : $shotNumber";?><br...
0
by: verian | last post by:
Im new to the forum so hello, its nice to be here. For the past few days iv been messing around with code that i found for using a webcam to take pictures and record videos. I had never worked with...
1
by: JohnDinzeo | last post by:
I have about 6 years of graphic design and CGI experience now. And about 2 months of serious programming experience(C/C++ only). I am very comfortable with programming in C++ as it was something I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.