Link to home
Start Free TrialLog in
Avatar of igoor
igoor

asked on

propertygrid file reference problem.

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
Avatar of Sijin
Sijin

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.
Avatar of igoor

ASKER

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.


ASKER CERTIFIED SOLUTION
Avatar of Sijin
Sijin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of igoor

ASKER

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?!!

Avatar of igoor

ASKER

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?
Avatar of igoor

ASKER

it just started working.

Thanks again for your help.

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.
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.
Avatar of igoor

ASKER

ah your right.

it is not a 100%.

works 9 times out of 10 approx.
Avatar of igoor

ASKER

managed to sort it now by calling

GC.Collect()

which forces the garbage collection.

Great :) :)