0

SVG# ( http://sharpvectors.codeplex.com/ ) を使用して、SVG ファイルを他の形式にバッチ変換しています。変換される SVG 画像は、背景のない黒い線画です。私は WPF または System.Windows.Media 名前空間の経験がほとんどないため、これが基本的な質問である場合はご容赦ください。

私は SVG# の ImageSvgConverter の修正版を使用してい System.Windows.Media.Drawingます。これはオブジェクトを受け入れ、エンコーダー ( など) を使用して変換されSystem.Windows.Media、目的のファイル形式にエクスポートされます。BmpBitmapEncoderPngBitmapEncoder

を使用してエクスポートするとTiffBitmapEncoderorPngBitmapEncoder またはGifBitmap画像が期待どおりに生成されます。生成された画像はすべて透明な背景を持っています。

ただし、JpegBitmapEncoderまたはを使用してエクスポートするとBmpBitmapEncoder、すべての画像が黒くなります。tif、png、gif はすべて透明な背景を持っているため、jpg / bmp 画像は正しく描画されていると思いますが、これらのファイル形式ではアルファがサポートされていないため、透明度が解釈されるため、出力を黒にすることは理にかなっています。何もない/黒として。

これは、これらの SO 投稿で説明されているとおりだと思います。BitmapSource からの奇妙な bmp 黒出力 - アイデアはありますか? 透明な PNG を黒以外の背景色の JPG に変換し、ビットマップを保存すると背景が黒くなります - C# .

ただし、これらの投稿が私の問題に解決策を適用する方法がわかりません。誰かが私を正しい方向に向けることができますか?

DrawingContext の PushOpacityMask メソッドに白い SolidColorBrush を適用しようとしましたが、違いはありません。

本当にありがとうございました。

        private Stream SaveImageFile(Drawing drawing)
    {
        // black output
        BitmapEncoder bitmapEncoder = new BmpBitmapEncoder(); 

        // works
        //bitmapEncoder  = new PngBitmapEncoder();

        // The image parameters...
        Rect drawingBounds = drawing.Bounds;
        int pixelWidth = (int)drawingBounds.Width;
        int pixelHeight = (int)drawingBounds.Height;
        double dpiX = 96;
        double dpiY = 96;

        // The Visual to use as the source of the RenderTargetBitmap.
        DrawingVisual drawingVisual = new DrawingVisual();
        DrawingContext drawingContext = drawingVisual.RenderOpen();

        // makes to difference - still black
        //drawingContext.PushOpacityMask(new SolidColorBrush(System.Windows.Media.Color.FromRgb(255,255,255)));

        drawingContext.DrawDrawing(drawing);
        drawingContext.Close();

        // The BitmapSource that is rendered with a Visual.
        RenderTargetBitmap targetBitmap = new RenderTargetBitmap(pixelWidth, pixelHeight, dpiX, dpiY, PixelFormats.Pbgra32);

        targetBitmap.Render(drawingVisual);

        // Encoding the RenderBitmapTarget as an image file.
        bitmapEncoder.Frames.Add(BitmapFrame.Create(targetBitmap));

        MemoryStream stream = new MemoryStream();
        bitmapEncoder.Save(stream);
        stream.Position = 0;
        return stream;
    }
4

1 に答える 1

2

drawing実際のオブジェクトの前に、適切なサイズで塗りつぶされた「背景」の長方形を描くことができます。

using (var drawingContext = drawingVisual.RenderOpen())
{
    drawingContext.DrawRectangle(Brushes.White, null, new Rect(drawingBounds.Size));
    drawingContext.DrawDrawing(drawing);
}
于 2013-01-16T15:29:46.683 に答える