最近在做軟件工程課程的作業,其中有一個地方需要用到GDI+繪製圖像並將句柄傳給托盤當作托盤圖標,可是因爲樓主本人的強迫症比較嚴重,所以在測試時發現了一些小問題:
紅線中是托盤區最左側的那個圖標
在底部爲暗色時“41”與其右側“38”看不出任何區別,但當底部用白色時就能看到“41”那個位置的圖標黑邊非常嚴重,這是爲什麼呢?
應該是在與GDI+對圖層複合的處理,就像以前在JAVA Swing中一樣,有透明度(就是半透明)的地方在輸出到圖像上時其顏色會被GDI+處理爲與黑色混合。因此若是默認情況下,字體會有Cleartype效果,使邊緣變得平滑,就是對邊緣的像素進行擴散,用一些有透明度的像素在周圍填充。這樣在將這個bufferedimage導出爲其他帶透明度格式時就會出現上面的情況
爲了避免這個只需要用最次的配置繪製就行:
g.SmoothingMode = SmoothingMode.HighSpeed;
g.CompositingQuality = CompositingQuality.HighSpeed;
g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
下面上整個過程:
private Icon GetImageSourceByText(String Inf)
{
Drawing.Image bufferedimage;
if (ico == IntPtr.Zero)
bufferedimage = new Bitmap(35, 30, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
else
bufferedimage = Bitmap.FromHicon(ico);
Graphics g = Graphics.FromImage(bufferedimage);
g.Clear(Color.FromArgb(0, 255, 255, 255));
g.SmoothingMode = SmoothingMode.HighSpeed;
g.CompositingQuality = CompositingQuality.HighSpeed;
g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
Pen pen = new Pen(Color.FromArgb(255, 255, 255, 255), 2);
g.DrawString(Inf, DisIconFont, pen.Brush, new Drawing.Point(0,0));
ico = (bufferedimage as Bitmap).GetHicon();
g.Dispose();
return Icon.FromHandle(ico);
}