将文本对齐到System.Drawing.RectangleF中心

时间:2013-07-01 21:40:58

标签: c# system.drawing

我正在尝试创建验证码图像。我正在生成一个随机字符串并以随机角度旋转文本并尝试创建一个字节数组。以下是我的代码段:

Image img = Image.FromFile(@"C:\Images\BackGround.jpg");
RectangleF myRect = new RectangleF(0, 0, width, height);
objGraphics.DrawImage(img, myRect);


Matrix myMatrix = new Matrix();
int i = 0;

StringFormat formatter = new StringFormat();
formatter.Alignment = StringAlignment.Center;

for (i = 0; i <= myString.Length - 1; i++)
{
    myMatrix.Reset();
    int charLenght = myString.Length;
    float x = width / (charLenght + 1) * i;
    float y = height / 30F;

    myMatrix.RotateAt(oRandom.Next(-40, 40), new PointF(x, y));
    objGraphics.Transform = myMatrix;

    objGraphics.DrawString(myString.Substring(i, 1), MyFont, MyFontEmSizes,       MyFontStyles,
                           MySolidBrush, x, Math.Max(width, height) / 50, formatter );

    objGraphics.ResetTransform();
}

每件事情都很好,除了我网页上最后一张图片中的第一个字符穿过矩形的左边框。如何将文本与矩形的中心对齐?

感谢。

0 个答案:

没有答案