I have some code that creates a graph using the Bitmap class. The quality of the produced bitmap, however, is pretty poor, how can I improve the quality of this? Note that I am using the Compact Framework!
Here is a few snippets of how I do various things:
Create a bitmap with a background based on an existing jpg
Bitmap bitmap = new Bitmap(filename);
I then create a Graphics class from the bitmap
Graphics myG = Graphics.FromImage(bitmap);
I then draw to the graphics like so:
myG.DrawLine(majorAxisPen, graphRect.Left, graphRect.Bottom, graphRect.Right, graphRect.Bottom);
After drawing everything I need to I then save out the Bitmap like so:
bitmap.Save(fileDestination, System.Drawing.Imaging.ImageFormat.Jpeg);
Here is the image it produces

There are two sources of “low quality” you may encounter.
Every
Graphicsobject has a number of properties that control the “quality” of the operations you can perform on the object. In particular, you should setSmoothingModetoHighQualityto avoid jaggies on line drawings. Unsupported in .NET Compact Framework.To avoid compression artifacts, you can control jpeg compression parameters by manually controlling compression rather than using the (low-quality) default
ImageFormat.Jpeg– unfortunately, doing so on CF is not possible without resorting to manual P/Invoking or a third party library. For a more extensive code sample for the full .NET Framework, see an answer I wrote to a similar question: How do you conver a HttpPostedFileBase to an Image?.An easy alternative is to use use PNG, a lossless image format that does not degrade quality each time it is saved – given the simple geometric shapes of the example you posted, this won’t even cost you anything in terms of file-size. Using another format may be your only option; customizing jpeg encoder parameters isn’t possible in the Compact Framework since the
ImageCodecInfoisn’t available. If you really need Jpeg, you could attempt to use an external library (e.g. http://www.opennetcf.com/library/sdf/), or P/Invoke native methods directly to access platform-specific encoders.Some possible approaches for anti-aliasing: