【问题标题】:How do I tile and overlay images in WPF?如何在 WPF 中平铺和覆盖图像?
【发布时间】:2010-11-24 11:31:16
【问题描述】:

我对 WPF 非常陌生,正在尝试将应用程序从 VB6 移植到 C# 和 XAML。

我现在需要做的是从许多小图像中创建一个大图像,像一系列“瓷砖”一样排列。其中一些较小的会有叠加层。

在 VB6 中,完成平铺和覆盖只需将 PaintPicture 方法与 PictureBox 控件一起使用即可。

这是我在一步中进行平铺和叠加的尝试(尽管实际上叠加可能会提前发生):

ImageDrawing Drawing1 = new ImageDrawing(new BitmapImage(new Uri(@"c:\one.bmp",
                                          UriKind.Absolute)),
                                         new Rect(0, 0, 40, 130));

ImageDrawing Drawing2 = new ImageDrawing(new BitmapImage(new Uri(@"c:\two.bmp",
                                          UriKind.Absolute)),
                                         new Rect(40, 0, 45, 130));

ImageDrawing Drawing3 = new ImageDrawing(new BitmapImage(new Uri(@"c:\overlay.bmp",
                                          UriKind.Absolute)),
                                         new Rect(40, 0, 45, 130));

DrawingGroup myDrawingGroup = new DrawingGroup();
myDrawingGroup.Children.Add(Drawing1);
myDrawingGroup.Children.Add(Drawing2);
myDrawingGroup.Children.Add(Drawing3);

myImage.Source = new DrawingImage(myDrawingGroup);

平铺工作正常,但覆盖是不行的。我想知道如果

  1. 有人可以指点我实现覆盖的方法和
  2. 有人可以指出这是否是进行平铺的最佳方式。

谢谢!!

【问题讨论】:

    标签: wpf image overlay tiles


    【解决方案1】:

    我在 MSDN 论坛上的一篇帖子中发现了一些东西,它也允许我使用 GDI+ 调用来解决覆盖问题:

    ImageDrawing Drawing1 = new ImageDrawing(new BitmapImage(new Uri(@"c:\one.bmp",
                                                                     UriKind.Absolute)),
                                                             new Rect(0, 0, 40, 130));
    
    ImageDrawing Drawing2 = new ImageDrawing(new BitmapImage(new Uri(@"c:\two.bmp",
                                                                     UriKind.Absolute)), 
                                                             new Rect(40, 0, 45, 130));
    
    Bitmap bitmap = new Bitmap(@"c:\overlay.bmp");
    
    bitmap.MakeTransparent();
    
    ImageDrawing Drawing3 = new ImageDrawing(Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(),
                                                                                   IntPtr.Zero, 
                                                                                   Int32Rect.Empty, 
                                                                                   BitmapSizeOptions.FromEmptyOptions()),
                                             new Rect(40, 0, 45, 130));
    
    DrawingGroup myDrawingGroup = new DrawingGroup();
    myDrawingGroup.Children.Add(Drawing1);
    myDrawingGroup.Children.Add(Drawing2);
    myDrawingGroup.Children.Add(Drawing3);
    
    myImage.Source = new DrawingImage(myDrawingGroup);
    

    虽然这很有效,但让我感到惊讶的是,它是一种特别复杂的达到目的的手段。当然还有更直接的全 WPF 方式!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      • 2021-03-05
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多