473,394 Members | 1,739 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,394 software developers and data experts.

Image saved as File is locked

I have an Image object (i) which I want to write to a file. This does work
but when I later try to do something with this file I get errors, because I
think the file is still 'locked'. I have to restart the application before
the locks are released. I tried it first as:

Image i = ... ; // correctly instantiated to some image
i.Save("image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
// later errors doing something with image.jpg

Then I tried cloning the image before I write to the file, and disposing of
both images after the file is written:

Image i = ... ; // correctly instantiated to some image
Image ii = (Image)i.Clone();
ii.Save("image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
ii.Dispose();
i.Dispose();
// later errors doing something with image.jpg

but STILL I'm getting the errors. I'm not sure if the lock is on the file or
the image it was based on. Anyone know what the problem can be and how to
get round it ?

Jul 20 '06 #1
6 7563
JezB,

Instead of using the Save overload which takes the file, I would create
a FileStream which you open for write access and then pass that to the Save
method. The Save method that takes the name of a file will open the file
exclusively, and until it is disposed, it doesn't let go of the FileStream
which it used to open the file exclusively.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"JezB" <je**@somewhere.comwrote in message
news:ez**************@TK2MSFTNGP03.phx.gbl...
>I have an Image object (i) which I want to write to a file. This does work
but when I later try to do something with this file I get errors, because I
think the file is still 'locked'. I have to restart the application before
the locks are released. I tried it first as:

Image i = ... ; // correctly instantiated to some image
i.Save("image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
// later errors doing something with image.jpg

Then I tried cloning the image before I write to the file, and disposing
of both images after the file is written:

Image i = ... ; // correctly instantiated to some image
Image ii = (Image)i.Clone();
ii.Save("image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
ii.Dispose();
i.Dispose();
// later errors doing something with image.jpg

but STILL I'm getting the errors. I'm not sure if the lock is on the file
or the image it was based on. Anyone know what the problem can be and how
to get round it ?

Jul 20 '06 #2
Thanks for the suggestion. Changed it to:

Image i = ... ; // correctly instantiated to some image
using (FileStream fs = new FileStream("image.jpg",FileMode.Create))
{
i.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
i.Dispose();
fs.Close();
}

but the file still seems to be locked. Anything I've missed?
Jul 20 '06 #3
JezB,

Are you sure you don't have the file open anywhere else? The using
statement is guaranteeing that the FileStream is disposed of properly, and
shouldn't keep the file locked.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"JezB" <je**@somewhere.comwrote in message
news:O2**************@TK2MSFTNGP04.phx.gbl...
Thanks for the suggestion. Changed it to:

Image i = ... ; // correctly instantiated to some image
using (FileStream fs = new FileStream("image.jpg",FileMode.Create))
{
i.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
i.Dispose();
fs.Close();
}

but the file still seems to be locked. Anything I've missed?

Jul 20 '06 #4
Sorry, that may turn out not to be true. The image I was trying this out on
happened to be in PNG format, though I was saving it as JPG !

"JezB" <je**@somewhere.comwrote in message
news:O2**************@TK2MSFTNGP04.phx.gbl...
Thanks for the suggestion. Changed it to:

Image i = ... ; // correctly instantiated to some image
using (FileStream fs = new FileStream("image.jpg",FileMode.Create))
{
i.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
i.Dispose();
fs.Close();
}

but the file still seems to be locked. Anything I've missed?

Jul 20 '06 #5
That was a red herring too. Still getting locked files ... oh dear ....

"JezB" <je**@somewhere.comwrote in message
news:u1**************@TK2MSFTNGP03.phx.gbl...
Sorry, that may turn out not to be true. The image I was trying this out
on happened to be in PNG format, though I was saving it as JPG !

"JezB" <je**@somewhere.comwrote in message
news:O2**************@TK2MSFTNGP04.phx.gbl...
>Thanks for the suggestion. Changed it to:

Image i = ... ; // correctly instantiated to some image
using (FileStream fs = new FileStream("image.jpg",FileMode.Create))
{
i.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
i.Dispose();
fs.Close();
}

but the file still seems to be locked. Anything I've missed?


Jul 20 '06 #6
See the GDI+ FAQ for file locked image info.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"JezB" <je**@somewhere.comwrote in message
news:ez**************@TK2MSFTNGP03.phx.gbl...
>I have an Image object (i) which I want to write to a file. This does work
but when I later try to do something with this file I get errors, because I
think the file is still 'locked'. I have to restart the application before
the locks are released. I tried it first as:

Image i = ... ; // correctly instantiated to some image
i.Save("image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
// later errors doing something with image.jpg

Then I tried cloning the image before I write to the file, and disposing
of both images after the file is written:

Image i = ... ; // correctly instantiated to some image
Image ii = (Image)i.Clone();
ii.Save("image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
ii.Dispose();
i.Dispose();
// later errors doing something with image.jpg

but STILL I'm getting the errors. I'm not sure if the lock is on the file
or the image it was based on. Anyone know what the problem can be and how
to get round it ?

Jul 21 '06 #7

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

Similar topics

3
by: dale zhang | last post by:
Hi, I am trying to read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp The article author is using PictureBox for windows...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
19
by: **Developer** | last post by:
When I get the image from the file the file remains locked so the Delete fails with a "used by another process" So I tried using a clone and disposing the obtained image. But that didn't fix...
3
by: thebhone | last post by:
I am creating a website updater program. Currently working on the image replacement module here. For some reason, after an image (say /images/1.jpg) has been used/opened in the application. I...
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: s.danyal.k | last post by:
Hi All, I have created an application in C# that converts HTML file to MS word documents. The HTML file may also have images , for e.g "<img src='http://www.google.com.pk/images/hp0.gif'></img>"....
8
by: MarkAurit | last post by:
I have to display some images kept into a sql server database (image data type) in an asp.net 1.1 page. Is it possible to use an asp control to display the contents of a byte or a MemoryStream?...
1
by: bharathv6 | last post by:
i need to do is modify the image in memory like resizing the image in memory etc ... with out saving it disk as i have to return back the image with out saving it disk PIL supports the use of...
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
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: 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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.