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? - 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
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. - 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
-
5 1032
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.
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........??
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. - 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
-
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"?
Iron Razor
Using the temp bitmap didn't work BUT io.filestream example DID -
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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....
|
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...
|
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...
|
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...
|
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...
|
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.
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |