gpt4 book ai didi

c# - 在 DrawingContext 中以一定角度绘制文本?

转载 作者:太空狗 更新时间:2023-10-30 00:43:10 25 4
gpt4 key购买 nike

所以我使用以下代码从输入表单中获取现有图像和文本,然后将输入表单中的文本放置到现有图像上并将其另存为新图像:

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,而不是其他文本,也不是原始图像源。

最佳答案

你可以简单地 push一个RotateTransform在绘制文本之前放到 DrawingContext 上。画好后,pop转换。

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

关于c# - 在 DrawingContext 中以一定角度绘制文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12067470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com