How do I tile and overlay images in WPF?

青春壹個敷衍的年華 提交于 2019-12-02 03:28:47

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);

While this works, it surprises me as being a particularly convoluted means to an end. Surely there's a more straightforward, all-WPF way!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!