我有一个UserControl,它是我的应用程序的一部分,我将它渲染到一个图像,但它正在呈现当前显示的尺寸。我想要的是将它渲染到固定尺寸,比如说500x500,但没有用新尺寸渲染给用户。
UserControl temp = pane.Content;
RadBitmap radImage = new RadBitmap(temp); // Renders UserControl to Image
PngFormatProvider provider = new PngFormatProvider();
return provider.Export(radImage); // returns the Image as a png encoded Byte Array
注意:我的UserControl是另一个Control的子节点,它决定了我的UserControl的大小。
谢谢
答案 0 :(得分:0)
UserControl userControl = pane.Content;
ContentPresenter visualParent = (VisualTreeHelper.GetParent(userControl) as ContentPresenter);
double oldWidth = visualParent.Width;
double oldHeight = visualParent.Height;
visualParent.Width = BitmapImageWidth;
visualParent.Height = BitmapImageHeight;
visualParent.UpdateLayout(); // This is required! To apply the change in Width and Height
WriteableBitmap bmp = new WriteableBitmap(BitmapImageWidth, BitmapImageHeight);
bmp.Render(userControl, null);
bmp.Invalidate(); // Only once you Invalidate is the Control actually rendered to the bmp
RadBitmap radImage = new RadBitmap(bmp);
visualParent.Width = oldWidth; // Revert back to original size
visualParent.Height = oldHeight; // Revert back to original size
return new PngFormatProvider().Export(radImage); // returns the Image as a png encoded Byte Array