[英]Why is rotation much faster on Image than using BitmapEncoder?
使用旋轉圖像
image.RenderTransform = new RotateTransform()...
幾乎是立即的。 另一方面,使用
bitmapEncoder.BitmapTransform.Rotation = BitmapRotation.Clockwise90Degrees...
(在FlushAsync()
) 要慢得多-超過半秒。
這是為什么? 有沒有一種方法可以利用快速旋轉來旋轉位圖?
第一個圖像image.RenderTransform
將通過使用硬件渲染來渲染位圖。 (GPU)圖像不旋轉,但將顯示為旋轉/縮放。 (將僅直接從視頻內存中/在視頻內存中訪問可見像素)
第二個將由CPU旋轉圖像本身(所有像素)。 它將為結果創建新的內存。 (非視頻內存)
更新:
有沒有一種方法可以使用GPU編輯位圖? 取決於您的需求:
如果要使用GPU。 您可以使用托管包裝器(例如Slim DX / Sharp DX),這將花費大量時間來獲得結果。 別忘了,通過gpu重新光柵化圖像可能會導致質量下降。
如果只想旋轉圖像(0、90、180、270)? 您可以使用帶有de ScanLine0選項的Bitmap類。 (這是為了保留質量和大小),您可以創建一個快速的實現。
看這里: 在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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.