日期:2014-05-18 浏览次数:20971 次
public partial class Form1 : Form { Bitmap memBmp = new Bitmap(150, 50); public Form1() { // 准备一个字符串图形 // using (Graphics g = Graphics.FromImage(memBmp)) { g.DrawString("Hello world", new Font("Times New Roman", 16), Brushes.Black, 10, 10); } } protected override void OnPaint(PaintEventArgs e) { using (Graphics g = e.Graphics) { // 原样画图 // g.DrawImageUnscaled(memBmp, 0, 0); // 放大,采用最近点取样,突出失真的效果 // g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; //<--- g.DrawImage(memBmp, new Rectangle(0, 50, 600, 200), new Rectangle(0, 0, 150, 50), GraphicsUnit.Pixel); //<--- } } }
------解决方案--------------------
public Bitmap CreateFontBmp(Font font, string text, float zoom) { if (string.IsNullOrEmpty(text)) return null; Size size = TextRenderer.MeasureText(text, font); Rectangle rect = new Rectangle(0,0,size.Width,size.Height); Bitmap bmp = new Bitmap(size.Width, size.Height); Graphics graphics = Graphics.FromImage(bmp); TextRenderer.DrawText( graphics, text, font, rect, SystemColors.ControlText); if (zoom > 0 && zoom != 1) { int width = (int)Math.Ceiling(size.Width * zoom); int height = (int)Math.Ceiling(size.Height * zoom); Rectangle tmpRect = new Rectangle(0,0,width,height); Bitmap tmpBmp = new Bitmap(width, height); graphics = Graphics.FromImage(tmpBmp); graphics.DrawImage( bmp, tmpRect, rect, GraphicsUnit.Pixel); graphics.Flush(); graphics.Dispose(); bmp.Dispose(); return tmpBmp; } graphics.Flush(); graphics.Dispose(); return bmp; }
------解决方案--------------------
参考:
Graphics g = this.pictureBox1.CreateGraphics(); g.SmoothingMode = Smoothi