【问题标题】:WPF image control sourceWPF图像控制源
【发布时间】:2015-08-24 08:38:26
【问题描述】:

我试图重新创建一个非常简单的 C# 项目 i WPF 示例,它是一个简单的图像查看器.. 来自 sam 的自学 C#,我设法打开打开文件对话框,但是我如何设置图像WPF 中 image.source 控件的路径?

private void SearchBtn_Click(object sender, RoutedEventArgs e)
{
     Microsoft.Win32.OpenFileDialog openfile = new Microsoft.Win32.OpenFileDialog();
     openfile.DefaultExt = "*.jpg";
     openfile.Filter = "Image Files|*.jpg";
     Nullable<bool> result = openfile.ShowDialog();
     if (result == true)
     {
       //imagebox.Source = openfile.FileName;
     }
}

【问题讨论】:

    标签: c# image wpf-controls


    【解决方案1】:
    imagebox.Source = new BitmapImage(new Uri(openfile.FileName));
    

    【讨论】:

    • 谢谢,你如何设置 Sizemode.zoom 属性?
    【解决方案2】:

    您需要将文件名更改为 URI,然后创建位图图像

    if (File.Exists(openfile.FileName))
    {
     // Create image element to set as icon on the menu element
     BitmapImage bmImage = new BitmapImage();
     bmImage.BeginInit();
     bmImage.UriSource = new Uri(openfile.FileName, UriKind.Absolute);
     bmImage.EndInit();
     // imagebox.Source = bmImage;
    }
    

    【讨论】:

      【解决方案3】:

      您也可以将图片添加为资源,即添加现有项并将图片的Build Action属性更改为Resource

      然后这样引用它

      BitmapImage bitImg = new BitmapImage();
      bitImg.BeginInit();
      bitImg.UriSource = new Uri("./Resource/Images/Bar1.png", UriKind.Relative);
      bitImg.EndInit();
      
      ((Image)sender).Source = bitImg;
      

      这样你就不需要在程序中包含图像,它作为资源捆绑到包中

      【讨论】:

        猜你喜欢
        • 2013-01-11
        • 1970-01-01
        • 2011-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-30
        • 2010-10-01
        相关资源
        最近更新 更多