2012-03-22 9 views
1

私は自分のアプリケーションの一部であるUserControlを持っていますが、Imageにレンダリングしていますが、現在表示されている次元でレンダリングされています。私が望むのは、固定寸法にレンダリングして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 
関連する問題