c# 创建斜着写字图片
void Main() { using (var bitmap = new Bitmap(3094, 2136,PixelFormat.Format32bppArgb)) { using (Graphics g = Graphics.FromImage(bitmap)) { var msg = "在线考试模板图片(勿删)"; var font = new Font("微软雅黑", 100); var brush = new SolidBrush(Color.FromArgb(30, Color.Black)); var size = g.MeasureString(msg,font ); g.ResetTransform(); //重置图像 g.TranslateTransform(bitmap.Width / 2, bitmap.Height / 2); //设置旋转中心 g.RotateTransform(53); //旋转53度 顺时针 g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; //指定文本呈现的质量 解决文字锯齿问题 g.Clear(Color.White); g.DrawString(msg, font , brush, 0 - size.Width /2 , 0- size.Height/2); //设置文字、字体、大小、颜色、起始位置 bitmap.Save(@"E:\Desktop\test.TIF", ImageFormat.Tiff); } } }
自动根据图像尺寸倾斜文字:
private Bitmap createEmptyImage(int width, int height) { var bitmap = new Bitmap(width, height); using (Graphics g = Graphics.FromImage(bitmap)) { var msg = "此区域考生未作答"; var brush = new SolidBrush(Color.FromArgb(30, Color.Black)); var fontSize = 100; var font = new Font("微软雅黑", fontSize); var size = g.MeasureString(msg, font); while (size.Width > width && fontSize > 1) { fontSize--; font = new Font("微软雅黑", fontSize); size = g.MeasureString(msg, font); } while (size.Height > height && fontSize > 1) { fontSize--; font = new Font("微软雅黑", fontSize); size = g.MeasureString(msg, font); } g.ResetTransform(); g.TranslateTransform(bitmap.Width / 2, bitmap.Height / 2); g.RotateTransform((int)(Math.Asin(height / Math.Sqrt(Math.Pow(width, 2) + Math.Pow(height, 2))) * 180 / Math.PI)); g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; g.Clear(Color.White); g.DrawString(msg, font, brush, 0 - size.Width / 2, 0 - size.Height / 2); } return bitmap; }
桂棹兮兰桨,击空明兮溯流光。