Skip to main content
 首页 » 编程设计

c#之为什么在 Image 上旋转比使用 BitmapEncoder 快得多

2024年01月31日33tuyile006

使用

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

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

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

比 慢得多(在 FlushAsync() 中)- 超过半秒。

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

请您参考如下方法:

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

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


更新:

Is there a way to use the GPU to edit bitmaps? Depends on what you need:

如果你想使用 GPU。您可以使用托管包装器(如 Slim DX/Sharp DX)这将需要很长时间才能获得结果。不要忘记,通过 gpu 重新栅格化图像可能会导致质量下降。

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

看这里: Fast work with Bitmaps in C#

我会为每个角度 (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; 
}