【问题标题】:WPF Rotate and return BitmapSource by any angleWPF 任意角度旋转并返回 BitmapSource
【发布时间】: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


【解决方案1】:

以下方法从另外两个位图创建一个组合位图,其中第二个围绕它们的共同中心点旋转。

此方法的两个关键部分是计算旋转位图的变换边界,以及两个位图在其共同中心点的对齐方式。

private BitmapSource ComposeImage(
    BitmapSource image1, BitmapSource image2, double rotationAngle)
{
    var rotation = new RotateTransform(rotationAngle);
    var size1 = new Size(image1.PixelWidth, image1.PixelHeight);
    var size2 = new Size(image2.PixelWidth, image2.PixelHeight);
    var center1 = new Vector(size1.Width / 2, size1.Height / 2);
    var center2 = new Vector(size2.Width / 2, size2.Height / 2);
    var rotatedSize = rotation.TransformBounds(new Rect(size2)).Size;
    var totalSize = new Size(
        Math.Max(size1.Width, rotatedSize.Width),
        Math.Max(size1.Height, rotatedSize.Height));
    var center = new Point(totalSize.Width / 2, totalSize.Height / 2);

    rotation.CenterX = center.X;
    rotation.CenterY = center.Y;

    var dv = new DrawingVisual();

    using (var dc = dv.RenderOpen())
    {
        dc.DrawImage(image1, new Rect(center - center1, size1));
        dc.PushTransform(rotation);
        dc.DrawImage(image2, new Rect(center - center2, size2));
    }

    var rtb = new RenderTargetBitmap(
        (int)totalSize.Width, (int)totalSize.Height, 96, 96, PixelFormats.Default);
    rtb.Render(dv);

    return rtb;
}

【讨论】:

    猜你喜欢
    • 2016-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    相关资源
    最近更新 更多