0

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?

Rogan
  • 47
  • 1
  • 6
  • A solution [there](http://stackoverflow.com/questions/4161359/save-bitmapimage-to-file). Just add "objImage" as output parameter of "SavePhoto()". – Graffito Jan 24 '17 at 22:35

2 Answers2

0

Do not try to store the image, just re-load it from the location given. Store the location in the project's Settings.

enter image description here

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

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • 1
    This solution does not taken into account when the user changes the location of the image locally. Storing the image inside the project directory and load its relative URI is safer – Quan Do Apr 25 '17 at 14:18
0

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.

Community
  • 1
  • 1
Graffito
  • 1,658
  • 1
  • 11
  • 10