5

コード:

private void Foo(Canvas canvas)
{
    // The content is a bit larger...
    Size size = new Size(canvas.ActualWidth * 1.1, canvas.ActualHeight * 1.2);

    // 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(canvas);

    // Then copy to clipboard
    Clipboard.SetImage(renderBitmap);
}

必要なもの:

背景が透明なキャンバスを画像にレンダリングしてから、クリップボードにコピーします(単純ですか?実際にはそうではありません)

問題 :

貼り付けると、背景が黒い醜い画像になります

解決策1:

canvas.Background = new SolidColorBrush(Colors.White);

いいえ。この厚さは機能しませんcanvas。次の背景は変更されません。 renderBitmap.Render(canvas);

代わりに、タイマーを使用し、WPFに背景を変更する時間を与えてから、そのタイマーのティックイベントでレンダリングする必要があります。それは機能しますが、残念ながら、の内容はcanvasそのサイズよりも大きいです...そのため、白い背景はその一部しかカバーできず、それでも醜い結果になります。(ところで、背景を変更するのに時間がかかる理由を誰かが知っていますか?私はそれをすぐに変更する必要があると思いました)

私は何か間違ったことをしましたか?クリップボードで白い背景の透明な画像を取得するにはどうすればよいですか?

さらに、アルファチャネルをサポートしていないmspaint.exeに貼り付けると、一部のPNG画像の背景が白のままであることに気付きましたが、他の一部は黒になります。

alternative colorたとえば、画像を貼り付ける場所がアルファチャネルをサポートしていない場合に背景として使用されるようなものはありますか?カスタマイズできますか?

今、私は別のものを白いコンテンツでレンダリングしました。それを背景としてrenderBitmapBitmapSourceと組み合わせる方法があれば、問題は解決しましたが、方法がわかりません...

int dWidth = (int)size.Width;
int dHeight = (int)size.Height;
int dStride = dWidth * 4;
byte[] pixels = new byte[dHeight * dStride];
for (int i = 0; i < pixels.Length; i++)
{
    pixels[i] = 0xFF;
}
BitmapSource bg = BitmapSource.Create(
    dWidth,
    dHeight,
    96,
    96,
    PixelFormats.Pbgra32,
    null,
    pixels,
    dStride
);
// Combine bg with renderBitmap
4

2 に答える 2

2

これが私の最後の解決策です、それが同じ問題で他の人を助けることを願っています

// 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);

// Create a white background render bitmap
int dWidth = (int)size.Width;
int dHeight = (int)size.Height;
int dStride = dWidth * 4;
byte[] pixels = new byte[dHeight * dStride];
for (int i = 0; i < pixels.Length; i++)
{
    pixels[i] = 0xFF;
}
BitmapSource bg = BitmapSource.Create(
    dWidth,
    dHeight,
    96,
    96,
    PixelFormats.Pbgra32,
    null,
    pixels,
    dStride
);

// Adding those two render bitmap to the same drawing visual
DrawingVisual dv = new DrawingVisual();
DrawingContext dc = dv.RenderOpen();
dc.DrawImage(bg, new Rect(size));
dc.DrawImage(renderBitmap, new Rect(size));
dc.Close();

// Render the result
RenderTargetBitmap resultBitmap =
    new RenderTargetBitmap(
    (int)size.Width,
    (int)size.Height,
    96d,
    96d,
    PixelFormats.Pbgra32
);
resultBitmap.Render(dv);

// Copy it to clipboard
try
{
    Clipboard.SetImage(resultBitmap);
} catch { ... }
于 2012-05-29T01:50:13.037 に答える
0

小さくて読みやすいソリューションを見つけました。https://social.msdn.microsoft.com/Forums/vstudio/en-US/a6972b7f-5ccb-422d-b203-134ef9f10084/how-toで見つけました。 -capture-entire-usercontrol-image-to-clipboard?forum = wpf

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

// Render a white background into buffer for clipboard to avoid black background on some elements
Rectangle vRect = new Rectangle()
{
    Width = (int)size.Width,
    Height = (int)size.Height,
    Fill = Brushes.White,
};
vRect.Arrange(new Rect(size));
renderBitmap.Render(vRect);

// renderBitmap is now white, so render your object on it
renderBitmap.Render(surface);

// Copy it to clipboard
try
{
    Clipboard.SetImage(resultBitmap);
} catch { ... }
于 2016-05-10T10:47:45.927 に答える