Bitmap.NET CF quality is poor

I have code that creates a graph using the Bitmap class. The quality of the generated bitmap, however, is pretty poor, how can I improve the quality of this? Please note that I am using the Compact Framework!

Here are a few snippets of how I do different things:

Create a bitmap with background based on existing jpg

 Bitmap bitmap = new Bitmap(filename); 

Then I create a Graphics class from a bitmap

 Graphics myG = Graphics.FromImage(bitmap); 

Then I draw like this:

 myG.DrawLine(majorAxisPen, graphRect.Left, graphRect.Bottom, graphRect.Right, graphRect.Bottom); 

After drawing all that I need, I then save the Bitmap like this:

 bitmap.Save(fileDestination, System.Drawing.Imaging.ImageFormat.Jpeg); 

Here is the image that he produces Poor quality image

+4
source share
4 answers

There are two sources of "poor quality" that you may encounter.

  • The actual drawing for the graphic object may be of poor quality - that is, the lines that you draw may be smoothed (pixelated).
  • Poor quality can be a side effect of jpeg coding when outputting graphics to a stream.

Each Graphics object has a number of properties that control the β€œquality” of operations that you can perform on the object. In particular, you should set SmoothingMode to HighQuality to avoid burrs in the drawings. Unsupported in the .NET Compact Framework .

To avoid compression artifacts, you can control the jpeg compression options by manually controlling the compression, rather than using the (poor quality) default ImageFormat.Jpeg - unfortunately, it is impossible to do this on CF without resorting to manual P / Invoking or a third-party library. For a more detailed code example for the full .NET Framework, see the answer I wrote to a similar question: How do you convert an HttpPostedFileBase to an image? .

An easy alternative is to use PNG, a lossless image format that does not degrade quality every time you save it - given the simple geometric shapes in the example you posted, it won't even cost you anything in terms of file size. Using a different format may be your only option; Configuring jpeg encoder settings is not possible in the Compact Framework, since ImageCodecInfo not available. If you really need Jpeg, you can try using an external library (for example, http://www.opennetcf.com/library/sdf/ ) or P / Invoke's own methods directly to access platform encoders.

Some possible anti-aliasing approaches are:

+5
source

You did not say that the quality is poor. Are there any visible JPEG artifacts? Are the colors wrong? Are the lines jagged?

Not knowing what the problem is, it is difficult to give a solution.

Some possible problems / solutions:

Lines are jagged: use Graphics.SmoothingMode to specify how lines are displayed.

Lines are jagged: use a compression format suitable for linear art: JPEG is not suitable for linear art, GIF, and PNG.

There are compression artifacts: It is not recommended to re-shoot already compressed (JPEG) graphics. If you have an uncompressed source image, use it as your base.

Compression artifacts exist: specify a lower JPEG level or use a lossless compressed type like GIF or PNG.

+2
source

At first glance, I would say check the options available in the Save call, I think you can specify a Jpeg quality value to use a higher level, or you can switch to lossless compression, for example PNG.

+1
source

Try experimenting with graphics quality:

 graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; 

In my experience, the lowest acceptable quality makes a relatively small file. The difference in visual quality between 90% and 100% is invisible, but the file will be up to 5 times smaller by 90%.

0
source

Source: https://habr.com/ru/post/1337786/


All Articles