代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private System.Drawing.Bitmap curMap; private void openImage_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
curMap = new Bitmap(openFileDialog1.FileName);
pictureBox1.Image = curMap; }
} private void button1_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Pen curPen = new Pen(Brushes.Black, 1);
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.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(46, 242));
g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(34, 234));
g.DrawLine(curPen, 48, 40, 50, 40);
g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(46, 242));
Rectangle rect = new Rectangle(0,0,curMap.Width,curMap.Height);
System.Drawing.Imaging.BitmapData bmpData = curMap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curMap.PixelFormat);
int bytes = bmpData.Width * bmpData.Height;
byte[] grayValue = new byte[bytes];
IntPtr ptr = bmpData.Scan0;
System.Runtime.InteropServices.Marshal.Copy(ptr, grayValue, 0, bytes);
byte temp = 0 ;
int maxPixel = 0;
int[] countPixle =new int[256];
Array.Clear(countPixle, 0, 256);
for (int i = 0; i < bytes; i++)
{
temp = grayValue[i];
countPixle[temp]++;
if(countPixle[temp]>maxPixel)
{
maxPixel = countPixle[temp];
}
}
System.Runtime.InteropServices.Marshal.Copy(grayValue, 0, ptr, bytes);
g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(46, 242));
g.DrawString(maxPixel.ToString(), new Font("New Timer", 8), Brushes.Black, new PointF(18, 34));
for (int i = 0; i < 256; )
{
temp = Convert.ToByte(200.0 * countPixle[i] / maxPixel);
g.DrawLine(curPen, 50 + i, 240, 50 + i, 240 - (int)temp);
}
curPen.Dispose();
}
}
}
运行完之后:却显示不出来直方图,只显示坐标,、
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private System.Drawing.Bitmap curMap; private void openImage_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
curMap = new Bitmap(openFileDialog1.FileName);
pictureBox1.Image = curMap; }
} private void button1_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Pen curPen = new Pen(Brushes.Black, 1);
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.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(46, 242));
g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(34, 234));
g.DrawLine(curPen, 48, 40, 50, 40);
g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(46, 242));
Rectangle rect = new Rectangle(0,0,curMap.Width,curMap.Height);
System.Drawing.Imaging.BitmapData bmpData = curMap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curMap.PixelFormat);
int bytes = bmpData.Width * bmpData.Height;
byte[] grayValue = new byte[bytes];
IntPtr ptr = bmpData.Scan0;
System.Runtime.InteropServices.Marshal.Copy(ptr, grayValue, 0, bytes);
byte temp = 0 ;
int maxPixel = 0;
int[] countPixle =new int[256];
Array.Clear(countPixle, 0, 256);
for (int i = 0; i < bytes; i++)
{
temp = grayValue[i];
countPixle[temp]++;
if(countPixle[temp]>maxPixel)
{
maxPixel = countPixle[temp];
}
}
System.Runtime.InteropServices.Marshal.Copy(grayValue, 0, ptr, bytes);
g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(46, 242));
g.DrawString(maxPixel.ToString(), new Font("New Timer", 8), Brushes.Black, new PointF(18, 34));
for (int i = 0; i < 256; )
{
temp = Convert.ToByte(200.0 * countPixle[i] / maxPixel);
g.DrawLine(curPen, 50 + i, 240, 50 + i, 240 - (int)temp);
}
curPen.Dispose();
}
}
}
运行完之后:却显示不出来直方图,只显示坐标,、
少了i++