【发布时间】:2019-03-12 17:48:59
【问题描述】:
嘿,我试过这个:
public static BitmapSource RotateImage(Image b, float angle)
{
BitmapSource rotita = (BitmapSource)b.Source;
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
var transform = new RotateTransform(angle);
drawingContext.PushTransform(transform);
drawingContext.DrawImage(rotita, new Rect(0,0, rotita.PixelWidth, rotita.PixelHeight));
drawingContext.Pop();
}
RenderTargetBitmap bmp = new RenderTargetBitmap(rotita.PixelWidth, rotita.PixelHeight, 96, 96, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);
rotita = bmp;
return rotita;
}
但这不能正常工作。我有这个image at 0 degree,旋转30度后this image。
我怎样才能让图片在旋转后变得完整?
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
drawingContext.DrawImage(back, new Rect(0, 0, imageWidth, imageHeight));
drawingContext.DrawImage(element, new Rect(x,y, elementWidth, elementHeight));
}
RenderTargetBitmap bmp = new RenderTargetBitmap(imageWidth, imageHeight, 96, 96, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);
image.Source = bmp;
元素是旋转后的图像
【问题讨论】:
-
有什么理由不简单地将转换应用于显示位图的 Image 元素的 RenderTransform 或 LayoutTransform 属性?目前尚不清楚“图片在旋转后完成”是什么意思。生成的 RenderTargetBitmap 仍然是轴对齐的矩形像素栅格。当你在一个封闭的轴对齐矩形内旋转一个矩形时,应该清楚的是外部矩形大于内部旋转的矩形。
-
因为我希望将图像绘制在另一个图像上:
-
请编辑您的问题。不要在 cmets 中发布代码。
-
为什么不能把两个 Image 元素放在一起呢?请注意,使用 DrawingVisuals 并不常见。这不是 WPF 通常的使用方式。
-
简而言之,我想将一个旋转的图像覆盖在另一个未旋转的图像上
标签: wpf rotation bitmapsource