3

現在、1 つの書き込み可能なビットマップ イメージと図面を含むキャンバスがあり、そのイメージをピアに送信したいと考えています。帯域幅を削減するために、キャンバスを書き込み可能なビットマップに変換したいので、両方のイメージを新しい書き込み可能なビットマップにブリットできます。問題は、キャンバスを変換する良い方法が見つからないことです。したがって、キャンバスを書き込み可能なビットマップ クラスに変換する直接的な方法があるかどうかを尋ねたいと思います。

4

1 に答える 1

5

これはこのブログ投稿から取得したものですが、ファイルに書き込む代わりに、WriteableBitmap に書き込みます。

public WriteableBitmap SaveAsWriteableBitmap(Canvas surface)
{
    if (surface == null) return null;

    // Save current canvas transform
    Transform transform = surface.LayoutTransform;
    // reset current transform (in case it is scaled or rotated)
    surface.LayoutTransform = null;

    // Get the size of canvas
    Size size = new Size(surface.ActualWidth, surface.ActualHeight);
    // Measure and arrange the surface
    // VERY IMPORTANT
    surface.Measure(size);
    surface.Arrange(new Rect(size));

    // Create a render bitmap and push the surface to it
    RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
      (int)size.Width, 
      (int)size.Height, 
      96d, 
      96d, 
      PixelFormats.Pbgra32);
    renderBitmap.Render(surface);


    //Restore previously saved layout
    surface.LayoutTransform = transform;

    //create and return a new WriteableBitmap using the RenderTargetBitmap
    return new WriteableBitmap(renderBitmap);

}
于 2013-04-17T02:14:58.597 に答える