问题
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.
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
- someone could point me towards a means of accomplishing the overlays and
- someone could indicate whether this is the best way to do the tiling.
Thanks!!
回答1:
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!
来源:https://stackoverflow.com/questions/1359582/how-do-i-tile-and-overlay-images-in-wpf