Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

propertygrid file reference problem.

Posted on 2003-11-11
11
Medium Priority
?
620 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 2000 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
Technology Partners: 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month9 days, 22 hours left to enroll

927 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