예제 #1
0
        private static Bitmap GenerateBitmap16x16(ColorGradientEnum gradient)
        {
            int    n = 16, m = 16;
            Bitmap bitmap = new Bitmap(n, m, PixelFormat.Format32bppArgb);

            float[,] values = new float[n, m];
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    values[i, j] = ((float)i + (float)j) / (float)(n * m);
                }
            }
            GetMinMax(values, out float vMin, out float vMax);
            Rectangle rectangle = new Rectangle(0, 0, n, m);

            GradientBitmap.FromValues(bitmap, rectangle, (int i, int j) =>
            {
                float z = (values[i, j] - vMin) / (vMax - vMin);
                return(ColorUtilities.GetColor(z, gradient, false));
            });
            return(bitmap);
        }
예제 #2
0
        public static void DrawLegend(
            Bitmap bitmap,
            float minValue, float maxValue, string format,
            ColorGradientEnum colorGradient, bool inverseGradient,
            params string[] lines)
        {
            if (float.IsNaN(minValue) || float.IsNaN(maxValue))
            {
                return;
            }
            const int a = 12; // distance from right legend to right bitmap
            const int b = 12; // distance from bottom legend to bottom bitmap
            const int l = 16; // height of a text line
            const int h = 60; // height of the color gradient indicator
            const int w = 64; // width
            const int d = 2;  // distance from y to text string
            int       n = lines != null ? lines.Length : 0;
            float     x1 = bitmap.Width - a, x0 = x1 - w, x2 = x0;
            float     y4 = bitmap.Height - b, y3 = y4 - l, y2 = y3 - h, y1 = y2 - l, y0 = y1 - n * l;

            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                // background
                using (SolidBrush backgroundBrush = new SolidBrush(Color.FromArgb(0xF0, 0xF0, 0xF0)))
                {
                    graphics.FillRectangle(backgroundBrush, x0, y0, w, h + (2 + n) * l);
                }

                // color gradient
                using (Pen gradientPen = new Pen(Color.Black, 1f))
                {
                    for (int i = 0; i <= h; i++)
                    {
                        float z = maxValue > minValue ? (float)i / h : 0.5f;
                        gradientPen.Color = ColorUtilities.GetColor(z, colorGradient, inverseGradient);
                        float y = y2 + i;
                        graphics.DrawLine(gradientPen, x0, y, x1, y);
                    }
                }

                // text
                using (Pen textPen = new Pen(Color.Black, 1f))
                    using (SolidBrush textBrush = new SolidBrush(Color.Black))
                        using (StringFormat stringFormat = new StringFormat
                        {
                            Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Near
                        })
                        {
                            Font font = Control.DefaultFont;
                            textBrush.Color = Color.Black;
                            textPen.Color   = Color.Black;
                            for (int i = 0; i < n; i++)
                            {
                                graphics.DrawString(lines[i], font, textBrush, x2, y0 + i * l + d, stringFormat);
                                graphics.DrawRectangle(textPen, x0, y0 + i * l, w, l);
                            }
                            graphics.DrawString(minValue.ToString(format), font, textBrush, x2, y1 + d, stringFormat);
                            graphics.DrawString(maxValue.ToString(format), font, textBrush, x2, y3 + d, stringFormat);
                            graphics.DrawRectangle(textPen, x0, y1, w, h + 2 * l);
                        }
            }
        }