如何在WPF中平铺和叠加图像? [英] How do I tile and overlay images in WPF?
问题描述
我是WPF的新手并尝试将应用程序从VB6移植到C#和XAML。
I'm very new to WPF and trying to port an application from VB6 to C# and XAML.
我现在需要做的是创建一个大图像许多小的,像一系列瓷砖排列。其中一些较小的叠加将叠加在它们上面。
What I need to do now is create one big image out of a number of small ones, arranged like a series of "tiles." Some of these smaller ones will have overlays superimposed on them.
在VB6中,完成平铺和叠加只需要将PaintPicture方法与PictureBox控件一起使用。
In VB6, accomplishing both the tiling and overlaying would simply be a matter of using the PaintPicture method with the PictureBox control.
这是我尝试平铺和覆盖一步(虽然事实上可能会发生重叠):
This is my attempt at the tiling and overlaying in one step (though really the overlaying could occur beforehand):
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);
平铺工作正常,但叠加是不行的。我想知道是否
The tiling works fine, but the overlay is a no-go. I was wondering if
- 有人可以指出我想要完成覆盖的方法和
- 某人可以表明这是否是最好的平铺方式。
谢谢!!
推荐答案
我在MSDN论坛的帖子中找到了一些东西,允许我使用GDI +调用来解决叠加问题:
I found something in a post on an MSDN forum that allowed me to solve the overlay issue, too, using GDI+ calls:
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的方式!
While this works, it surprises me as being a particularly convoluted means to an end. Surely there's a more straightforward, all-WPF way!
这篇关于如何在WPF中平铺和叠加图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!