Use OctreeQuantizer to improve the Drawing Quality of gdi +, octreequantizergdi
In. net, the quality of graphics drawn by gdi + is very small, because gdi + uses 256 colors.
The "REE" algorithm can be used to improve the image quality. The "REE" algorithm allows us to insert our own algorithms to quantize our images.
A good "Color quantization" algorithm should consider filling the two pixel particles with a transitional color similar to the two pixels to provide more visible color space.
Morgan Skinner provides good "REE" algorithm code. You can download it for reference.
It is convenient to use 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;
// Draw an image
This. RenerImage (g );
System. IO. MemoryStream MS = new System. IO. MemoryStream ();
// Clear the image using 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;
}
- OctreeQuantizer.zip (6.6 KB)
For the problem of GDI + plotting in c #
Even if you can do this, it is not logical and not easy to maintain.
Remember that the screen has no memory
If you change the form size so that the customer region of the control is exposed or exposed, the re-painting event of the control is triggered.
What you have painted in other Code cannot be called during re-painting.
The simplest example is that you have successfully painted the image in mousemove.
Minimize the form-maximize the size of your painting and nothing else
This is the refresh principle of controls based on GDI drawing.
Therefore, if you trigger the click or mousemove event, you need to change the state variables in some classes and then call the invalidate redraw control. The drawing code should be correctly written in the paint.
C # GDI + plotting
Let's take a look at the following code.
Private void button#click (object sender, EventArgs e)
{
Graphics g = this. panel1.CreateGraphics ();
Rectangle PicRect = this. panel1.ClientRectangle;
Rectangle myRect = new Rectangle (0, 0, PicRect. Width/3, PicRect. Height );
G. DrawRectangle (Pens. Red, myRect );
G. FillRectangle (Brushes. Red, myRect );
MyRect = new Rectangle (PicRect. Width/3, 0, PicRect. Width/3, PicRect. Height );
G. DrawRectangle (Pens. Green, myRect );
G. FillRectangle (Brushes. Green, myRect );
MyRect = new Rectangle (PicRect. Width/3*2, 0, PicRect. Width/3, PicRect. Height );
G. DrawRectangle (Pens. Blue, myRect );
G. FillRectangle (Brushes. Blue, myRect );
}
Private void button2_Click (object sender, EventArgs e)
{
// Create a Graphics instance so that it can interact with external devices through the windows system;
Graphics g = this. panel1.CreateGraphics ();
G. Clear (Color. White); // clean the screen
// Design the painting transparency
Int transValue = 255;
// Use a red paint brush;
SolidBrush myBrush = new SolidBrush (Color. FromArgb (transValue, Color. Red ));
// Get a red paint brush
Pen myPen = new Pen (Color. FromArgb (transValue, Color. Red), 1 );
G. DrawLine (myPen,-200, 0,200, 0); // draw a line horizontally.
G. DrawLine (myPen, 0,-200, 0,200); // draw a line in the vertical direction
Font myFont ...... remaining full text>