當前位置:編程學習大全網 - 編程語言 - 如何用C#生成直方圖

如何用C#生成直方圖

這是8位灰度直方圖編程實例:彩色的修改壹下就行了。

VS2008 "繪制直方圖"窗體的代碼部分

public partial class histForm : Form

{

public histForm(Bitmap bmp)

{

InitializeComponent();

bmpHist = bmp;

countPixel = new int[256];

}

private void close_Click(object sender, EventArgs e)

{

this.Close();

}

private void histForm_Paint(object sender, PaintEventArgs e)

{

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

Graphics g = e.Graphics;

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

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

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

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

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

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

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

g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(46, 242));

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

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

g.DrawString("150", new Font("New Timer", 8), Brushes.Black, new PointF(189, 242));

g.DrawString("200", new Font("New Timer", 8), Brushes.Black, new PointF(239, 242));

g.DrawString("250", new Font("New Timer", 8), Brushes.Black, new PointF(289, 242));

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

g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(34, 234));

g.DrawString(maxPixel.ToString(), new Font("New Timer", 8), Brushes.Black, new PointF(18, 34));

double temp = 0;

for (int i = 0; i < 256; i++)

{

temp = 200.0 * countPixel[i] / maxPixel;

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

}

curPen.Dispose();

}

private void histForm_Load(object sender, EventArgs e)

{

Rectangle rect = new Rectangle(0, 0, bmpHist.Width, bmpHist.Height);

System.Drawing.Imaging.BitmapData bmpData = bmpHist.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmpHist.PixelFormat);

IntPtr ptr = bmpData.Scan0;

int bytes = bmpHist.Width * bmpHist.Height;

byte[] grayValues = new byte[bytes];

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

byte temp = 0;

maxPixel = 0;

Array.Clear(countPixel, 0, 256);

for (int i = 0; i < bytes; i++)

{

temp = grayValues[i];

countPixel[temp]++;

if (countPixel[temp] > maxPixel)

{

maxPixel = countPixel[temp];

}

}

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

bmpHist.UnlockBits(bmpData);

}

}

窗體設計類部分。

partial class histForm

{

/// <summary>

/// 必需的設計器變量。

/// </summary>

private System.ComponentModel.IContainer components = null;

/// <summary>

/// 清理所有正在使用的資源。

/// </summary>

/// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows 窗體設計器生成的代碼

/// <summary>

/// 設計器支持所需的方法 - 不要

/// 使用代碼編輯器修改此方法的內容。

/// </summary>

private void InitializeComponent()

{

this.close = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// close

//

this.close.Location = new System.Drawing.Point(252, 265);

this.close.Name = "close";

this.close.Size = new System.Drawing.Size(75, 23);

this.close.TabIndex = 0;

this.close.Text = "關閉";

this.close.UseVisualStyleBackColor = true;

this.close.Click += new System.EventHandler(this.close_Click);

//

// histForm

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(347, 305);

this.ControlBox = false;

this.Controls.Add(this.close);

this.Name = "histForm";

this.Text = "直方圖";

this.Paint += new System.Windows.Forms.PaintEventHandler(this.histForm_Paint);

this.Load += new System.EventHandler(this.histForm_Load);

this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button close;

private System.Drawing.Bitmap bmpHist;

private int[] countPixel;

private int maxPixel;

}

  • 上一篇:冰球比賽冰球比賽規則
  • 下一篇:安徽博微長安電子有限公司是國企嗎
  • copyright 2024編程學習大全網