2011-02-04 51 views
7

我向RenderTargetBitmap渲染了幾十個視覺效果。每個都在它自己的Rect中呈現。 我想要做的是將從RenderTargetBitmap實例呈現的這些Rect區域中的一個複製到WriteableBitmap的相同區域...快速複製矩形像素或水平。像那樣。如何從RenderTargetBitmap bitblit到WriteableBitmap?

那麼,有沒有辦法將RenderTargetBitmap中的rect以快速方式複製到WriteableBitmap?

在此先感謝。

回答

3

通過複製整個RenderTargetBitmap解決了WriteableBitmap的是這樣的:

protected override void OnRender(DrawingContext drawingContext) 
{ 
    if (ActualWidth == 0 || ActualHeight == 0) return; 
    // Create destination bitmap 
    wb = new WriteableBitmap((int) ActualWidth, (int) ActualHeight, 96, 96, PixelFormats.Pbgra32, null); 
    wb.Lock(); 
    rtb = new RenderTargetBitmap(wb.PixelWidth, wb.PixelHeight, wb.DpiX, wb.DpiY, PixelFormats.Pbgra32); 
    foreach (MyVisual visual in visuals) 
    { 
    visual.Render(rtb); 
    } 

    rtb.CopyPixels(new Int32Rect(0,0, rtb.PixelWidth, rtb.PixelHeight), 
    wb.BackBuffer, 
    wb.BackBufferStride * wb.PixelHeight, wb.BackBufferStride); 

    wb.AddDirtyRect(new Int32Rect(0, 0, (int)ActualWidth, (int)ActualHeight)); 
    wb.Unlock(); 

    drawingContext.DrawImage(wb, new Rect(0, 0, ActualWidth, ActualHeight)); 
}