2012-03-22 71 views
1

我有一個用戶控件,它是我的應用程序的一部分,我將它呈現給一個圖像,但它正在顯示當前正在顯示的尺寸。我想要的是將其渲染爲固定的尺寸,例如500x500,但不會將其用新尺寸呈現給用戶。將UserControl渲染爲具有不同大小的圖像,然後顯示它?

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是另一個控制它的UserControl的大小的孩子。

謝謝你

回答

0

我自己解決了這個問題。你需要做的是調整你想要的圖片尺寸的UserControl的VisualParent大小,將你的UserControl渲染成圖像,並將VisualParent的大小恢復到原來的大小。

 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