WPF-将画布保存为jpeg的黑色背景 [英] WPF - Black Background surrounding saved canvas as jpeg

查看:256
本文介绍了WPF-将画布保存为jpeg的黑色背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用JpegBitmapEncoder时遇到困难,因为它正在创建放置在黑色矩形中的图像.而且我没有解决方案.

I'm having difficulty with JpegBitmapEncoder since it is creating an image that is placed in black rectangle. and I don't have the solution for fix.

 private void SaveImage(Canvas canvas, string fileName)
    {
        SaveFileDialog s = new SaveFileDialog();
        s.FileName = "Pic";
        s.DefaultExt = ".jpg";
        s.Filter = "JPG files (.jpg)|*.jpg";

        Nullable<bool> result = s.ShowDialog();
        if (result == true)
        {
            RenderTargetBitmap renderBitmap = new RenderTargetBitmap(6646, 3940, 2000d, 2000d, PixelFormats.Pbgra32);

            canvas.Measure(new Size((int)canvas.Width, (int)canvas.Height));
            canvas.Arrange(new Rect(new Size((int)canvas.Width, (int)canvas.Height)));

            renderBitmap.Render(canvas);

            string filename = s.FileName;
            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(renderBitmap));

            using (FileStream file = File.Create(filename))
            {
                encoder.Save(file);
            }
        }
    }

使用此代码,我得到:

但是当我使用PngBitmap编码器时,这不会发生.谁能发光? 如何删除黑色矩形或通过增加图片的尺寸填充它?

but when i use PngBitmap Encoder this doesn't happen. Can anyone shine a light? How do I remove the black rectangle or perhaps fill it by increasing the dimensions of the picture?

推荐答案

.png支持透明性,而.jpg不支持透明性.我怀疑您的画布背景是透明的,并且由于它不知道如何处理它,因此仅将像素保留为默认值(0,0,0),即黑色. Png默认为(0,0,0,0),为透明黑色.

.png supports transparency, whereas .jpg doesn't. I suspect that the background of your canvas is transparent, and as it doesn't know what to do with it it just leaves the pixels default (0,0,0) i.e. black. Png defaults to (0,0,0,0), which is transparent black.

或者,您的画布与图像的尺寸不同(您使用硬编码的宽度和高度创建图像,而不是使用画布的宽度和高度).较大时,只会渲染画布实际覆盖的部分.

Or, your canvas isn't the same dimensions as the image (your create your image with hardcoded width & height, rather than use the Width and Height of your canvas). As it's larger, it only renders the parts where the canvas actually covers.

如果是第一种情况,请尝试将画布Background ="White"设置为画布,以使其呈现所有透明效果.如果是第二个,请尝试使用适当的Fill ="White"创建图像尺寸的Rectangle,并首先在画布之前渲染该尺寸.像这样:

If it's the first case, try setting your canvas Background="White" to your canvas so it renders all of it with no transparency. If it's the second, try creating a Rectangle the dimensions of your image with the appropriate Fill="White" and rendering that first before your canvas. Something like this:

Rectangle fillBackground = new Rectangle {
    Width = 6646,
    Height = 3940,
    Fill=Brushes.White
}

renderBitmap.Render(fillBackground);
renderBitmap.Render(canvas);

一些建议,您不应真正使用Width和Height,对于布局由其父级决定的控件而言,它们可以是NAN.您实际上应该使用Canvas.ActualWidthCanvas.ActualHeightCanvas.RenderSize.Width/Height.实际上,这实际上会始终反映屏幕大小.

Some advice, you shouldn't really use Width and Height, these can be NAN for a control that's layout is determined by it's parent. You should actually use Canvas.ActualWidth and Canvas.ActualHeight or Canvas.RenderSize.Width/Height. This will actually reflect the on-screen size all the time.

此外,要计算针对DPI选择进行调整的输出图像的大小,您可以使用以下代码:

Also, to calculate the size of your output image adjusting for your DPI choice you can use the following:

RenderTargetBitmap renderBitmap = new RenderTargetBitmap(Width * DPI/96, 
                                                         Height * DPI/96, 
                                                         DPI, DPI, PixelFormats.Pbgra32);

这篇关于WPF-将画布保存为jpeg的黑色背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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