我是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);
平铺工作正常,但叠加是不行的。我想知道是否
谢谢!
答案 0 :(得分: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的方式!