|
|
[C#]
using (Bitmap bm = (Bitmap)Bitmap.FromFile(Server.MapPath("rez/josh-hild-_TuI8tZHlk4-unsplash.jpg"))) {
Dictionary<ColorChannel, double[]> hist = Effects.Histogram(bm);
double aspectRatio = bm.Width / (double)bm.Height;
int height = (int)Math.Round(600 / aspectRatio);
using (Image small = bm.GetThumbnailImage(600, height)) {
using (Graphics gr = Graphics.FromImage(small)) {
DrawGraph(gr, small.Size, Color.Red, hist[ColorChannel.Red]);
DrawGraph(gr, small.Size, Color.Green, hist[ColorChannel.Green]);
DrawGraph(gr, small.Size, Color.Blue, hist[ColorChannel.Blue]);
}
small.Save(Server.MapPath("IG8_Effects_Histogram.jpg"));
}
}
void DrawGraph(Graphics gr, SizeF size, Color color, double[] values) {
double max = 0;
for (int i = 0; i < values.Length; i++)
max = Math.Max(max, values[i]);
PointF[] points = new PointF[values.Length];
for (int i = 0; i < values.Length; i++) {
double x = i * (size.Width / values.Length);
double y = values[i] * (size.Height / max);
points[i] = new PointF(x, y);
}
gr.DrawLines(new Pen(new SolidBrush(color), 5), points);
}
Here we determine the color histogram for an image. Using the
values we then draw a graph of the color levels on the image. Ouput
files are shown below.

josh-hild-_TuI8tZHlk4-unsplash.jpg

IG8_Effects_Histogram.jpg
|