Solved

propertygrid file reference problem.

Posted on 2003-11-11
11
610 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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now