4

したがって、次のコードを使用して、入力フォームから既存の画像とテキストを取得し、入力フォームからのテキストを既存の画像に配置して、新しい画像として保存しています。

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

ご覧のとおり、次の関数を呼び出してテキストを描画します。

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

今私が必要とするのは簡単です...中央の位置から角度を付けてtext2(およびtext2のみ)を描画する必要があります。中央の位置は単純です。次のようになります。

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

では、このテキストを、中心位置で回転させて -30 度の角度で描画したい場合はどうすればよいでしょうか? 他のテキストではなく、元の画像ソースでもなく、テキスト 2 のみを回転させたいことを覚えておいてください。

4

1 に答える 1

8

テキストを描画する前に、RotateTransformを DrawingContext に単純にプッシュできます。描画後、トランスフォームをポップします。

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);
于 2012-08-22T07:17:36.803 に答える