在网格中平铺图像

时间:2015-07-14 10:39:54

标签: c# image windows-phone-8.1

如何在Grid容器中多次平铺单个图像(小尺寸),以便Grid显示一个图像而不是多个图像平铺在一起?

我已经看到通过blitting多次复制较小图像来创建单个图像的方法,但该过程在计算上很昂贵。我不想创造更大的形象;我只想多次使用相同的单个图像,以便该过程不需要CPU周期。

如何做到这一点?

更新:似乎没有简单的方法来执行上述操作。因此,作为一种解决方法,如何通过在WP8.1 RT中将多个较小的图像拼接在一起来创建单个较大的图像?

1 个答案:

答案 0 :(得分:1)

在这段代码中,我只实例化了一个位图图像。如果它是记忆保存的话,我不确定。

{
      int n = 5;
      Grid grid = new Grid();
      BitmapImage bitmapImage = new BitmapImage(new Uri("pack://application:,,,/StackOverflowTest;Component/1.jpg"));
      for (int i = 0; i < n; i++)
      {
          grid.ColumnDefinitions.Add(new ColumnDefinition {Width = GridLength.Auto});
          grid.RowDefinitions.Add(new RowDefinition {Height= GridLength.Auto});
      }
      for (int i = 0; i < n; i++)
      {
          for (int j = 0; j < n; j++)
          {                
              Image image = new Image { Source = bitmapImage };
              Grid.SetRow(image, i);
              Grid.SetColumn(image, j);
              grid.Children.Add(image);
          }
      }
      containerOfGrid.Children.Add(grid);
}

修改

我已经检查过了,在我看来,在立即窗口中,源图像没有多次分配。

((System.Windows.Controls.Image)((new System.Linq.SystemCore_EnumerableDebugView(((System.Windows.Controls.Panel)(grid)).Children)).Items[0]))._bitmapSource.GetHashCode()
37855919
((System.Windows.Controls.Image)((new System.Linq.SystemCore_EnumerableDebugView(((System.Windows.Controls.Panel)(grid)).Children)).Items[1]))._bitmapSource.GetHashCode()
37855919
((System.Windows.Controls.Image)((new System.Linq.SystemCore_EnumerableDebugView(((System.Windows.Controls.Panel)(grid)).Children)).Items[0])).GetHashCode()
19914648
((System.Windows.Controls.Image)((new System.Linq.SystemCore_EnumerableDebugView(((System.Windows.Controls.Panel)(grid)).Children)).Items[1])).GetHashCode()
3378500