
///<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));