histForm.cs
上传用户:c6x6r6
上传日期:2022-07-31
资源大小:515k
文件大小:6k
源码类别:

波变换

开发平台:

Visual Basic

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Drawing.Imaging;
  10. using System.Runtime.InteropServices;
  11. namespace WindowsFormsApplication2
  12. {
  13.     public partial class histForm : Form
  14.     {
  15.         public histForm(Bitmap bmp)
  16.         {
  17.             InitializeComponent();
  18.             bmpHist = bmp;
  19.             countPixel = new int [3 , 256];
  20.             maxPixel = new int[3];
  21.         }
  22.         //关闭按钮
  23.         private void button1_Click(object sender, EventArgs e)
  24.         {
  25.             this.Close();
  26.         }
  27.         private void histForm_Paint(object sender, PaintEventArgs e)
  28.         {
  29.             Pen curPen = new Pen(Brushes.Red, 1);
  30.             Graphics g = e.Graphics;
  31.             g.DrawLine(curPen, 50, 240, 320, 240);
  32.             g.DrawLine(curPen, 50, 240, 50, 30);
  33.             g.DrawLine(curPen, 100, 240, 100, 242);
  34.             g.DrawLine(curPen, 150, 240, 150, 242);
  35.             g.DrawLine(curPen, 200, 240, 200, 242);
  36.             g.DrawLine(curPen, 250, 240, 250, 242);
  37.             g.DrawLine(curPen, 300, 240, 300, 242);
  38.             g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(46, 242));
  39.             g.DrawString("50", new Font("New Timer", 8), Brushes.Black, new PointF(92, 242));
  40.             g.DrawString("100", new Font("New Timer", 8), Brushes.Black, new PointF(139, 242));
  41.             g.DrawString("150", new Font("New Timer", 8), Brushes.Black, new PointF(189, 242));
  42.             g.DrawString("200", new Font("New Timer", 8), Brushes.Black, new PointF(239, 242));
  43.             g.DrawString("250", new Font("New Timer", 8), Brushes.Black, new PointF(289, 242));
  44.             g.DrawLine(curPen, 48, 40, 50, 40);
  45.             g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(34, 234));
  46.             g.DrawString(maxPixel[0].ToString(), new Font("New Timer", 8), Brushes.Black, new PointF(18, 34));
  47.             double temp = 0;
  48.             for (int i = 0; i < 256; i++)
  49.             {
  50.                 temp = 200.0 * countPixel[0 , i] / maxPixel[0];
  51.                 g.DrawLine(curPen, 50 + i, 240, 50 + i, 240 - (int)temp);
  52.             }
  53.             curPen.Dispose();
  54.             curPen = new Pen(Brushes.Green, 1);
  55.             g.DrawLine(curPen, 350, 240, 620, 240);
  56.             g.DrawLine(curPen, 350, 240, 350, 30);
  57.             g.DrawLine(curPen, 400, 240, 400, 242);
  58.             g.DrawLine(curPen, 450, 240, 450, 242);
  59.             g.DrawLine(curPen, 500, 240, 500, 242);
  60.             g.DrawLine(curPen, 550, 240, 550, 242);
  61.             g.DrawLine(curPen, 600, 240, 600, 242);
  62.             g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(346, 242));
  63.             g.DrawString("50", new Font("New Timer", 8), Brushes.Black, new PointF(392, 242));
  64.             g.DrawString("100", new Font("New Timer", 8), Brushes.Black, new PointF(439, 242));
  65.             g.DrawString("150", new Font("New Timer", 8), Brushes.Black, new PointF(489, 242));
  66.             g.DrawString("200", new Font("New Timer", 8), Brushes.Black, new PointF(539, 242));
  67.             g.DrawString("250", new Font("New Timer", 8), Brushes.Black, new PointF(589, 242));
  68.             g.DrawLine(curPen, 348, 40, 350, 40);
  69.             g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(334, 234));
  70.             g.DrawString(maxPixel[0].ToString(), new Font("New Timer", 8), Brushes.Black, new PointF(318, 34));
  71.             temp = 0;
  72.             for (int i = 0; i < 256; i++)
  73.             {
  74.                 temp = 200.0 * countPixel[1, i] / maxPixel[1];
  75.                 g.DrawLine(curPen, 350 + i, 240, 350 + i, 240 - (int)temp);
  76.             }
  77.             curPen.Dispose();
  78.             curPen = new Pen(Brushes.Blue, 1);
  79.             g.DrawLine(curPen, 650, 240, 920, 240);
  80.             g.DrawLine(curPen, 650, 240, 650, 30);
  81.             g.DrawLine(curPen, 700, 240, 700, 242);
  82.             g.DrawLine(curPen, 750, 240, 750, 242);
  83.             g.DrawLine(curPen, 800, 240, 800, 242);
  84.             g.DrawLine(curPen, 850, 240, 850, 242);
  85.             g.DrawLine(curPen, 900, 240, 900, 242);
  86.             g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(646, 242));
  87.             g.DrawString("50", new Font("New Timer", 8), Brushes.Black, new PointF(692, 242));
  88.             g.DrawString("100", new Font("New Timer", 8), Brushes.Black, new PointF(739, 242));
  89.             g.DrawString("150", new Font("New Timer", 8), Brushes.Black, new PointF(789, 242));
  90.             g.DrawString("200", new Font("New Timer", 8), Brushes.Black, new PointF(839, 242));
  91.             g.DrawString("250", new Font("New Timer", 8), Brushes.Black, new PointF(889, 242));
  92.             g.DrawLine(curPen, 648, 40, 650, 40);
  93.             g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(634, 234));
  94.             g.DrawString(maxPixel[0].ToString(), new Font("New Timer", 8), Brushes.Black, new PointF(618, 34));
  95.             temp = 0;
  96.             for (int i = 0; i < 256; i++)
  97.             {
  98.                 temp = 200.0 * countPixel[2, i] / maxPixel[2];
  99.                 g.DrawLine(curPen, 650 + i, 240, 650 + i, 240 - (int)temp);
  100.             }
  101.             curPen.Dispose();
  102.         }
  103.         private void histForm_Load(object sender, EventArgs e)
  104.         {
  105.             Rectangle rect = new Rectangle(0, 0, bmpHist.Width, bmpHist.Height);
  106.             BitmapData bmpData = bmpHist.LockBits(rect, ImageLockMode.ReadWrite, bmpHist.PixelFormat);
  107.             IntPtr ptr = bmpData.Scan0;
  108.             int bytes = bmpHist.Width * bmpHist.Height;
  109.             byte[] rgbValues = new byte[bytes * 3];
  110.             Marshal.Copy(ptr, rgbValues, 0, bytes * 3);
  111.             int rTemp , gTemp , bTemp;
  112.             for (int i = 0; i < bytes; i++)
  113.             {
  114.                 rTemp = ++countPixel[0 , rgbValues[i * 3 + 2]];
  115.                 gTemp = ++countPixel[1 , rgbValues[i * 3 + 1]];
  116.                 bTemp = ++countPixel[2 , rgbValues[i * 3]];
  117.                 if ( rTemp > maxPixel[0])
  118.                 {
  119.                     maxPixel[0] = rTemp;
  120.                 }
  121.                 if (gTemp > maxPixel[1])
  122.                 {
  123.                     maxPixel[1] = gTemp;
  124.                 }
  125.                 if (bTemp > maxPixel[2])
  126.                 {
  127.                     maxPixel[2] = bTemp;
  128.                 }
  129.             }
  130.             bmpHist.UnlockBits(bmpData);
  131.         }
  132.     }
  133. }