PC Review


Reply
Thread Tools Rate Thread

How To Clear an Image

 
 
Peter
Guest
Posts: n/a
 
      18th Jul 2006
..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


 
Reply With Quote
 
 
 
 
Marina Levit [MVP]
Guest
Posts: n/a
 
      18th Jul 2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> .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
>



 
Reply With Quote
 
Mel
Guest
Posts: n/a
 
      18th Jul 2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> .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
>



 
Reply With Quote
 
Peter
Guest
Posts: n/a
 
      18th Jul 2006

"Mel" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> .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!


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
byte[] to image Please could your clear this for me Cz Microsoft Dot NET Compact Framework 0 3rd Jun 2007 04:55 AM
Look @ Code how can I clear the image;looks like image control retaining the value that it had from the last record that loaded it Incolor Microsoft Access Form Coding 1 22nd Feb 2007 07:19 PM
GDI... how do i clear image.. Supra Microsoft VB .NET 1 19th Jun 2004 02:01 PM
How do I clear an Image active Microsoft VB .NET 5 1st Dec 2003 01:05 AM
Image will not clear desktop Paul Windows XP Help 0 16th Nov 2003 09:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:34 PM.