Csharp/C#教程:在位图图像中设置背景颜色分享


在位图图像中设置背景颜色

我想将canvas保存为图像。 它有效,但背景颜色为黑色。 如何添加以改变颜色?

我用这个代码:

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

尝试使用PixelFormats.DefaultPixelFormats.Bgra32PixelFormats.Rgb24而不是PixelFormats.Pbgra32

P代表预乘 – 假设每个通道预先乘以alpha。

MSDN参考

试试这个

上述就是C#学习教程:在位图图像中设置背景颜色分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

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

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/987179.html

(0)
上一篇 2021年12月22日
下一篇 2021年12月22日

精彩推荐