2016-11-07 7 views
1

WinformおよびWPF要素のコレクションを含むWPFパネル(WPFアプリケーション内)があります。 Winform要素はWindowsFormsHostでホストされています。WinformおよびWPF要素を含むパネルからBitmapSourceを作成する

私はそれを操作/保存/印刷することができるように、パネル全体のBitmapSourceを作成する必要があります。

WPF要素のBitmapSourceを作成するのに問題はありませんが、Winform要素はレンダリングされません(どこに白いスペースがあるか)。その

一例:

void GetBitmapSource(FrameworkElement element) 
{ 
    var matrix = PresentationSource.FromVisual(element).CompositionTarget.TransformToDevice; 
    double dpiX = 96.0 * matrix.M11; 
    double dpiY = 96.0 * matrix.M22; 
    var bitmapSource = new RenderTargetBitmap((int)element.ActualWidth + 1, (int)element.ActualHeight + 1, dpiX, dpiY, PixelFormats.Pbgra32); 
    bitmapSource.Render(element); 
    return bitmapSource; 
} 

これはうまくWPF要素を印刷するが、Winフォーム内容を無視します。それをどのように含めるのですか?

下の画像は、左側のタブパネルと右側のビットマップソースを示しています。 Winform Rectangleの内容が空であることに注目してください。 Tabpanel on the left and BitmapSource on the right.

+1

多分役立つ:http://stackoverflow.com/q/1756954/1136211 – Clemens

+0

私は彼の答えでトーマス・レベスクから解決策を見つけ、[ここ](http://stackoverflow.com/a/1756976/3064934 )。 – Daltons

答えて

1

フルスクリーンショットを撮ってから要素にカットするのはどうですか?

BitmapSource ScreenShot(int x, int y, int width, int height) 
{ 
    using (var screenBitmap = new Bitmap(width,height,System.Drawing.Imaging.PixelFormat.Format32bppArgb)) 
    { 
     using (var g = Graphics.FromImage(screenBitmap)) 
     { 
      g.CopyFromScreen(x, y, 0, 0, screenBitmap.Size); 
      var result = Imaging.CreateBitmapSourceFromHBitmap(
       screenBitmap.GetHbitmap(), 
       IntPtr.Zero, 
       Int32Rect.Empty, 
       BitmapSizeOptions.FromEmptyOptions()); 
      return result; 
     } 
    } 
} 
+0

これはスポイラーボックスの上部に大きな "NSFW"警告が表示されるはずですが、**動作します**! (私は、Windows Forms HostedコントロールがビジュアルツリーのどこかにあるテンプレートWPFコントロールを印刷しなければなりません。これは、黒または白の四角形を残さない唯一のものです。残念ながら、乞食はチョーザーになれません。) –

関連する問題