1

I need to enter an image source from the code in C#. In my previous projects (not in uwp) I would do the following:

imagename.Source=new BitmapImage(new Uri(stringPath));

but in UWP the BitmapImage class seems not to exist.

Is there something similar to use or any solution for this problem?

Servy
  • 202,030
  • 26
  • 332
  • 449

3 Answers3

1

MSDN reckons BitmapImage is still a thing.

See here: MSDN help document

Its usage is a little different though:

<Image Loaded="Image_Loaded"/>

And the C#:

void Image_Loaded(object sender, RoutedEventArgs e)
{
    Image img = sender as Image; 
    BitmapImage bitmapImage = new BitmapImage();
    img.Width = bitmapImage.DecodePixelWidth = 80; 
    // Natural px width of image source.
    // You don't need to set Height; the system maintains aspect ratio, and calculates the other
    // dimension, as long as one dimension measurement is provided.
    bitmapImage.UriSource = new Uri(img.BaseUri,"Images/myimage.png");
}
Clint
  • 6,133
  • 2
  • 27
  • 48
0

There is a BitmapImage API within UWP. Microsoft has a guide for its use too.

This is the code sample taken directly from the guide for the C# segment:

// Create Image Element
Image myImage = new Image();
myImage.Width = 200;

// Create source
BitmapImage myBitmapImage = new BitmapImage();

// BitmapImage.UriSource must be in a BeginInit/EndInit block
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg");

// To save significant application memory, set the DecodePixelWidth or  
// DecodePixelHeight of the BitmapImage value of the image source to the desired 
// height or width of the rendered image. If you don't do this, the application will 
// cache the image as though it were rendered as its normal size rather then just 
// the size that is displayed.
// Note: In order to preserve aspect ratio, set DecodePixelWidth
// or DecodePixelHeight but not both.
myBitmapImage.DecodePixelWidth = 200;
myBitmapImage.EndInit();
//set image source
myImage.Source = myBitmapImage;

And this is the XAML segment:

<!-- Simple image rendering. However, rendering an image this way may not
     result in the best use of application memory. See markup below which
     creates the same end result but using less memory. -->
<Image Width="200" 
Source="C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg"/>

<Image Width="200">
  <Image.Source>
    <!-- To save significant application memory, set the DecodePixelWidth or  
     DecodePixelHeight of the BitmapImage value of the image source to the desired 
     height and width of the rendered image. If you don't do this, the application will 
     cache the image as though it were rendered as its normal size rather then just 
     the size that is displayed. -->
    <!-- Note: In order to preserve aspect ratio, only set either DecodePixelWidth
         or DecodePixelHeight but not both. -->
    <BitmapImage DecodePixelWidth="200"  
     UriSource="C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg" />
  </Image.Source>
</Image>
kayleeFrye_onDeck
  • 6,648
  • 5
  • 69
  • 80
0

Especially if you come from WPF you have to add the using directive

using Windows.UI.Xaml.Media.Imaging;

What was next in going from WPF to UWP is the image source string. It needs the absolute path:

imagename.UriSource = new Uri("ms-appx:/your_path");
rlinner
  • 51
  • 5