【问题标题】:C# and WPF imageC# 和 WPF 图像
【发布时间】:2016-11-22 17:05:56
【问题描述】:

我正在尝试做一个简单的打开对话框并将图像 jpeg 文件添加到图像对象,我太接近了...我希望,但不确定我缺少什么,任何帮助将不胜感激,得到了 C# 代码和下面的wpf:

private void btn_Open_Click(object sender, RoutedEventArgs e)
{
     OpenFileDialog ofd = new OpenFileDialog();
     ofd.Filter = "Image jpeg(*.jpg)|*.jpg|Image png(*.png)|*.png";
     ofd.DefaultExt = ".jpeg";
     Nullable<bool> result = ofd.ShowDialog();

     Images i = new Images();

     // Process open file dialog box results 
     if (result == true)
     {
         // Open document 

         i.Imagepath = ofd.FileName;

         Cropped.Source = i;

     }  
}

它在 Crooped.Source = i 上显示错误,Cropped 是图像名称,我将更进一步,用户使用鼠标控制将裁剪图像。

下面是 wpf xaml:

<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="291" Margin="26,10,0,0" VerticalAlignment="Top" Width="313">
    <Image x:Name="Cropped" Source="{Binding}" />
</Border>

【问题讨论】:

  • 绑定到数据上下文本身的事实可能是问题的一大根源;您也没有对您创建的 Image 对象做任何事情(并且Image 不是ImageSource

标签: c# wpf image xaml


【解决方案1】:
private void btn_Open_Click(object sender, RoutedEventArgs e)
{
    var ofd = new OpenFileDialog();
    ofd.Filter = "Image jpeg(*.jpg)|*.jpg|Image png(*.png)|*.png";
    ofd.DefaultExt = ".jpeg";

    // Process open file dialog box results 
    if (ofd.ShowDialog() == true)
    {
        Cropped.Source = new BitmapImage(new Uri(ofd.FileName));
    }  
}

【讨论】:

    【解决方案2】:

    试试看:

    if (result == true)
                {
                    Cropped.Source = new BitmapImage(new Uri(ofd.FileName));
                }
    

    【讨论】:

    • 因为你没有解释它为什么起作用。每个答案都应该附带一个简单的解释,说明它为什么会解决问题(附注:我不是反对者)
    • 可能是因为您发布的内容与其他答案中显示的完全相同。
    • 非常感谢您的工作:) 两种解决方案都有效,希望我能给予两个信用:(
    猜你喜欢
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 2022-07-11
    • 2022-01-18
    • 2013-09-29
    • 2016-01-29
    相关资源
    最近更新 更多