好的,我试过这个:
TransformedBitmap tbm = new TransformedBitmap(myBitmapSource, new RotateTransform(angle));
return tbm;
但这不适用于除 90 度的倍数以外的角度。
新我尝试使用 RenderTargetBitmap:
var image = new Canvas();
image.Width = myBitmapSource.PixelWidth;
image.Height = myBitmapSource.PixelHeight;
image.Background = new ImageBrush(myBitmapSource);
image.RenderTransform = new RotateTransform(angle);
RenderTargetBitmap rtb = new RenderTargetBitmap(myBitmapSource.PixelWidth, myBitmapSource.PixelHeight, myBitmapSource.DpiX, myBitmapSource.DpiY, myBitmapSource.Format);
rtb.Render(image);
return rtb;
但这给了我:
"The calling thread must be STA, because many UI components require this."
这在没有 GUI 的服务中运行。
有人可以给我一个关于如何在 WPF(没有 GUI)中以任何角度旋转 BitmapSource 的工作代码示例吗?
更新:
为功能请求投票:http: //visualstudio.uservoice.com/forums/121579-visual-studio-2015/suggestions/10870098-allow-rotation-of-bitmapsource-by-any-angle