首页 > 解决方案 > 如何从图像获取绑定在源上的图像路径

问题描述

Hi everyone.

我需要从绑定在 Image 上的 Source 获取 Text 并创建一个返回字符串的 Class 以使用此值在 xaml 上加载图像。

出色地...

  1. 我有一个用户控件:
 <UserControl 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:cal="http://www.caliburnproject.org"
             xmlns:Ihor="clr-namespace:ConsoleApp1;assembly=WpfApp14"
             mc:Ignorable="d"
  1. 我有一个带有 ConsoleApp1 命名空间的 WpfApp14.dll
             xmlns:Ihor="clr-namespace:ConsoleApp1;assembly=WpfApp14"
   
  1. 在画布中,我得到了图像:
 <Canvas Name="Canvas">
.
.
.
            <Image x:Name="MoveImageFront" Grid.Column="1" Grid.Row="1" Source="{Binding Path=ActiveGame.FrontImagePath}" Opacity="100" >
.
.
.
 </Canvas>
</UserControl>
  1. 我需要一个读取Source="{Binding Path=ActiveGame.FrontImagePath}"获取高度和宽度并返回图像方向的类,就像这样(只是一个例子):

[我想在 ConsoleApp1 命名空间下将这个类添加到我的 WpfApp14.dll]

        public static string ImgOrientation(Bitmap imagefront)
        {
            string Orientationimg;
            
          if (imagefront.Width = 0)
                Orientationimg = ("Portrait");
                return Orientationimg;

          else
            if (imagefront.Width >= imagefront.Height)
                Orientationimg = ("Landscape");
            else Orientationimg = ("Portrait");
            
            return Orientacionimg;
        }

  1. 然后在 XAML 中,如果返回是纵向,我想显示一个名为 portrait.png 的图像,如果返回是横向,我想显示一个名为 landscape.png 的图像。

提前致谢!

标签: c#wpfxamlmvvm

解决方案


推荐阅读