MLyons10

Hi everyone. I was playing around with this, and I'm not sure of the best approach. I have a pictureBox that I load with a local file (C:\, etc...). It isn't an embedded resource or anything. I would like to have the ability to overwrite the image and repopulate the textBox, but it says that the file is in use. I thought that the pictureBox would just load the image into the pictureBox and close the connection to the file, allowing it to be overwritten, but apparently that is not the case. In order to do what I want, do I need to load the image into memory or something first and then populate the pictureBox with the image in memory Or is there an easier way to do this

This is the code that I am using:

this.pictureBox1.Image = Image.FromFile(Environment.CurrentDirectory + @"/Pics/" + this.textBox3.Text + ".jpg");

Any help is greatly appreciated.

Thanks Again,


Re: Visual C# Express Edition Overwrite Picture Question

MLyons10

I just wanted to follow up on this question, because I'm not sure if there is an easier way to do this. It seems a bit unnecessary to load the image into memory, etc. Can I just close the connection to this picture I also have a click event on the pictureBox, which shows a larger picture. I could close this connection as well, if that is possible...

Any help and information is greatly appreciated.

Thanks,




Re: Visual C# Express Edition Overwrite Picture Question

JohnWein

Assign null or a new image to the PictureBox's image property. Until you do the image will have a reference to the image file.





Re: Visual C# Express Edition Overwrite Picture Question

Citizen on the earth

Hi MLyons10,

As I understand from your description, you would like to load a image file to a PictureBox and replace it with another one in the PictureBox at runtime.

My suggestions just as follows:

1. Try to embed image resources into your project and get them at runtime.

Sample codes as follows:

Stream imgStream = null;
Bitmap bmp = null;

// get a reference to the current assembly
Assembly a = Assembly.GetExecutingAssembly();

// get a list of resource names from the manifest
string [] resNames = a.GetManifestResourceNames();

// populate the textbox with information about our resources
// also look for images and put them in our arraylist
txtInfo.Clear();

txtInfo.Text += String.Format("Found {0} resources\r\n", resNames.Length);
txtInfo.Text += "----------\r\n";
foreach(string s in resNames)
{
txtInfo.Text += s + "\r\n";
if(s.EndsWith(".bmp"))
{
// attach to stream to the resource in the manifest
imgStream = a.GetManifestResourceStream(s);
if( !(null==imgStream) )
{
// create a new bitmap from this stream and
// add it to the arraylist
bmp = Bitmap.FromStream( imgStream ) as Bitmap;
if( !(null==bmp) )
{
pics.Add( bmp );
}
bmp = null;
imgStream.Close();
imgStream = null;
}
}
}

Try to check out this article about "Embedding and Using Resources in C#" for details - http://www.devhood.com/tutorials/tutorial_details.aspx tutorial_id=75

2. Try to load an image to the PictureBox according the following samples and check if it is faster for you.

public static void LoadImage(String path, PictureBox box)
// Load image file <path> into picturebox <box>
{
Image image = Bitmap.FromFile(path);
Bitmap thumb = new Bitmap(box.Width, box.Height);
Graphics gr = Graphics.FromImage(thumb);
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
gr.DrawImage(image, new Rectangle(0, 0, thumb.Width, thumb.Height));
box.Image = thumb;
image.Dispose();
gr.Dispose();
}

Check out this thread for details - http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=646583&SiteID=1

Hope this helps,

Citizens on the earth





Re: Visual C# Express Edition Overwrite Picture Question

MLyons10

Thank you very much for your response. As the user is seting the image, I can't really use an embedded resource. For other images in the application I do, and that works fine. My issue is that, say I have a record. Right now, I am able to set an image for this record and the image is saved using a specific name on the local machine. Whenever I open the record, it looks for and loads the image from the local machine. In some instances though, the user may wish to replace the image with another, and this is where I am running into an issue. Presently, the application retains the connection to the image file (Which surprises me, but I dunno), meaning that the file is in use and cannot simply be overwritten. If I load the file into memory, then from memory into the pictureBox, I "should" be able to overwrite the actual file, but I do not know if this would work either. Additionally, I am concerned about a delay in doing it this way if a simpler / better way exists.

I've worked a lot with embedded resources, and understand for the most part how those work. For this example though, I would like to use local files, and there exists a small learning curve that I haven't yet been able to overcome. Everything works fine until I need to overwrite the file. I tried researching this and couldn't find any examples of disposing a connection to a local file either...

Thanks Again,




Re: Visual C# Express Edition Overwrite Picture Question

nobugz

Check this thread for a solution.





Re: Visual C# Express Edition Overwrite Picture Question

MLyons10

Thank you very much for your help. I thought that loading it into memory was an option, but didn't know if it was the best option. I added the last line of code, which populates the pictureBox with the image.

Image img;
using (FileStream fs = new FileStream(Environment.CurrentDirectory + @"/Pics/" + this.textBox3.Text + ".jpg", FileMode.Open))
{
int len = (int)fs.Length;
byte[] buf = new byte[len];
fs.Read(buf, 0, len);
using (MemoryStream ms = new MemoryStream(buf))
img = new Bitmap(ms);
this.pictureBox1.Image = img;
}

When I use this code though, I do have one very strange effect.

When I open the form that shows the preview of the image, this works fine. If I then click on the pictureBox to show the larger preview of the image, this works fine. If I close the large preview and replace the image with a different image using the following code:

//Copy File
File.Copy(filename, Environment.CurrentDirectory + @"/Pics/" + this.textBox3.Text + ".jpg", true);

//Refresh Image
this.pictureBox1.Image = Image.FromFile(Environment.CurrentDirectory + @"/Pics/" + this.textBox3.Text + ".jpg");

It does replace the image with the new image and refresh the pictureBox. If I now click on the pictureBox to show a larger preview of the image, the pictureBox on the following form is blank. If I close the first form and reopen it, now the small preview pictureBox is blank. I need to reopen the application in order to refresh the picture boxes.

I did add the above code to both the first form and the large preview form.

I also tried adding the following line of code to see if this would help, but it did not:

fs.Dispose();

This is a very bizarre issue... I would think at the very least reopening the form would cause the pictureBox to refresh, but that does not appear to be the case. I'm not sure why that would be though.

Any help and information is greatly appreciated.

Thanks Again,





Re: Visual C# Express Edition Overwrite Picture Question

nobugz

You lost me. Why are you copying files Why are you using LoadImage() again instead of using the MemoryStream trick





Re: Visual C# Express Edition Overwrite Picture Question

MLyons10

O.K., sorry...

Let me try again.

Basically, what I need to do is display an image in the pictureBox (And on click of the pictureBox in the larger preview window). This works fine, and worked fine before.

Where my issue exists, is that I would like to have the ability to replace the image with another image (Which means overwriting the one in use). Using my prior code, I could not overwrite the file because it was in use (Which is why I had hoped there was a way of disposing access to that image file). Using the filestream trick that you had suggested, I am able to overwrite the file without a problem, but have the issue with it under some circumstances not showing the image in the pictureBox.

Allow me to give an example. Say you're using a program where you can select and attach an image to a record (Such as an address book). Say the image file is saved on the local machine to prevent the database from growing too large unnecessarily, etc. One day you go into this record and want to change the image associated with this record. Using my prior code, the file would not be able to be overwritten. This seems to me to be a fairly common program function, which I think has a lot to do with why I'm so confused. It doesn't seem like it should be this difficult...

I hope that explains it better. If not, please let me know.

Any help and information is greatly appreciated.

Thanks Again,






Re: Visual C# Express Edition Overwrite Picture Question

nobugz

That didn't really help. I don't get the idea of caching bitmaps locally on the PC to keep the database from growing too large. The idea of a database is that other clients can modify the contents of the tables. How would you ever be able to detect that another client changed the bitmap, forcing you to update you local cached copy What data would you use to update your copy if it is not in the table Even if there is only one client, how would storing a copy of the image on disk in a separate file and not in the database make any difference Your still burning the disk space.