如何使用c#在WPF上的图像控制中显示位图图像

时间:2022-09-02 00:27:09

I want that when I double click on a row in ListView, it should display the Image corresponding to that row. This row also contains the path of the Image.

我希望当我双击ListView中的一行时,它应该显示对应于这一行的图像。这一行还包含映像的路径。

I tried the following but it displays the same Image for all rows because I have given the path for a specific Image:

我尝试了下面的方法,但是它对所有行都显示相同的图像,因为我已经为特定的图像指定了路径:

private void ListViewEmployeeDetails_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    ImageSource imageSource = new BitmapImage(new Uri(@"C:\northwindimages\king.bmp"));
    image1.Source = imageSource;
}

Please suggest something.

请建议什么。

2 个解决方案

#1


1  

They key is to retrieve the row index that was clicked, and get the image URL for that row. Since you say you are clicking on the row, this can be done in a method similar to that below

它们的关键是检索被单击的行索引,并获取该行的图像URL。因为你说你点击了行,这可以用类似下面的方法来完成。

private void ListViewEmployeeDetails_MouseDoubleClick(object sender, MouseButtonEventArgs e) 
    {
        DataRow row = (DataRow)sender; //Get the row that was clicked
        string imageURL = row["imageUrl"].ToString();//Get the img URL for that row
        ImageSource imageSource = new BitmapImage(new Uri(imageURL));
        image1.Source = imageSource; 
    }

Hope this helps

希望这有助于

#2


0  

Suppose that:

假设:

  1. The list you are binding to contains Elephant objects and,
  2. 要绑定到的列表包含大象对象,
  3. You want the image to show Elephant.Picture whenever you double-click an item.
  4. 你想让图像显示大象。当你双击一个项目时,想象一下。

You can set the image from an event handler as follows:

您可以从事件处理程序中设置图像,如下所示:

private void ListViewEmployeeDetails_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
  var viewItem = sender as ListViewItem;
  if(viewItem!=null)
  {
    var elephant = viewItem.DataContext as Elephant;
    image1.Source = elephant.Picture;
  }
}

Note that it is important to only accept double clicks on the ListViewItem.

注意,只接受ListViewItem上的双击非常重要。

The above code assumes that elephant.Picture is of type ImageSource. If it is something else you will have to convert it. For example, if instead Elephant has a string "PicturePath" property, the image1.Source line would change to:

上面的代码假设大象。图片是类型ImageSource。如果是别的东西,你就得把它转换。例如,如果大象有一个字符串“PicturePath”属性,image1。源线将改为:

    image1.Source = new BitmapImage(new Uri(elephant.PicturePath));

#1


1  

They key is to retrieve the row index that was clicked, and get the image URL for that row. Since you say you are clicking on the row, this can be done in a method similar to that below

它们的关键是检索被单击的行索引,并获取该行的图像URL。因为你说你点击了行,这可以用类似下面的方法来完成。

private void ListViewEmployeeDetails_MouseDoubleClick(object sender, MouseButtonEventArgs e) 
    {
        DataRow row = (DataRow)sender; //Get the row that was clicked
        string imageURL = row["imageUrl"].ToString();//Get the img URL for that row
        ImageSource imageSource = new BitmapImage(new Uri(imageURL));
        image1.Source = imageSource; 
    }

Hope this helps

希望这有助于

#2


0  

Suppose that:

假设:

  1. The list you are binding to contains Elephant objects and,
  2. 要绑定到的列表包含大象对象,
  3. You want the image to show Elephant.Picture whenever you double-click an item.
  4. 你想让图像显示大象。当你双击一个项目时,想象一下。

You can set the image from an event handler as follows:

您可以从事件处理程序中设置图像,如下所示:

private void ListViewEmployeeDetails_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
  var viewItem = sender as ListViewItem;
  if(viewItem!=null)
  {
    var elephant = viewItem.DataContext as Elephant;
    image1.Source = elephant.Picture;
  }
}

Note that it is important to only accept double clicks on the ListViewItem.

注意,只接受ListViewItem上的双击非常重要。

The above code assumes that elephant.Picture is of type ImageSource. If it is something else you will have to convert it. For example, if instead Elephant has a string "PicturePath" property, the image1.Source line would change to:

上面的代码假设大象。图片是类型ImageSource。如果是别的东西,你就得把它转换。例如,如果大象有一个字符串“PicturePath”属性,image1。源线将改为:

    image1.Source = new BitmapImage(new Uri(elephant.PicturePath));