473,385 Members | 2,003 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,385 software developers and data experts.

Issue with overwriting a bitmap in VB.NET??? (seems to be locked)

I use a bitmap class new bitmap(filepath)

this should and does load my jpg into memory.

I then want to use mybitmap.save(filepath,imaging.imageformat.jpeg) to save
it; overwriting the original.

If this outfile path is different there is no problem but when it is the
same file it blows up with a generic GDI+error.

So I tried
dim strmOut as new filestream(filepath,filemode.create)
then mybitmap.save(strmOut,imaging.imageformat.jpeg)
strmOut.close

This tells me that it can't open the file because another process has it
open.

Should the bitmap constructor open a file, read it in, close it and release
any locks?

If so why I am having problems trying to overwrite the file?

Any help appreciated.

Thanks,

Shane
Nov 20 '05 #1
9 8377
Cor
Hi Shane,

I am not really getting what you are after, but I think that you can have a
look for the "memorystream", that is for playing with byte areas, but I use
it only for bitmaps.

Cor
I use a bitmap class new bitmap(filepath)
this should and does load my jpg into memory.
I then want to use mybitmap.save(filepath,imaging.imageformat.jpeg) to save it; overwriting the original.
If this outfile path is different there is no problem but when it is the
same file it blows up with a generic GDI+error.

So I tried
dim strmOut as new filestream(filepath,filemode.create)
then mybitmap.save(strmOut,imaging.imageformat.jpeg)
strmOut.close

This tells me that it can't open the file because another process has it
open.

Should the bitmap constructor open a file, read it in, close it and release any locks?

If so why I am having problems trying to overwrite the file?

Any help appreciated.

Nov 20 '05 #2
On 2004-01-30, SStory <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> wrote:
I use a bitmap class new bitmap(filepath)

this should and does load my jpg into memory.

I then want to use mybitmap.save(filepath,imaging.imageformat.jpeg) to save
it; overwriting the original.

If this outfile path is different there is no problem but when it is the
same file it blows up with a generic GDI+error.

So I tried
dim strmOut as new filestream(filepath,filemode.create)
then mybitmap.save(strmOut,imaging.imageformat.jpeg)
strmOut.close

This tells me that it can't open the file because another process has it
open.

Should the bitmap constructor open a file, read it in, close it and release
any locks?

If so why I am having problems trying to overwrite the file?

Any help appreciated.


You might want to open the file and read it into a byte array - then
create a memory stream from the array. Then you can close the file and create
the bitmap from the memory stream....

Dim reader As New FileStream(filepath, FileAccess.Read)
Dim bytes(reader.Length - 1) As Byte

reader.Read(bytes, 0, bytes.Length)
reader.Close()

Dim memory As New MemoryStream(bytes)
Dim bmp As New BitMap(memory)
memory.Close()

....

bmp.Save(filepath, Imaging.ImageFormat.Jpeg)

This is untested - so you may have to play around a bit...

--
Tom Shelton [MVP]
Powered By Gentoo Linux 1.4
"You're very sure of your facts, " he said at last, "I
couldn't trust the thinking of a man who takes the Universe
- if there is one - for granted. "
Nov 20 '05 #3
"SStory" <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> schrieb
I use a bitmap class new bitmap(filepath)

this should and does load my jpg into memory.

I then want to use mybitmap.save(filepath,imaging.imageformat.jpeg)
to save it; overwriting the original.

If this outfile path is different there is no problem but when it is
the same file it blows up with a generic GDI+error.

So I tried
dim strmOut as new filestream(filepath,filemode.create)
then mybitmap.save(strmOut,imaging.imageformat.jpeg)
strmOut.close

This tells me that it can't open the file because another process has
it open.

Should the bitmap constructor open a file, read it in, close it and
release any locks?

If so why I am having problems trying to overwrite the file?


The file is not unlocked. Instanciate from a filestream instead:

dim fs as new filestream(filepath, filemode.open)
bmp = new bitmap(fs)
fs.close

Though, I don't know why you are getting the GDI+ error.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #4
You need to copy the Bitmap( mybitmap) to another NEW Bitmap in-memory and
dispose of the orignal and then save it to the same file name using the new
bitmap.
GDI+ locks the the file you open until you dispose of the original in-memory
copy. Once you do that, you should be able to over-write the old file, using
the same file name.
james

"SStory" <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> wrote in message
news:uD**************@TK2MSFTNGP10.phx.gbl...
I use a bitmap class new bitmap(filepath)

this should and does load my jpg into memory.

I then want to use mybitmap.save(filepath,imaging.imageformat.jpeg) to save it; overwriting the original.

If this outfile path is different there is no problem but when it is the
same file it blows up with a generic GDI+error.

So I tried
dim strmOut as new filestream(filepath,filemode.create)
then mybitmap.save(strmOut,imaging.imageformat.jpeg)
strmOut.close

This tells me that it can't open the file because another process has it
open.

Should the bitmap constructor open a file, read it in, close it and release any locks?

If so why I am having problems trying to overwrite the file?

Any help appreciated.

Thanks,

Shane

Nov 20 '05 #5
* "SStory" <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> scripsit:
I use a bitmap class new bitmap(filepath)

this should and does load my jpg into memory.

I then want to use mybitmap.save(filepath,imaging.imageformat.jpeg) to save
it; overwriting the original.

If this outfile path is different there is no problem but when it is the
same file it blows up with a generic GDI+error.

So I tried
dim strmOut as new filestream(filepath,filemode.create)
then mybitmap.save(strmOut,imaging.imageformat.jpeg)
strmOut.close

This tells me that it can't open the file because another process has it
open.

Should the bitmap constructor open a file, read it in, close it and release
any locks?


Use something like this to load the file:

\\\
Dim fs As FileStream = New FileStream("C:\WINDOWS\Angler.bmp", FileMode.Open)
Dim bmp As Bitmap = New Bitmap(fs)
..
..
..
bmp.Dispose()
fs.Close()
///

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #6
Thanks, James,

This is an interesting idea.

I found out that it was being locked until I got rid of it but didn't think
about a in memory copy. I just made it make a backup of the file delete the
original, save as original filename and delete the backup file.

The in memory idea might be better though.

What do you mean by dispose of? Call .Dispose and set to nothing?
Does that unlock the file?
Or must I delete the file from disk(dispose)?

Thanks,

Shane
"james" <jjames700ReMoVeMe at earthlink dot net> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
You need to copy the Bitmap( mybitmap) to another NEW Bitmap in-memory and
dispose of the orignal and then save it to the same file name using the new bitmap.
GDI+ locks the the file you open until you dispose of the original in-memory copy. Once you do that, you should be able to over-write the old file, using the same file name.
james

"SStory" <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> wrote in message
news:uD**************@TK2MSFTNGP10.phx.gbl...
I use a bitmap class new bitmap(filepath)

this should and does load my jpg into memory.

I then want to use mybitmap.save(filepath,imaging.imageformat.jpeg) to

save
it; overwriting the original.

If this outfile path is different there is no problem but when it is the
same file it blows up with a generic GDI+error.

So I tried
dim strmOut as new filestream(filepath,filemode.create)
then mybitmap.save(strmOut,imaging.imageformat.jpeg)
strmOut.close

This tells me that it can't open the file because another process has it
open.

Should the bitmap constructor open a file, read it in, close it and

release
any locks?

If so why I am having problems trying to overwrite the file?

Any help appreciated.

Thanks,

Shane


Nov 20 '05 #7
Thanks to all for the replies.

Before getting these, I discovered that the Bitmap object must lock the file
and so made a backup file of the original, wrote out the modified bitmap to
the original filename and then deleted the bkp.

Thanks for the ideas of in memory bitmaps that were given. I appreciate
them.

Shane

"SStory" <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> wrote in message
news:uD**************@TK2MSFTNGP10.phx.gbl...
I use a bitmap class new bitmap(filepath)

this should and does load my jpg into memory.

I then want to use mybitmap.save(filepath,imaging.imageformat.jpeg) to save it; overwriting the original.

If this outfile path is different there is no problem but when it is the
same file it blows up with a generic GDI+error.

So I tried
dim strmOut as new filestream(filepath,filemode.create)
then mybitmap.save(strmOut,imaging.imageformat.jpeg)
strmOut.close

This tells me that it can't open the file because another process has it
open.

Should the bitmap constructor open a file, read it in, close it and release any locks?

If so why I am having problems trying to overwrite the file?

Any help appreciated.

Thanks,

Shane

Nov 20 '05 #8
Cor
Hi Shane,

Although I told you to look at the memorystream I would first try the
complete solution you have got from Herfried. (Including the dispose).

Cor
Nov 20 '05 #9
Great!

Thanks I will,

Shane

"Cor" <no*@non.com> wrote in message
news:OG**************@tk2msftngp13.phx.gbl...
Hi Shane,

Although I told you to look at the memorystream I would first try the
complete solution you have got from Herfried. (Including the dispose).

Cor

Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: James Dean | last post by:
At the moment i create a bitmap but i only give the graphics region a cloned part of the entire bitmap. The performance seems to be very good.....much faster than loading the full image. What i...
2
by: alex | last post by:
I hade some problems with file locking and did a simple test using process explorer tool from sysinternals. Bitmap BitmapInWork = new Bitmap(Server.MapPath(fileFullPath)); //at this point file...
2
by: active | last post by:
I find Bitmap.Save works for WMF files but Bitmap.FromFile does not. If I use FromFile on a WMF file that came with VS I get an exception. If I use it on a WMF file created with Bitmap.Save I...
3
by: CSH | last post by:
Hi all, I've run into a small problem opening and saving bitmaps. Concider the following code: Dim oBM as Bitmap Dim cFileName as String ... some code to get the filename etc...
7
by: Dennis | last post by:
I am trying to implement drawing on a bitmap and using bitblt to transfer it to the control graphics object in the paint event. It seems to draw on the bitmap ok but doesn't get transferred to the...
4
by: Qwert | last post by:
Hello, I have a listview with an image list. After dispoing the images and the image list, the bitmap files remain locked: REM Create. objImage = Image.FromFile(strBmp) If...
4
by: Michael C | last post by:
It seems that no matter how I load a bitmap into memory it remains attached to whatever I used to create it. If I obtained the bitmap from a file the file stays locked or if it came from a stream I...
2
by: Michael Maes | last post by:
Hi, We have been using ImageLists in our Projects extensively. Many forms have two ILs with nearly 900 bmp's each. They are configured: 32Bit, Fuchsia, 16x16 (and one 24x24). In VS2003 there...
0
by: Andy_Khosravi | last post by:
I'm having issues with updates being blocked due to some sort of record locking issue. The error does not occur consistently, so I've had a hard time nailing it down. It does happen enough to cause...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.