Draw line rotated at an angle over bitmap

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;
    }

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.

Please guide me in the right direction.

Thanks

Answers


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));

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


Need Your Help

ajax call not hitting MVC Action Result

jquery ajax asp.net-mvc asp.net-ajax

I have a ajax call that needs to hit my MVC Action Result.

Dynamic database connection to mongodb or mongoose from nodejs

javascript node.js mongodb express mongoose

I am trying to create a multi-tenant app (saas), where each client has its own database.