Red de conocimiento informático - Material del sitio web - Cómo generar un histograma usando C#

Cómo generar un histograma usando C#

Aquí hay un ejemplo de programación de histograma en escala de grises de 8 bits: simplemente modifíquelo para colorearlo.

Parte del código del formulario "Dibujar histograma" de VS2008

clase parcial pública histForm: Formulario

{

public histForm( Mapa de bits bmp)

{

InitializeComponent();

bmpHist = bmp;

countPixel = new int[256]

}

private void close_Click(remitente del objeto, EventArgs e)

{

this.Close();

this.Close();

this.Close();

this.Close();

this.Close();

this.Close()Close();

}

private void histForm_Paint(remitente del objeto, PaintEventArgs e)

{

Pluma curPen = new Pluma(Brushes.Black, 1);

Gráficos g = e.Graphics

this.Graphics

g.DrawLine; ( curPen, 50, 240, 320, 240);

g.DrawLine(curPen, 50, 240, 50, 30);

g.

g.DrawLine(curPen, 50, 240, 50, 30

g.DrawLine(curPen, 50, 240, 50, 30); > g.

g.DrawLine(curPen, 250, 240, 250, 242);

g.DrawLine(curPen, 300, 240, 300, 242);

g.DrawString("0", nueva fuente("Nuevo temporizador", 8), Brushes.G.DrawString("0", nueva fuente("Nuevo temporizador", 8), Brushes.Black, nuevo PointF ( 46, 242));

G. DrawString("100", new Font("New Timer", 8), Brushes.Black, new PointF(92, 242));

G.

g.DrawString("100", new Font("Nuevo temporizador", 8), Brushes.Black, new PointF(139, 242));

g.DrawString( "150", nueva fuente ("Nuevo temporizador", 8), Pinceles.Negro, nuevo PuntoF(289, 242));

g.DrawLine(curPen, 48, 40, 50, 40);

g.DrawString("0", new Font("Nuevo temporizador", 8), Brushes.Black, new PointF(18, 34));

double temp = 0 ;

for (int i = 0; i lt; 256; i )

{

temp = 200.0 * countPixel[i] / maxPixel;

p>

g. DrawLine(curPen, 50 i, 240, 50 i, 240 - (int)temp

}

curPen.Dispose); ();

p>

}

privado void histForm_Load(objeto remitente, EventArgs e)

{

Rectángulo rectángulo = nuevo Rectángulo(0, 0, bmpHist. Ancho, bmpHist.Height);

System.Drawing.Imaging.BitmapData bmpData = bmpHist.LockBits(rect, System.Drawing.Imaging.ImageLingBits);

int bytes = bmpHist. Ancho * bmpHist.Height;

byte[] grayValues ​​​​= nuevo byte[bytes]; (ptr, valores grises

System.Runtime.InteropServices.Copy(ptr, valores grises

System.Runtime.InteropServices.Copy(ptr, valores grises

);

System.Runtime.InteropServices.Copy(ptr, grayValues)Copy(ptr, grayValues, 0, bytes);

byte temp = 0

maxPixel = 0; /p>

Array.Clear(countPixel, 0, 256);

for (int i = 0; i lt; bytes; i)

{

temp = grayValues[i];

p>

countPixel[temp];

if (countPixel[temp] gt; m

axPixel)

{

maxPixel = countPixel[temp];

}

}

System.Runtime .InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);

bmpHist.UnlockBits(bmpData);

}

}

Clases parciales utilizadas para el diseño de formularios.

clase parcial histForm

{

/// lt; resumengt;

/// Variables de diseño requeridas.

/// lt;/summarygt;

componentes privados System.ComponentModel.IContainer = null

/// lt;summarygt;

/// Borrar todos los recursos en uso.

/// lt;/summarygt;

/// lt;param name="disposing"gt; verdadero si el recurso administrado debe liberarse; en caso contrario, falso.