Solved

propertygrid file reference problem.

Posted on 2003-11-11
11
616 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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