Solved

propertygrid file reference problem.

Posted on 2003-11-11
11
615 Views
Last Modified: 2010-04-16
I have a windows application that has a propertygrid control and the selected object is a picturebox control.

I set the the Image property to a file on my HD, and the picture box displays the image.

But the propertygrid maintains a reference to the file on the HD, which i don't want as i intend to destroy the image after it is assigned to the picture box.

is there a way around this?



thanks

igoor
0
Comment
Question by:igoor
  • 6
  • 5
11 Comments
 
LVL 3

Expert Comment

by:Sijin
ID: 9721554
Can you be more specific as to how it is maintaining a reference to hefile on the HD?...Shouldn't the property grid just be storing the path to the file?

Are you getting any errors at runtime?..If so then please specify.
0
 

Author Comment

by:igoor
ID: 9721643
on further inspection it is the picture box control that holds the reference.

the error i get when i try and delete the file is "There has been a sharing violation. the source or desitnation file may be in use."

basically what i would like to be able to do, via the propertygrid, is to set the image property of the picturebox control to a snapshot/copy of an image file. so that the image file is no longer required/referenced.

to replicate,

create a window app in .net
add a picturebox control and a propertygrid control
set the propertygrids selectedobject to the picturebox.
run the application.
set the image property in the propertygrid to an image file.
now try delete the image file - cant do it.

i would like to be able to delete the file from the hard drive, but remain in memory in my app.


0
 
LVL 3

Accepted Solution

by:
Sijin earned 500 total points
ID: 9721870
Hi mate,

I suspected that it was the PictureBox that was holding the reference. Well if you stick to the propertyGrid way of setting the Image then this problem will persist. This is because the Image object holds a direct reference to the image file on the disk. The way around is to move the Image completely into memory, for that you will have to use somthing like the MemoryStream.

Here is some code that i wrote to test it out,

//Create a new Image from the file on Disk
                  Image diskImg = new Bitmap(@"c:\test1.jpg");

                  //Create an empty memory stream
                  MemoryStream ms = new MemoryStream();

                  //Save the disk image to the memory stream
                  diskImg.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);

                  //Create a new Image from memory
                  Image memoryImg = new Bitmap(ms);

                  //Dispose of the reference to the image on disk
                  diskImg.Dispose();

                  //Set the pictureBox Image to the image in memory
                  pictureBox1.Image = memoryImg;

Basically i have a form with a pictureBox, I create an image then copy it into memory and then set the Image property of the pictureBox. Now since the image is in memory and i have disposed of the only reference that there was i.e (diskImg) i am free to delete the file on te disk.


The only problem is that this means you cannot directly set the Image property of the pictureBox via the propertyGrid.

I hope the above example illustrates the problem better for you and helps you to make an appropriate decision.

Let me know if you have any doubts.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:igoor
ID: 9722135
thanks for your help.

but i have to use the propertygrid to set the image object.

maybe i could do something in the PropertyValueChanged event of the propertygrid to set the image?!!

0
 

Author Comment

by:igoor
ID: 9722547
i have tried to use the PropertyValueChanged to call some similar to the code you described earlier.

as follows:
private void propertyGrid1_PropertyValueChanged(object s, System.Windows.Forms.PropertyValueChangedEventArgs e) {
                  
  if(e.ChangedItem.Label == "Image"){
    if(pictureBox1.Image !=null){
      Image diskImg = pictureBox1.Image;
      MemoryStream mem = new MemoryStream();
      diskImg.Save(mem,System.Drawing.Imaging.ImageFormat.Bmp);
      Image memImg = new Bitmap(mem);
      this.pictureBox1.Image.Dispose();
      diskImg.Dispose();
      pictureBox1.Image = memImg;
    }
  }
}

but it seems as if the picturebox image isn't getting disposed of.

any ideas?
0
 

Author Comment

by:igoor
ID: 9722604
it just started working.

Thanks again for your help.

0
 
LVL 3

Expert Comment

by:Sijin
ID: 9722781
I think you could use the event handler to switch the image from a disk based one to a memory based one. I am still working on it, will let you know tomorrow morning. It's late over here.
0
 
LVL 3

Expert Comment

by:Sijin
ID: 9722812
Oh...great :) :) Just be sure that it is working all the time, there maybe a timing issue involved with respect to the garbage collector.

Let me know if you have problems, i'll sort them out.
0
 

Author Comment

by:igoor
ID: 9722904
ah your right.

it is not a 100%.

works 9 times out of 10 approx.
0
 

Author Comment

by:igoor
ID: 9723314
managed to sort it now by calling

GC.Collect()

which forces the garbage collection.

0
 
LVL 3

Expert Comment

by:Sijin
ID: 9737547
Great :) :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question