5

キャンバスを画像に保存したい。動作しますが、背景色は黒です。色を変更するにはどのように追加する必要がありますか?

私はこのコードを使用します:

Size size = new Size(surface.Width, surface.Height);

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

// Create a file stream for saving image
using (FileStream outStream = new FileStream(filename, FileMode.Create))
{
    BmpBitmapEncoder encoder = new BmpBitmapEncoder();
    // push the rendered bitmap to it
    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
    // save the data to the stream
    encoder.Save(outStream);
}
4

2 に答える 2

5

これを試して

Size size = new Size(surface.Width, surface.Height);

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

DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
    VisualBrush visualBrush = new VisualBrush(surface);
    drawingContext.DrawRectangle(visualBrush, null, 
      new Rect(new Point(), new Size(size.Width, size.Height)));
}

renderBitmap.Render(drawingVisual);

// Create a file stream for saving image
using (FileStream outStream = new FileStream(filename, FileMode.Create))
{
    BmpBitmapEncoder encoder = new BmpBitmapEncoder();
    // push the rendered bitmap to it
    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
    // save the data to the stream
    encoder.Save(outStream);
}
于 2012-05-30T13:38:35.057 に答える
1

の代わりにPixelFormats.DefaultまたはPixelFormats.Bgra32またはを試してください。PixelFormats.Rgb24PixelFormats.Pbgra32

Pは pre-multiplied の略で、各チャンネルがアルファで事前に乗算されていることを前提としています。

MSDN リファレンス

于 2012-05-30T13:22:45.923 に答える