WPF技术问题:如何将多个RenderTargetBitmap合并为单图并复制到剪贴板
Solution to Combine Multiple RenderTargetBitmap and Copy to Clipboard
Got it, let's solve this problem together! The core issue here is that you need to merge all your individual RenderTargetBitmap instances into a single composite image before copying to the clipboard—this way you'll get all elements in one paste instead of just the last one. Here's a step-by-step approach and modified code:
Step 1: Calculate the Total Size of the Combined Image
First, decide how you want to arrange your elements (vertical stack, horizontal row, etc.). For this example, we'll use a vertical stack:
- Total width = maximum width of all elements (so narrower elements are centered)
- Total height = sum of all elements' heights
Step 2: Create a Composite RenderTargetBitmap
Make a new large bitmap that can hold all your individual images.
Step 3: Draw All Individual Images onto the Composite Bitmap
Use a DrawingVisual to render each image at the correct position on the large bitmap.
Modified Full Code
public static void CopyManyUiElementToClipboard(List<FrameworkElement> elements) { var images = new List<RenderTargetBitmap>(); // First, render each element to its own RenderTargetBitmap (your existing logic) elements.ForEach(element => { double width = element.ActualWidth; double height = element.ActualHeight; var size = new Size(width, height); var drawingContext = new DrawingVisual(); using (DrawingContext draw = drawingContext.RenderOpen()) { var visualBrush = new VisualBrush(element); draw.DrawRectangle(visualBrush, null, new Rect(new Point(), size)); } var renderBitmap = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96d, 96d, PixelFormats.Pbgra32); var vRect = new Rectangle { Width = (int)size.Width, Height = (int)size.Height, Fill = Brushes.White }; vRect.Arrange(new Rect(size)); renderBitmap.Render(vRect); renderBitmap.Render(drawingContext); images.Add(renderBitmap); }); // Now combine all images into one if (images.Count == 0) return; // Calculate total dimensions (vertical stack example) double totalWidth = images.Max(img => img.Width); double totalHeight = images.Sum(img => img.Height); // Create the combined bitmap var combinedBitmap = new RenderTargetBitmap((int)totalWidth, (int)totalHeight, 96d, 96d, PixelFormats.Pbgra32); // Draw all individual images onto the combined bitmap var compositeVisual = new DrawingVisual(); using (DrawingContext dc = compositeVisual.RenderOpen()) { double currentYPosition = 0; foreach (var img in images) { // Center narrower elements horizontally double horizontalOffset = (totalWidth - img.Width) / 2; dc.DrawImage(img, new Rect(horizontalOffset, currentYPosition, img.Width, img.Height)); currentYPosition += img.Height; } } combinedBitmap.Render(compositeVisual); // Copy the combined image to clipboard Clipboard.SetImage(combinedBitmap); }
Customization Tips
- Horizontal Arrangement: If you want to stack elements side-by-side instead, change the dimension calculations:
Then trackdouble totalHeight = images.Max(img => img.Height); double totalWidth = images.Sum(img => img.Width);currentXPositioninstead ofcurrentYPositionand update theRectaccordingly. - Add Spacing: To add gaps between elements, just adjust the position increment (e.g.,
currentYPosition += img.Height + 10;for 10px vertical spacing). - Custom Background: If you want a non-white background for the composite image, draw a full-size rectangle first in the
DrawingContext:dc.DrawRectangle(Brushes.LightGray, null, new Rect(0, 0, totalWidth, totalHeight));
内容的提问来源于stack exchange,提问作者Wojciech Szabowicz
相关产品推荐
相关产品推荐

