使用BmpBitmapEncoder保存Windows.Media.Drawing黑色图像-如何删除Alpha? [英] Saving Windows.Media.Drawing with BmpBitmapEncoder black image - how to remove alpha?

查看:424
本文介绍了使用BmpBitmapEncoder保存Windows.Media.Drawing黑色图像-如何删除Alpha?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SVG#( http://sharpvectors.codeplex.com/),将SVG文件批量转换为其他格式.转换的SVG图像是没有背景的黑色线条图.我对WPF或System.Windows.Media命名空间的经验很少,所以请问这是一个基本问题.

I'm using SVG# (http://sharpvectors.codeplex.com/), to batch convert SVG files to other formats. The SVG images being converted are black line drawings with no background. I've little experience with WPF or the System.Windows.Media namespace, so please excuse me if this is a basic question.

我正在使用来自SVG#的ImageSvgConverter的修改版本,该版本接受System.Windows.Media.Drawing对象,然后使用System.Windows.Media编码器(BmpBitmapEncoderPngBitmapEncoder等)将其转换以导出到所需的对象文件格式.

I'm using a modified version of the ImageSvgConverter from SVG#, which accepts a System.Windows.Media.Drawing object, which is then converted using a System.Windows.Media encoder (BmpBitmapEncoder, PngBitmapEncoder etc) to export to the desired file format.

当我使用TiffBitmapEncoderorPngBitmapEncoderGifBitmap导出时,将按预期生成图像.生成的图像均具有透明背景.

When I export using either a TiffBitmapEncoderor, PngBitmapEncoder or GifBitmap the images are generated as expected. The generated images all have transparent backgrounds.

但是,当我使用JpegBitmapEncoderBmpBitmapEncoder导出时,所有图像都是黑色的.由于tif,png和gif都具有透明背景,我认为jpg/bmp图像已正确绘制,但是,由于这些文件格式不支持alpha,因此黑色输出是有意义的,因为会解释透明性没什么/黑色.

However, when I export using the JpegBitmapEncoder or BmpBitmapEncoder, all images are coming out black. As the tif, png and gif all have transparent backgrounds, I think that the jpg / bmp images are being drawn correctly, however, as alpha isn't supported in these file formats, having a black output makes sense as the transparency would be interpreted as nothing / black.

我认为这是这些SO帖子中所述的奇怪的bmp黑色从BitmapSource输出-有任何想法吗?当保存位图-C#时,背景会变黑.

I think this is as described in these SO posts Strange bmp black output from BitmapSource - any ideas? , Convert Transparent PNG to JPG with Non-Black Background Color and Background Turns Black When Saving Bitmap - C#.

但是,我看不到将解决方案应用于这些问题的方法.谁能指出我正确的方向?

However, I cannot see a way of applying the solutions is these posts to my problem. Can anyone point me in the right direction?

我尝试将白色的SolidColorBrush应用于DrawingContext的PushOpacityMask方法,但这没什么区别.

I have tried applying a white SolidColorBrush to the DrawingContext's PushOpacityMask method, however, this makes no difference.

真的很感谢任何指针.

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

推荐答案

您可以在实际的drawing对象之前绘制一个适当大小的填充背景"矩形.

You could draw a filled "background" rectangle with an appropriate size prior to the actual drawing object.

using (var drawingContext = drawingVisual.RenderOpen())
{
    drawingContext.DrawRectangle(Brushes.White, null, new Rect(drawingBounds.Size));
    drawingContext.DrawDrawing(drawing);
}

这篇关于使用BmpBitmapEncoder保存Windows.Media.Drawing黑色图像-如何删除Alpha?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆