473,513 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How To Clear an Image

..NET 2.0

I have an Image on my form and I am trying to clear this image and re-load
it with different image.
Everything works fine, but once I load the image I can not delete this image
until I close the program. How can I clear the image so I can delete it.

Here's my code:

string imgFileName = @"C:\myImage.jpg";
this.pic.Image = Image.FromFile(imgFileName);
//
// clear the image
//
this.pic.Image.Dispose();
this.pic.Image = null;
//
//
//
If I try to delete the image with Windows Explorer before I end the program
I get the following error message

---------------------------
Error Deleting File or Folder
---------------------------
Cannot delete 14036: It is being used by another person or program.

Close any programs that might be using the file and try again.
---------------------------
OK
---------------------------
Thank You
Peter
Jul 18 '06 #1
3 9157
All you did was clear the image variable. That is not related to the image
file on disk.

It's like loading an assembly dynamically. You may be done loading it and
using it, but the executable will have a lock on it until it is done.

I would try manually reading the image as a series of bytes, make sure you
open the stream in readonly mode. Then close it, and create the image from
the bytes (I think there is a method to do that).

"Peter" <pc*****@nospam.nospamwrote in message
news:eB**************@TK2MSFTNGP04.phx.gbl...
.NET 2.0

I have an Image on my form and I am trying to clear this image and re-load
it with different image.
Everything works fine, but once I load the image I can not delete this
image until I close the program. How can I clear the image so I can
delete it.

Here's my code:

string imgFileName = @"C:\myImage.jpg";
this.pic.Image = Image.FromFile(imgFileName);
//
// clear the image
//
this.pic.Image.Dispose();
this.pic.Image = null;
//
//
//
If I try to delete the image with Windows Explorer before I end the
program I get the following error message

---------------------------
Error Deleting File or Folder
---------------------------
Cannot delete 14036: It is being used by another person or program.

Close any programs that might be using the file and try again.
---------------------------
OK
---------------------------
Thank You
Peter

Jul 18 '06 #2
Mel
I had the same problem awhile back and the best way I found was to load it
from a filestream;

using (FileStream fsIn = new FileStream(@"C:\myImage.jpg",, FileMode.Open,
FileAccess.Read, FileShare.Read))
{
pictureBox1.Image = Image.FromStream(fsIn);
}

"Peter" <pc*****@nospam.nospamwrote in message
news:eB**************@TK2MSFTNGP04.phx.gbl...
.NET 2.0

I have an Image on my form and I am trying to clear this image and re-load
it with different image.
Everything works fine, but once I load the image I can not delete this
image until I close the program. How can I clear the image so I can
delete it.

Here's my code:

string imgFileName = @"C:\myImage.jpg";
this.pic.Image = Image.FromFile(imgFileName);
//
// clear the image
//
this.pic.Image.Dispose();
this.pic.Image = null;
//
//
//
If I try to delete the image with Windows Explorer before I end the
program I get the following error message

---------------------------
Error Deleting File or Folder
---------------------------
Cannot delete 14036: It is being used by another person or program.

Close any programs that might be using the file and try again.
---------------------------
OK
---------------------------
Thank You
Peter

Jul 18 '06 #3

"Mel" <Me************@insdirect.comwrote in message
news:up**************@TK2MSFTNGP03.phx.gbl...
>I had the same problem awhile back and the best way I found was to load it
from a filestream;

using (FileStream fsIn = new FileStream(@"C:\myImage.jpg",, FileMode.Open,
FileAccess.Read, FileShare.Read))
{
pictureBox1.Image = Image.FromStream(fsIn);
}

"Peter" <pc*****@nospam.nospamwrote in message
news:eB**************@TK2MSFTNGP04.phx.gbl...
>.NET 2.0

I have an Image on my form and I am trying to clear this image and
re-load it with different image.
Everything works fine, but once I load the image I can not delete this
image until I close the program. How can I clear the image so I can
delete it.

Here's my code:

string imgFileName = @"C:\myImage.jpg";
this.pic.Image = Image.FromFile(imgFileName);
//
// clear the image
//
this.pic.Image.Dispose();
this.pic.Image = null;
//
//
//
If I try to delete the image with Windows Explorer before I end the
program I get the following error message

---------------------------
Error Deleting File or Folder
---------------------------
Cannot delete 14036: It is being used by another person or program.

Close any programs that might be using the file and try again.
---------------------------
OK
---------------------------
Thank You
Peter


Thank you very much!

This works great!
Jul 18 '06 #4

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

Similar topics

4
5712
by: Florian Brucker | last post by:
Hi! I've got a problem with float & clear. Take this example code: <div style=" width:100px; height:100px; background-color:#FF0000; float:left; margin:10px;"></div> <span...
3
2354
by: steflhermitte | last post by:
Dear cpp-ians, I have a class: class mImage { public: //constructors / destructor mImage(unsigned int nrLayers); ~mImage();
0
1380
by: Paul E Collins | last post by:
Hello. I want to display a number of individual blocks of text. Each block has an associated image, which should be displayed on the right and aligned with the top of its block of text, i.e....
2
20863
by: Tomomichi Amano | last post by:
Hello How can I delete (clear) lines that were made useing Graphics.DrawLine() ? Thanks in advance! Have a nice day!
5
9085
by: active | last post by:
In a PictureBox if I want to clear the Image or background do I simply use FromFile("") (just a guess) I did see about FromFile: If the file does not have a valid image format or if GDI+ does...
7
2038
by: moondaddy | last post by:
I'm painting images onto a windows form using this method: e.Graphics.DrawImageUnscaled(m_ItemImage, x, y) every time I select a product. However, some products don't have an image so when a...
1
2019
by: laurakr | last post by:
I am trying to use a clear to get my bottom nav bar below the quote box on the right, but it isn't working. I would like the bottom edge of the quote box to "stick" to the footer nav bar but copy...
3
5941
by: ffrugone | last post by:
I am creating a blog and I have a problem which seems fairly common: Within the content of the posts I sometimes have floated images. The comments bar that follows each post will not clear the...
0
1738
by: williamr4j | last post by:
Very first thing, after preloader, is a blurry image that is supposed to get clear when mouse goes over the image. Problem is, the image that is clear will not, when previewing the movie, move from...
0
7265
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
7171
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...
1
7111
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5692
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,...
1
5095
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...
0
4751
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...
0
3240
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...
0
3228
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.