How can I upload an image into the WPF application
How do I store the image uploaded in the local file system so it can remain in the file it is loaded into?
How can I upload an image into the WPF application
How do I store the image uploaded in the local file system so it can remain in the file it is loaded into?
Do not try to store the image, just re-load it from the location given. Store the location in the project's Settings.
For example in that settings tab as we have created a string value called FileName
and we will store it:
Settings.Default.FileName = op.FileName;
Settings.Default.Save();
Then when the app starts we make a call to load it:
if (!String.IsNullOrEmpty(Settings.Default.FileName ))
... load file as you already have...
See
Define the function:
public BitmapImage SavePhoto(string istrImagePath)
{
BitmapImage objImage = new BitmapImage(new Uri(istrImagePath, UriKind.RelativeOrAbsolute));
objImage.DownloadCompleted += objImage_DownloadCompleted;
return objImage ;
}
Then, call it:
imagebox.Source =SavePhoto(op.FileName);
and don't forget to add the "objImage_DownloadCompleted" function code:
private void objImage_DownloadCompleted(object sender, EventArgs e)
{
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
Guid photoID = System.Guid.NewGuid();
String photolocation = photoID.ToString() + ".jpg"; //file name
encoder.Frames.Add(BitmapFrame.Create((BitmapImage)sender));
using (var filestream = new FileStream(photolocation, FileMode.Create))
encoder.Save(filestream);
}
Credit to Chris Baxter: link.