WPF:如何将 BitmapSource 旋转任意角度
WPF: How to rotate a BitmapSource by any angle
好的,我试过了:
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 的工作代码示例吗?
更新:
好吧,您可以 运行 在单独的线程中编写该代码。只需设置单线程单元 (STA)。
Thread thread = new Thread(DoTheRotation);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
线程调用的方法中的旋转代码:
public void DoTheRotation()
{
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);
}
那你只需要改一下代码就可以传对象了
只需使用 TransformedBitmap
var rotatedImage = new TransformedBitmap(image, new RotateTransform(180));
请注意,TransformedBitmap 仅支持正交变换,例如 90° 增量的旋转变换和缩放变换。不支持扭曲图像的变换。所以对于旋转你只能使用90、180、270度!正如原问题中已经提到的!
好的,我试过了:
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 的工作代码示例吗?
更新:
好吧,您可以 运行 在单独的线程中编写该代码。只需设置单线程单元 (STA)。
Thread thread = new Thread(DoTheRotation);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
线程调用的方法中的旋转代码:
public void DoTheRotation()
{
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);
}
那你只需要改一下代码就可以传对象了
只需使用 TransformedBitmap
var rotatedImage = new TransformedBitmap(image, new RotateTransform(180));
请注意,TransformedBitmap 仅支持正交变换,例如 90° 增量的旋转变换和缩放变换。不支持扭曲图像的变换。所以对于旋转你只能使用90、180、270度!正如原问题中已经提到的!