简体   繁体   English

uwp 中没有位图图像?

[英]No bitmapimage in uwp?

I need to enter an image source from the code in C#.我需要从 C# 中的代码输入图像源。 In my previous projects (not in uwp) I would do the following:在我以前的项目中(不在 uwp 中),我会执行以下操作:

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

but in UWP the BitmapImage class seems not to exist.但在 UWP 中BitmapImage类似乎不存在。

Is there something similar to use or any solution for this problem?是否有类似的用途或解决此问题的任何解决方案?

MSDN reckons BitmapImage is still a thing. MSDN 认为BitmapImage仍然是一个东西。

See here: MSDN help document请参阅此处: MSDN 帮助文档

Its usage is a little different though:不过它的用法有点不同:

<Image Loaded="Image_Loaded"/>

And the C#:和 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");
}

There is a BitmapImage API within UWP. UWP 中有一个BitmapImage API Microsoft has a guide for its use too.微软也有使用指南

This is the code sample taken directly from the guide for the C# segment:这是直接取自 C# 段指南的代码示例:

// 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:这是 XAML 段:

<!-- 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>

Especially if you come from WPF you have to add the using directive特别是如果您来自 WPF,则必须添加 using 指令

using Windows.UI.Xaml.Media.Imaging;

What was next in going from WPF to UWP is the image source string.从 WPF 到 UWP 的下一步是图像源字符串。 It needs the absolute path:它需要绝对路径:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM