
///<summary>

/// 任意角度旋转

///</summary>

///<param name="bmp">原始图Bitmap</param>

///<param name="angle">旋转角度</param>

///<param name="bkColor">背景色</param>

///<returns>输出Bitmap</returns>

publicstatic Bitmap KiRotate(Bitmap bmp, float angle, Color bkColor)

{

int w = bmp.Width +2;

int h = bmp.Height +2;

PixelFormat pf;

if (bkColor == Color.Transparent)

{

pf = PixelFormat.Format32bppArgb;

}

else

{

pf = bmp.PixelFormat;

}

Bitmap tmp =new Bitmap(w, h, pf);

Graphics g = Graphics.FromImage(tmp);

g.Clear(bkColor);

g.DrawImageUnscaled(bmp, 1, 1);

g.Dispose();

GraphicsPath path =new GraphicsPath();

path.AddRectangle(new RectangleF(0f, 0f, w, h));

Matrix mtrx =new Matrix();

mtrx.Rotate(angle);

RectangleF rct = path.GetBounds(mtrx);

Bitmap dst =new Bitmap((int)rct.Width, (int)rct.Height, pf);

g = Graphics.FromImage(dst);

g.Clear(bkColor);

g.TranslateTransform(-rct.X, -rct.Y);

g.RotateTransform(angle);

g.InterpolationMode = InterpolationMode.HighQualityBilinear;

g.DrawImageUnscaled(tmp, 0, 0);

g.Dispose();

tmp.Dispose();

return dst;

}
这张图由如下代码产生。左面的是用DrawString函数直接绘制在Form上的,右面的是在Bitmap对象中绘制好后,DrawImage到窗体上的。能够看到文本显示效果很是不一样。由于我想使用双缓冲,把大量文本先绘制在BitMap上,再让它显示出来,可是两种显示效果不同是没法容忍的。请问,这是为何?怎样让两种方法绘制的文本显示效果如出一辙即便换了字体,两种方法的显示效果仍然不一致。
private void Form1_Paint(object sender, PaintEventArgs e) { bmp = new Bitmap(ClientRectangle.Width, ClientRectangle.Height); Graphics gBmp = Graphics.FromImage(bmp);//内存位图 Graphics gForm = e.Graphics;//Form for (int i = 1; i < 20; i++) { System.Drawing.Font f = new Font("宋体", i, FontStyle.Regular); gForm.DrawString("this is a test", f, Brushes.Black, new PointF(10f, i*Font.Height)); gBmp.DrawString("this is a test", f, Brushes.Black, new PointF(0f, i * Font.Height)); } gForm.DrawImage(bmp, new Point(200, 0));