简体   繁体   English

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

[英]Why is rotation much faster on Image than using BitmapEncoder?

Rotating an Image using 使用旋转图像

image.RenderTransform = new RotateTransform()...

is almost immediate. 几乎是立即的。 On the other hand, using 另一方面,使用

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

is much slower (in the FlushAsync() ) - more than half a second. (在FlushAsync()慢得多-超过半秒。

Why is that? 这是为什么? And is there a way to harness the fast rotation in order to rotate bitmaps? 有没有一种方法可以利用快速旋转来旋转位图?

The first one image.RenderTransform will render the bitmap by using hardware rendering. 第一个图像image.RenderTransform将通过使用硬件渲染来渲染位图。 (GPU) The image isn't rotate but will be displayed rotated/scaled. (GPU)图像不旋转,但将显示为旋转/缩放。 (will access only visible pixels directly from/in the videomemory) (将仅直接从视频内存中/在视频内存中访问可见像素)

The second one will rotate the image itself by the CPU (all pixels). 第二个将由CPU旋转图像本身(所有像素)。 It will create new memory for the result. 它将为结果创建新的内存。 (non-video memory) (非视频内存)


update: 更新:

Is there a way to use the GPU to edit bitmaps? 有没有一种方法可以使用GPU编辑位图? Depends on what you need: 取决于您的需求:

If you want to use a GPU. 如果要使用GPU。 You could use an managed wrapper (like Slim DX/Sharp DX) This will take much time to get results. 您可以使用托管包装器(例如Slim DX / Sharp DX),这将花费大量时间来获得结果。 Don't forget, rerasterizing images via gpu could lead to quality lost. 别忘了,通过gpu重新光栅化图像可能会导致质量下降。

If you want to rotate images only (0, 90, 180, 270)? 如果只想旋转图像(0、90、180、270)? you could use a Bitmap class with de ScanLine0 option. 您可以使用带有de ScanLine0选项的Bitmap类。 (this is to preserve quality and size) and you could create a fast implementation. (这是为了保留质量和大小),您可以创建一个快速的实现。

Look here: Fast work with Bitmaps in C# 看这里: 在C#中快速使用位图

I would create an algoritm foreach angle (0,90,180,270). 我将为每个角度(0,90,180,270)创建一个算法。 Because you don't want to calculate the x, y position for each pixel. 因为您不想为每个像素计算x,y位置。 Something like below.. 像下面这样。


Tip: 小费:

try to lose the multiplies/divides. 尝试失去乘法/除法。

/*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
    }
}

Becomes something like: 变成类似:

/*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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么串行代码比并行代码快得多? - why serial code is much faster than parallel? 为什么字典比列表快这么多? - Why is dictionary so much faster than list? 为什么使用Func &lt;&gt;比在泛型序列创建器上使用new()约束要快得多 - Why is using a Func<> so much faster than using the new() constraint on a generic sequence creator 为什么我在 C# 中的计算比 Python 快得多 - Why is my computation so much faster in C# than Python 为什么C#在AppDomain中调用函数要比VB快得多 - Why is C# much faster at calling a function in an AppDomain than VB 为什么OrderBy返回IOrderedEnumerable <T> 比排序快得多? - Why is OrderBy which returns IOrderedEnumerable<T> much faster than Sort? 为什么后续的直接方法调用比第一次调用快很多? - Why subsequent direct method call is much faster than the first call? 为什么结构比这个特定情况下的类快得多? - Why are structs so much faster than classes for this specific case? 为什么将数组作为文字而不是参数传递这么快? - Why is it so much faster passing an array as a literal rather than a parameter? 为什么将 XAML 图像源设置为 URI 比使用 HttpClient 获取图像更快? - Why is setting a XAML Image Source to URI faster than using HttpClient to get the Image?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM