Scenes
- When developing WTL programs,
Gdiplus
drawPNG
pictures are used. Sometimes you will encounter the following situations. When drawing an original picture with equal width and height, the picture looks clear on the interface, and when thePNG
width and height are reduced, the picture drawn is obviously jagged. What is the reason?
Description
-
Use the
Gdiplus::Graphics
object methodDrawImage
to draw theRect
source rectangular area of the original image to the targetRect
, that is, if the width and height of the target area and the source area are inconsistent, the image will be zoomed and drawn. For example, we have an256x256
icon, and then we need to draw the zoom icon to the upper left corner of the title bar, and the icon of this position not only need32x32
, if you do not have a separate32x32
small icon, we need to call in the big iconDrawImage
when Specify the width and height of the target area32x32
, soGraphics
that the icon will be scaled when drawing, so there is no need to create a new oneGdiplus::Bitmap
for drawing. -
Graphics
When zooming and drawing icons, theInterpolation Mode
interpolation mode determines the quality of zooming. Bygraphics.SetInterpolationMode
setting the interpolation mode, bygraphics.GetInterpolationMode
obtaining the current interpolation mode. -
In the header file
<gdiplusenums.h>
, the following enumeration constants are optional.InterpolationModeDefault
The value is the default interpolation mode value, which is equal toInterpolationModeLowQuality
and equal toInterpolationModeBilinear
. AndInterpolationModeHighQuality
it is supported by default maximum quality option here because the quality is the bestInterpolationModeHighQualityBicubic
, soInterpolationModeHighQuality
that is equalInterpolationModeHighQualityBicubic
.typedef enum InterpolationMode { InterpolationModeInvalid, InterpolationModeDefault, // 默认,即等于 InterpolationModeBilinear InterpolationModeLowQuality, // 默认,即等于InterpolationModeBilinear InterpolationModeHighQuality, // 最高质量,即等于InterpolationModeHighQualityBicubic InterpolationModeBilinear, // 双线性插值 InterpolationModeBicubic, // 双三次插值 InterpolationModeNearestNeighbor, // 最近邻插值 InterpolationModeHighQualityBilinear, InterpolationModeHighQualityBicubic } ;
-
The following code can be used to verify their relationship. The quality of the interpolation mode is the same as their enumeration constant value, from low to high. If you want to set the highest zoom quality, use
InterpolationModeHighQualityBicubic
parameters. Of course, the higher the quality, the more computing resources are needed. AsCPU
the performance improves, this loss can be ignored. If you follow the highest optimization algorithm supported by the system, you can set itInterpolationModeHighQuality
, and the current highest value is still equal toInterpolationModeHighQualityBicubic
.graphics.SetInterpolationMode(InterpolationModeLowQuality); auto mode = graphics.GetInterpolationMode(); // mode:InterpolationModeBilinear
example
- Let's take a look at an
128x128
icon that uses the default and highest interpolation mode. The quality is very different. The outer circle of low quality has burrs, and the high quality is soft.
InterpolationModeBilinear(InterpolationModeLowQuality)
figure 1:
InterpolationModeHighQualityBicubic(InterpolationModeHighQuality)
figure 2:
Original image
reference
Using Interpolation Mode to Control Image Quality During Scaling