Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我应该在WPF应用程序中何时使用PixelFormats.Pbgra32?_C#_Wpf - Fatal编程技术网

C# 我应该在WPF应用程序中何时使用PixelFormats.Pbgra32?

C# 我应该在WPF应用程序中何时使用PixelFormats.Pbgra32?,c#,wpf,C#,Wpf,考虑以下设置:我在磁盘上有一些图像。我无法修改这些图像,因为它们属于另一个项目。我只是读了一下 我需要对它们进行处理,使其在我的应用程序中看起来最佳。我的意思是像素着色处理。 完成处理后,输出图像将用作某种ListView或其他控件中的缩略图 我选择了PixelFormats.Bgra32作为输出格式,因为为其构建像素着色器非常简单。 不过我读的是PixelFormats。Pbgra32是Windows本机使用的,因此速度更快 在图像上使用淡入淡出效果时,速度是否更快?在托管代码中预先倍增通道而

考虑以下设置:我在磁盘上有一些图像。我无法修改这些图像,因为它们属于另一个项目。我只是读了一下

我需要对它们进行处理,使其在我的应用程序中看起来最佳。我的意思是像素着色处理。 完成处理后,输出图像将用作某种ListView或其他控件中的缩略图

我选择了
PixelFormats.Bgra32
作为输出格式,因为为其构建像素着色器非常简单。 不过我读的是PixelFormats。Pbgra32是Windows本机使用的,因此速度更快


在图像上使用淡入淡出效果时,速度是否更快?在托管代码中预先倍增通道而不是将其留给框架有意义吗?

因此,我测试了如何(使用/unsafe进行编译):

并且不要使
像素
功能失效,否则会破坏性能。说到性能,不安全的像素操作似乎比使用安全的
CopyPixels()
/
WritePixels()方法快2到3倍


不,我不会使用
Pbgra32
为位图创建位图或像素着色器。我的意见是:不值得。如果我错了,请纠正我。

监视器没有alpha通道,因此预乘像素格式可以更快,因为不需要后乘。但是,实现淡入效果的着色器将需要做更多的工作来恢复原始RGB值并重新应用新的alpha。准确度也有很大的损失。很难想象它会更好,试试吧。我想你是对的,尽管我已经看过很多次“使用Pbgra32”。我猜这是一个优化,若位图是直接渲染的,但-若WPF只是将所有渲染发送到合成它们的渲染队列,它怎么能直接渲染呢。所有WPF控件都使用32位颜色笔刷,但是,如果笔刷中的alpha字节为0xff,则渲染器可能会决定跳过一步。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace BgraVsPbgra {

    public partial class MainWindow : Window {

        readonly WrapPanel Panel;
        readonly TextBlock Output;
        readonly PixelFormat FN = PixelFormats.Bgra32;
        readonly PixelFormat FP = PixelFormats.Pbgra32;
        readonly List<long> TN = new List<long>();
        readonly List<long> TP = new List<long>();
        DateTime T0;
        long DT => (DateTime.Now - T0).Ticks;
        DateTime Now => DateTime.Now;

        public MainWindow() {
            InitializeComponent();
            SizeToContent = SizeToContent.WidthAndHeight;
            Title = "Bgra32 vs Pbgra Benchmark";
            Panel = new WrapPanel {
                Width = 512,
                Height = 512
            };
            Output = new TextBlock {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                Foreground = new SolidColorBrush(Colors.White)
            };
            (Content as Grid).Children.Add(Panel);
            (Content as Grid).Children.Add(Output);
            Dispatcher.BeginInvoke(new Action(() => Start()), DispatcherPriority.SystemIdle);
        }

        private async void Start() {
            Output.Text += "Bgra32 vs Pbgra32 benchmark started.\r\n\r\n";
            var t0 = DateTime.Now;
            var n = 3;
            var i = 16384;
            for (var p = 1; p <= n; p++) {
                Output.Text += $"\r\nPass {p}/{n}.\r\n\r\n";
                await Benchmark(i / 4, 0.75, true);
                await Benchmark(i / 4, 0.75, false);
                await Benchmark(i / 4, 1, true);
                await Benchmark(i / 4, 1, false);
            }
            var t = (DateTime.Now - t0).TotalSeconds;
            Output.Text += $"\r\nDone in {t:0.0}s.\r\n";
        }

        private async Task Benchmark(int n = 256, double opacity = 1, bool cheat = false) {
            Output.Text += $"Benchmarking with opacity = {opacity}...   ";
            if (cheat) Output.Text += "CHEATING!...   ";
            //Output.Text += "\r\n";
            for (int i = 0; i < n; i++) await Dispatcher.BeginInvoke(new Action(async () => await DrawTestAsync(opacity, cheat)), DispatcherPriority.Send);
            Output.Text += ($"Pbgra32 advantage: {(TN.Average() / TP.Average() * 100d - 100d):0.0}%\r\n");
        }

        async Task DrawTestAsync(double opacity = 1, bool cheat = false) {
            await Dispatcher.BeginInvoke(new Action(() => {
                Panel.Children.Clear();
                for (int i = 0; i < 8; i++) {
                    T0 = Now; Panel.Children.Add(new Image { Source = CreateBitmap(FP, cheat), Width = 128, Height = 128, Opacity = opacity }); TP.Add(DT);
                    T0 = Now; Panel.Children.Add(new Image { Source = CreateBitmap(FN, cheat), Width = 128, Height = 128, Opacity = opacity }); TN.Add(DT);
                }
            }), DispatcherPriority.Send);

        }



        BitmapSource CreateBitmap(PixelFormat pixelFormat, bool cheat = false) {
            var bitmap = new WriteableBitmap(256, 256, 96, 96, pixelFormat, null);
            bitmap.Lock();
            unsafe
            {
                var pixels = (uint*)bitmap.BackBuffer;
                if (pixelFormat == FN || cheat)
                    for (uint y = 0; y < 256; y++) for (uint x = 0; x < 256; x++) pixels[x + y * 256] = 0x80000000u | (x << 16) | y;
                else
                    for (uint y = 0; y < 256; y++) for (uint x = 0; x < 256; x++) pixels[x + y * 256] = 0x80000000u | (x / 2 << 16) | y / 2;
            }
            bitmap.Unlock();
            bitmap.Freeze();
            return bitmap;
        }

    }

}
uint pixel = a << 24 | r * a / 255 << 16 | g * a / 255 << 8 | b * a / 255;