Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DrawText at an angle in DrawingContext?

So I am using the following code to take an existing image, text from an input form, and then place the text from the input form onto the existing image and save it as a new image:

using (FileStream output = new FileStream(match_outputFile, FileMode.Create))
{
    BitmapImage bitmap = new BitmapImage(new Uri(match_sourceFile, UriKind.Relative));
    DrawingVisual visual = new DrawingVisual();

    using (DrawingContext image = visual.RenderOpen())
    {
        image.DrawImage(bitmap, new Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight));

        buildText(image, "text1", this.text1.Text);
        buildText(image, "text2", this.text2.Text);
        buildText(image, "text3", this.text3.Text);
    }

    RenderTargetBitmap target = new RenderTargetBitmap(bitmap.PixelWidth, bitmap.PixelHeight, bitmap.DpiX, bitmap.DpiY, PixelFormats.Default);
    target.Render(visual);

    BitmapEncoder encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(target));
    encoder.Save(output);
}

As you can see, it calls the following function to draw the text:

private void buildText(DrawingContext image, string settings, string input)
{
    string[] setting = (Properties.Settings.Default[settings].ToString()).Split(',');

    FormattedText text = new FormattedText(
        input,
        System.Globalization.CultureInfo.InvariantCulture,
        FlowDirection.LeftToRight,
        new Typeface(new FontFamily(setting[0]), FontStyles.Normal, FontWeights.Bold, FontStretches.Normal),
        Convert.ToDouble(setting[5]),
        (Brush) new BrushConverter().ConvertFromString(setting[4])
    );
    text.MaxTextWidth = Convert.ToDouble(setting[8]);
    text.MaxTextHeight = Convert.ToDouble(setting[9]);

    Point position = new Point(Convert.ToDouble(setting[7]), Convert.ToDouble(setting[6]));

    switch (setting[2])
    {
        case "center": position.X += (Convert.ToDouble(setting[8]) - text.Width) / 2; break;
        case "right": position.X += Convert.ToDouble(setting[8]) - text.Width; break;
    }

    switch (setting[3])
    {
        case "middle": position.Y += (Convert.ToDouble(setting[9]) - text.Height) / 2; break;
        case "bottom": position.Y += Convert.ToDouble(setting[9]) - text.Height; break;
    }

    image.DrawText(text, position);
}

Now what I need is simple... I need to draw text2 (and ONLY text2) at an ANGLE from the center position. The center position is simple, it would be:

centerX = (setting[8] - setting[7]) / 2;
centerY = (setting[9] - setting[6]) / 2;

So what if I wanted to draw this text, at a -30 degree angle rotated at the center position? Remember I want ONLY text 2 rotated, not the other text, and not the original image source either.

like image 623
Jason Axelrod Avatar asked Feb 20 '23 10:02

Jason Axelrod


1 Answers

You could simply push a RotateTransform onto the DrawingContext before drawing the text. After drawing, pop the transform.

buildText(image, "text1", this.text1.Text);
image.PushTransform(new RotateTransform(angle, centerX, centerY));
buildText(image, "text2", this.text2.Text);
image.Pop();
buildText(image, "text3", this.text3.Text);
like image 55
Clemens Avatar answered Feb 28 '23 08:02

Clemens