为什么Image上的旋转要比使用BitmapEncoder快得多?

时间:2016-09-05 11:21:14

标签: c# xaml win-universal-app uwp uwp-xaml

使用

旋转图像
image.RenderTransform = new RotateTransform()...

几乎是即时的。 另一方面,使用

bitmapEncoder.BitmapTransform.Rotation = BitmapRotation.Clockwise90Degrees...

更慢(在FlushAsync()中) - 超过半秒。

为什么?有没有办法利用快速旋转来旋转位图?

1 个答案:

答案 0 :(得分:5)

第一个image.RenderTransform将使用硬件渲染呈现位图。 (GPU)图像不旋转但会旋转/缩放显示。 (将仅直接从视频记录中访问可见像素)

第二个将由CPU(所有像素)旋转图像本身。它将为结果创建新的内存。 (非视频内存)

更新

  

有没有办法使用GPU编辑位图?   取决于您的需求:

如果您想使用GPU。您可以使用托管包装器(如Slim DX / Sharp DX)这需要很长时间才能获得结果。不要忘记,通过gpu对图像进行重新编程可能会导致质量丢失。

如果您只想旋转图像(0,90,180,270)?您可以使用带有de ScanLine0选项的Bitmap类。 (这是为了保持质量和尺寸),您可以创建一个快速实现。

看这里: Fast work with Bitmaps in C#

我会创建一个algoritm foreach角度(0,90,180,270)。因为您不想计算每个像素的x,y位置。像下面的东西..

提示:

尝试失去乘法/除法。

/*This time we convert the IntPtr to a ptr*/
byte* scan0 = (byte*)bData.Scan0.ToPointer();

for (int i = 0; i < bData.Height; ++i)
{
    for (int j = 0; j < bData.Width; ++j)
    {
        byte* data = scan0 + i * bData.Stride + j * bitsPerPixel / 8;

        //data is a pointer to the first byte of the 3-byte color data
    }
}

变成类似的东西:

/*This time we convert the IntPtr to a ptr*/
byte* scan0 = (byte*)bData.Scan0.ToPointer();

byte* data = scan0;

int bytesPerPixel = bitsPerPixel / 8;

for (int i = 0; i < bData.Height; ++i)
{
    byte* data2 = data;
    for (int j = 0; j < bData.Width; ++j)
    {
        //data2 is a pointer to the first byte of the 3-byte color data

        data2 += bytesPerPixel;
    }
    data += bData.Stride;
}