本文介绍了画线在位图上旋转了一个角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下代码中从png图像的中心到顶部绘制一条线:

I am drawing a line from centre of png image to top in below code:

    private string ProcessImage(string fileIn)
    {
        var sourceImage = System.Drawing.Image.FromFile(fileIn);
        var fileName = Path.GetFileName(fileIn);
        var finalPath = Server.MapPath(@"~/Output/" + fileName);

        int x = sourceImage.Width / 2;
        int y = sourceImage.Height / 2;
        using (var g = Graphics.FromImage(sourceImage))
        {
            g.DrawLine(new Pen(Color.Black, (float)5), new Point(x, 0), new Point(x, y));
        }
        sourceImage.Save(finalPath);

        return @"~/Output/" + fileName;
    }

这很好用,并且我有一条线距图像中心90度.现在我需要的是代替90度垂直线,我想接受用户输入的度数.如果用户输入45度,则线条应与png图像的中心成45度角.

This works fine and I have a line that is 90 degrees from centre of image.Now what I need is instead of 90 degree perpendicular line, I would like to accept the degree from user input. If user enters 45 degree the line should be drawn at 45 degrees from centre of png image.

请指引我正确的方向.

谢谢

Thanks

推荐答案

让我们假设您在float angle中具有所需的角度,您所需要做的就是在绘制线条之前插入以下三行:

Let's assume you have the angle you want in a float angle all you need to do is to insert these three lines before drawing the line:

    g.TranslateTransform(x, y);   // move the origin to the rotation point
    g.RotateTransform(angle);     // rotate
    g.TranslateTransform(-x, -y); // move back

    g.DrawLine(new Pen(Color.Black, (float)5), new Point(x, 0), new Point(x, y));

如果您想在不轮值调用的情况下绘制更多内容,请按g.ResetTranform()

If you want to draw more stuff without the rotation call g.ResetTranform() !

这篇关于画线在位图上旋转了一个角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:13