.net中gdi+繪制的圖形質量很少,原因是gdi+使用的是256色的。
為了提高繪制圖片的質量,可以使用是“Octree“ 算法。“Octree“ 算法允許我們插入自己的算法來量子化我們的圖像?!?/p>
一個好的“顏色量子化”算法“應該考慮在兩個像素顆粒之間填充與這兩個像素顏色相近的過渡顏色,提供更多可視顏色空間。
Morgan Skinner提供了很好的“Octree“ 算法代碼,大家可以下載參考使用。
使用OctreeQuantizer很方便:
public byte[] Draw()
{
System.Drawing.Bitmap image = new System.Drawing.Bitmap(this.imageWidth, this.imageHeight);
Graphics g = Graphics.FromImage(image);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//繪制圖片
this.RenerImage(g);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
//使用octreequantizer清晰化圖片
OctreeQuantizer oqt = new OctreeQuantizer(255, 8);
System.Drawing.Bitmap highImage = oqt.Quantize(image);
highImage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
byte[] buffer = ms.ToArray();
g.Dispose();
image.Dispose();
highImage.Dispose();
return buffer;
}
新聞熱點
疑難解答