histForm.cs
上传用户:c6x6r6
上传日期:2022-07-31
资源大小:515k
文件大小:6k
- 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;
- using System.Drawing.Imaging;
- using System.Runtime.InteropServices;
- namespace WindowsFormsApplication2
- {
- public partial class histForm : Form
- {
- public histForm(Bitmap bmp)
- {
- InitializeComponent();
- bmpHist = bmp;
- countPixel = new int [3 , 256];
- maxPixel = new int[3];
- }
- //关闭按钮
- private void button1_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- private void histForm_Paint(object sender, PaintEventArgs e)
- {
- Pen curPen = new Pen(Brushes.Red, 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[0].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[0 , i] / maxPixel[0];
- g.DrawLine(curPen, 50 + i, 240, 50 + i, 240 - (int)temp);
- }
- curPen.Dispose();
- curPen = new Pen(Brushes.Green, 1);
- g.DrawLine(curPen, 350, 240, 620, 240);
- g.DrawLine(curPen, 350, 240, 350, 30);
- g.DrawLine(curPen, 400, 240, 400, 242);
- g.DrawLine(curPen, 450, 240, 450, 242);
- g.DrawLine(curPen, 500, 240, 500, 242);
- g.DrawLine(curPen, 550, 240, 550, 242);
- g.DrawLine(curPen, 600, 240, 600, 242);
- g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(346, 242));
- g.DrawString("50", new Font("New Timer", 8), Brushes.Black, new PointF(392, 242));
- g.DrawString("100", new Font("New Timer", 8), Brushes.Black, new PointF(439, 242));
- g.DrawString("150", new Font("New Timer", 8), Brushes.Black, new PointF(489, 242));
- g.DrawString("200", new Font("New Timer", 8), Brushes.Black, new PointF(539, 242));
- g.DrawString("250", new Font("New Timer", 8), Brushes.Black, new PointF(589, 242));
- g.DrawLine(curPen, 348, 40, 350, 40);
- g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(334, 234));
- g.DrawString(maxPixel[0].ToString(), new Font("New Timer", 8), Brushes.Black, new PointF(318, 34));
- temp = 0;
- for (int i = 0; i < 256; i++)
- {
- temp = 200.0 * countPixel[1, i] / maxPixel[1];
- g.DrawLine(curPen, 350 + i, 240, 350 + i, 240 - (int)temp);
- }
- curPen.Dispose();
- curPen = new Pen(Brushes.Blue, 1);
- g.DrawLine(curPen, 650, 240, 920, 240);
- g.DrawLine(curPen, 650, 240, 650, 30);
- g.DrawLine(curPen, 700, 240, 700, 242);
- g.DrawLine(curPen, 750, 240, 750, 242);
- g.DrawLine(curPen, 800, 240, 800, 242);
- g.DrawLine(curPen, 850, 240, 850, 242);
- g.DrawLine(curPen, 900, 240, 900, 242);
- g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(646, 242));
- g.DrawString("50", new Font("New Timer", 8), Brushes.Black, new PointF(692, 242));
- g.DrawString("100", new Font("New Timer", 8), Brushes.Black, new PointF(739, 242));
- g.DrawString("150", new Font("New Timer", 8), Brushes.Black, new PointF(789, 242));
- g.DrawString("200", new Font("New Timer", 8), Brushes.Black, new PointF(839, 242));
- g.DrawString("250", new Font("New Timer", 8), Brushes.Black, new PointF(889, 242));
- g.DrawLine(curPen, 648, 40, 650, 40);
- g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(634, 234));
- g.DrawString(maxPixel[0].ToString(), new Font("New Timer", 8), Brushes.Black, new PointF(618, 34));
- temp = 0;
- for (int i = 0; i < 256; i++)
- {
- temp = 200.0 * countPixel[2, i] / maxPixel[2];
- g.DrawLine(curPen, 650 + i, 240, 650 + i, 240 - (int)temp);
- }
- curPen.Dispose();
- }
- private void histForm_Load(object sender, EventArgs e)
- {
- Rectangle rect = new Rectangle(0, 0, bmpHist.Width, bmpHist.Height);
- BitmapData bmpData = bmpHist.LockBits(rect, ImageLockMode.ReadWrite, bmpHist.PixelFormat);
- IntPtr ptr = bmpData.Scan0;
- int bytes = bmpHist.Width * bmpHist.Height;
- byte[] rgbValues = new byte[bytes * 3];
- Marshal.Copy(ptr, rgbValues, 0, bytes * 3);
- int rTemp , gTemp , bTemp;
- for (int i = 0; i < bytes; i++)
- {
- rTemp = ++countPixel[0 , rgbValues[i * 3 + 2]];
- gTemp = ++countPixel[1 , rgbValues[i * 3 + 1]];
- bTemp = ++countPixel[2 , rgbValues[i * 3]];
- if ( rTemp > maxPixel[0])
- {
- maxPixel[0] = rTemp;
- }
- if (gTemp > maxPixel[1])
- {
- maxPixel[1] = gTemp;
- }
- if (bTemp > maxPixel[2])
- {
- maxPixel[2] = bTemp;
- }
- }
- bmpHist.UnlockBits(bmpData);
- }
- }
- }