첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170
using System.Drawing;

private const int HISTOGRAM_BITMAP_WIDTH  = 256;
private const int HISTOGRAM_BITMAP_HEIGHT = 200;

#region 히스토그램 비트맵 구하기 - GetHistogramBitmap(sourceArray, foregroundColor, backgroundColor)

/// <summary>
/// 히스토그램 비트맵 구하기
/// </summary>
/// <param name="sourceArray">소스 배열</param>
/// <param name="foregroundColor">전경색</param>
/// <param name="backgroundColor">배경색</param>
/// <returns>히스토그램 비트맵</returns>
public Bitmap GetHistogramBitmap(int[,] sourceArray, Color foregroundColor, Color backgroundColor)
{
    int      width          = sourceArray.GetUpperBound(1) + 1;
    int      height         = sourceArray.GetUpperBound(0) + 1;
    int[]    histogramArray = new int[256];
    int      maximumCount   = 0;
    Bitmap   bitmap         = new Bitmap(HISTOGRAM_BITMAP_WIDTH, HISTOGRAM_BITMAP_HEIGHT);
    Graphics graphics       = Graphics.FromImage(bitmap);

    histogramArray.Initialize();

    for(int y = 0; y < height; y++)
    {
        for(int x = 0; x < width; x++)
        {
            histogramArray[sourceArray[y, x]]++;
        }
    }

    for(int i = 0; i < 256; i++)
    {
        if(histogramArray[i] > maximumCount)
        {
            maximumCount = histogramArray[i];
        }
    }

    graphics.Clear(backgroundColor);

    Pen pen = new Pen(foregroundColor);

    for(int x = 0; x < HISTOGRAM_BITMAP_WIDTH; x++)
    {
        double barHeight = (double)histogramArray[x] * (HISTOGRAM_BITMAP_HEIGHT - 1) / (double)maximumCount;

        graphics.DrawLine(pen, x, HISTOGRAM_BITMAP_HEIGHT - (int)barHeight, x, 200);
    }

    graphics.DrawRectangle(Pens.Black, 0, 0, bitmap.Width - 1, bitmap.Height - 1);

    return bitmap;
}

#endregion
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요