C# DrawString 水平及垂直居中

        public static Bitmap getPictureIMEI(string templatePathName, string imei)
        {
            try
            {
                Bitmap bmp = new Bitmap(templatePathName);
                Graphics g = Graphics.FromImage(bmp);

                Font f=new Font("Arial", 12, FontStyle.Bold);              
                RectangleF rect = new RectangleF(0, (bmp.Height-f.GetHeight(g))/2,bmp.Width,bmp.Height);

                var stringFormat = new StringFormat();
                stringFormat.Alignment = StringAlignment.Center;    //居中字符串【左、右对齐类似】,无法垂直居中,可以通过上面的y
                g.DrawString("IMEI:" + imei, f, Brushes.Black, rect, stringFormat); //写sn字

                bmp.Save("result.bmp", System.Drawing.Imaging.ImageFormat.Bmp); //保存
                return bmp;
            }
            catch (Exception e)
            {
                //MessageBox("错误 合成图片" + e.ToString());
                return null;
            }
        }

你可能感兴趣的:(C#,c#)