Solved

propertygrid file reference problem.

Posted on 2003-11-11
11
613 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 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