【问题标题】:C# - WPF - Set random picture to <Image>C# - WPF - 将随机图片设置为 <Image>
【发布时间】:2017-02-07 10:14:47
【问题描述】:

如何将随机图片设置为 WPF &lt;Image&gt;

在我的代码中,我有 4 张图片。

函数img1_Loaded_1 设置图像来源如下:

问题是我希望 2 张图片有相同的图片。例如,img1 和 img4 将具有相同的图片,而 image2 将具有不同的图片,而 img3 也将具有不同的图片。此刻我是这样制作的,但我想将这张图片动态设置为 img1、img2 等。我还想随机选择这 4 张图片中的哪两张有相同的图片。

有什么想法可以实现吗?

【问题讨论】:

  • 为什么要让生活变得困难,并且有 2 张不同名称的图片有相同的图片?
  • 是的,我可以设置相同图片的 2 张图片的名称,但例如 img1 将始终具有 bitImages[0],我想随机化最后 3 张图片中的哪一张(@987654325 @, img3, img4) 将与img1具有相同的图片
  • 但是为什么同一张图片要存储多次呢?为什么不直接参考原始的,省得自己头疼呢?这就像对同一棵树拍 5 次照片,而不是拍 1 次然后再打印几次......
  • 你在哪里看到我已经多次存储了同一张图片?
  • 您的问题:For example img1 and img4 would have the same picture。 img1 和 img4 有 same 图片。

标签: c# wpf image


【解决方案1】:

已将这些基本代码快速组合在一起,希望能让您走上正轨。该代码应位于 Window_Loaded 或其他类似的高级容器中,因为它只需要调用一次。您可能会做其他事情,例如使用 LINQ 过滤列表(例如,选择图像控件将使源无效)。如果您有任何问题,请告诉我!

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        //Set up lists to track which images / image controls have been used .. image resources and image controls will be removed from the relevant list once used.
        int imageCount = 4;
        IList<string> imageNames = new List<string>(imageCount) { "Image0", "Image1", "Image2", "Image3" };
        IList<Image> imageControls = new List<Image>(imageCount) { img1, img2, img3, img4 };

        //Pick an image resource at random to use as the one that will appear twice.
        Random random = new Random();
        int duplicatedImageIndex = random.Next(0, imageCount - 1);
        Uri duplicatedImageUri = new Uri("/rnd_pic;component/Images/" + imageNames[duplicatedImageIndex] + ".jpg", UriKind.RelativeOrAbsolute);

        //Select a random image control for first instance of image resource to be duplicated, then remove control from list to ensure it's not re-used.
        int firstDuplicatedImageControlIndex = random.Next(0, imageCount - 1);
        imageControls[firstDuplicatedImageControlIndex].Source = new BitmapImage(duplicatedImageUri);
        imageControls.RemoveAt(firstDuplicatedImageControlIndex);

        //Select a random image control for second instance of image resource to be duplicated, then remove control & image resource from relevant list to ensure they're not re-used.
        int secondDuplicatedImageControlIndex = random.Next(0, --imageCount - 1);
        imageControls[secondDuplicatedImageControlIndex].Source = new BitmapImage(duplicatedImageUri);
        imageControls.RemoveAt(secondDuplicatedImageControlIndex);
        imageNames.RemoveAt(duplicatedImageIndex);

        //loop through remaining image controls setting one of the remaining random images, removing control & image resource from relevant list as you go.
        while (imageControls.Any())
        {
            int randomUpperBound = --imageCount - 1;
            int nextRandomImageIndex = random.Next(0, randomUpperBound);
            int imageControlIndex = random.Next(0, randomUpperBound);
            Uri nextImageUri = new Uri("/rnd_pic;component/Images/" + imageNames[nextRandomImageIndex] + ".jpg", UriKind.RelativeOrAbsolute);
            imageControls[imageControlIndex].Source = new BitmapImage(nextImageUri);
            imageControls.RemoveAt(imageControlIndex);
            imageNames.RemoveAt(nextRandomImageIndex);
        }
    }

【讨论】:

  • 谢谢你,它似乎工作,但现在我有一个问题。在按钮单击事件中,我比较像这样的图像源img0.Source == img2.Source,它们是相同的,但没有任何反应
  • 如果我使用我的解决方案设置图像源,则图像源的比较在按钮单击事件中起作用,当我使用您的解决方案时,按钮单击事件不起作用
  • 你能告诉我你想通过比较和按钮点击事件实现什么吗?如果你能粘贴你的代码,那就更好了。
猜你喜欢
  • 2017-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多