473,700 Members | 2,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(f ilepath,imaging .imageformat.jp eg) 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(file path,filemode.c reate)
then mybitmap.save(s trmOut,imaging. imageformat.jpe g)
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 8418
Cor
Hi Shane,

I am not really getting what you are after, but I think that you can have a
look for the "memorystre am", 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(f ilepath,imaging .imageformat.jp eg) 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(file path,filemode.c reate)
then mybitmap.save(s trmOut,imaging. imageformat.jpe g)
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*******@TAKE OUTTHISSPAMBUST ERsofthome.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(f ilepath,imaging .imageformat.jp eg) 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(file path,filemode.c reate)
then mybitmap.save(s trmOut,imaging. imageformat.jpe g)
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(file path, FileAccess.Read )
Dim bytes(reader.Le ngth - 1) As Byte

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

Dim memory As New MemoryStream(by tes)
Dim bmp As New BitMap(memory)
memory.Close()

....

bmp.Save(filepa th, Imaging.ImageFo rmat.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*******@TAKE OUTTHISSPAMBUST ERsofthome.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(f ilepath,imaging .imageformat.jp eg)
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(file path,filemode.c reate)
then mybitmap.save(s trmOut,imaging. imageformat.jpe g)
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(file path, 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*******@TAKE OUTTHISSPAMBUST ERsofthome.net> wrote in message
news:uD******** ******@TK2MSFTN GP10.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(f ilepath,imaging .imageformat.jp eg) 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(file path,filemode.c reate)
then mybitmap.save(s trmOut,imaging. imageformat.jpe g)
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*******@TAKE OUTTHISSPAMBUST ERsofthome.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(f ilepath,imaging .imageformat.jp eg) 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(file path,filemode.c reate)
then mybitmap.save(s trmOut,imaging. imageformat.jpe g)
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" <jjames700ReMoV eMe at earthlink dot net> wrote in message
news:e1******** ******@TK2MSFTN GP10.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*******@TAKE OUTTHISSPAMBUST ERsofthome.net> wrote in message
news:uD******** ******@TK2MSFTN GP10.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(f ilepath,imaging .imageformat.jp eg) 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(file path,filemode.c reate)
then mybitmap.save(s trmOut,imaging. imageformat.jpe g)
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*******@TAKE OUTTHISSPAMBUST ERsofthome.net> wrote in message
news:uD******** ******@TK2MSFTN GP10.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(f ilepath,imaging .imageformat.jp eg) 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(file path,filemode.c reate)
then mybitmap.save(s trmOut,imaging. imageformat.jpe g)
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******** ******@tk2msftn gp13.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
1414
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 wanted to know is....at the moment i create the entire bitmap image from compressed data then only return a part of this bitmap each time the program requires it. Every time the scrollbar moves i return a rectangular part of my full bitmap image. Is...
2
1653
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 referenced by fileFullPath is locked by aspnet worker process BitmapInWork = ResizeImage(BitmapInWork, intTargetWidth,..); //at this point it is not locked anymore, at least process explorer doesnt find any handles. and I can delete the file...
2
6694
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 don't get an exception but it appears the Bitmap is blank. Can anyone share some knowledge on this?
3
8633
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
2022
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 control graphics object in the paint event. Any help would be appreciated. Here is my code: public class as mycontrol Private Declare Auto Function BitBlt Lib "GDI32.DLL" (ByVal hdcDest As IntPtr, ByVal nxDest As Integer, ByVal nyDest As...
4
2497
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 Me.lsvTest.LargeImageList Is Nothing Then Me.lsvTest.LargeImageList = New ImageList() End If
4
3755
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 can't close the stream. I don't see why it is doing this when it loads the full image into memory anyway. I could understand it doing this if the bitmap discards it's data when not needed but it doesn't seem to do this. The only way I've found to...
2
1355
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 was no issue with this. Upgrading to VS2005 however makes it impossible to open those forms in Design-View. We have tested it with a blank project, and yes, even then it does not work.
0
1996
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 major problems for my users though as they are working in this all day. I've been pounding my head against the wall for the last week trying to get this figured out. BACKGROUND I have an Access 03 DB that is split with the BE on a network...
0
8718
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9206
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7802
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5899
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4400
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2383
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2022
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.