0

This might sound simple but I'v noticed that it's not to me! I want to save a simple jpg or other format image from internet to hard drive. I use the following code to get an image from the net. How do I save it to HDD ?

BitmapImage posterBitmap = new BitmapImage(new Uri(imageLocationOnNet, UriKind.Absolute));

I'd appreciate lot if someone could give me a simple code example and explain how it works.
Preferably in C# and Windows Universal App compatible, thank you.

EDIT. I'm also interested to know if it is possible to convert BitmapImage to JSON and read it back from there?

Antoine
  • 352
  • 1
  • 10
  • 18
Weissu
  • 409
  • 3
  • 15
  • Did you try the sample code that you can find in the official documentation? https://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage(v=vs.110).aspx To send an image using JSON you could convert it to base64. – TOAOGG Aug 09 '16 at 12:17

2 Answers2

0

I'm guessing the reason it doesn't seem simple to you is that you are looking in the wrong place

Bitmap source and Bitmap image are only for loading images, the saving functionality is in the encoder classed such as PngEncoder

once you have selected the correct encoder then you just need to add the image to it as a frame and then save it to a stream

encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(stream);
MikeT
  • 5,398
  • 3
  • 27
  • 43
0

Found here. When you create that image it takes some time to download it. You can check if it's downloading with

posterBitmap.IsDownloading

after it's done, add the handler

posterBitmap.DownloadCompleted += posterBitmap_DownloadCompleted;

and then write the function to save the image

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);
}
Community
  • 1
  • 1
kalbsschnitzel
  • 817
  • 1
  • 8
  • 29
  • I've seen this code before but it doesn't work because the following error(s): Error CS0246 The type or namespace name 'JpegBitmapEncoder' could not be found (are you missing a using directive or an assembly reference?) Error CS0117 'BitmapFrame' does not contain a definition for 'Create' So, what's wrong in my code? Missing some reference or is it possible that code just doesn't be Universal App compatible? This is so confusing... – Weissu Aug 10 '16 at 02:30
  • I just checked ... This code work well on Wpf-Application platform but not on Windows Universal platform. Microsoft has changed api so dramatically and that's why it's not piece of cake to find right solution :-( – Weissu Aug 10 '16 at 04:34
  • As I can see JpegBitmapEncode is in System.Windows.Media.Imaging. This is in PresentationCore.dll. Can you just reference this namespace? – kalbsschnitzel Aug 10 '16 at 12:40