110,537
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DIPcSharp
{
public partial class MainForm : Form
{
ClassImage mImage;
bool hasHist;
public MainForm()
{
InitializeComponent();
mImage = new ClassImage();
mImage.ReadImage("E:\\oop\\lena.jpg");
hasHist = false;
}
private void ImagePanel_Paint(object sender, PaintEventArgs e)
{
mImage.Display(e.Graphics, ImagePanel.Width, ImagePanel.Height);
if (hasHist) mImage.drawHist(e.Graphics);
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog opnDlg = new OpenFileDialog();
opnDlg.Filter = "所有图像文件|*.bmp;*.pcx;*.png;*.jpg;*.gif;";
opnDlg.Title = "打开图像文件";
opnDlg.ShowHelp = true;
if (opnDlg.ShowDialog() == DialogResult.OK)
{
curFilename = opnDlg.FileName;//读取文件名
try
{
curBitmap = (Bitmap)Image.FromFile(curFilename);
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
Refresh();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace DIPcSharp
{
class ClassImage
{
Bitmap BMap;
int Width, Height, FWidth;
byte [] ImageB;
Int32 [] Hist = new Int32[256];
//int ImageType;
public int ReadImage(string FileName)
{
BMap = new Bitmap(FileName);
Width = BMap.Width;
Height = BMap.Height;
getBitMapData();
//ImageType = BMap.PixelFormat;
return 0;
}
public int Display(System.Drawing.Graphics e, int w, int h)
{
e.DrawImage(BMap, 0, 0, w, h);
return 0;
}
private bool getBitMapData()
{
// 从BitMap对象里获取图像数据
if(BMap == null) // 图像对象必须存在
{
return false;
};
Rectangle rect = new Rectangle(0, 0, Width, Height); //' 为锁定图像范围定义矩形
System.Drawing.Imaging.BitmapData bmpData = BMap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, BMap.PixelFormat);
//' Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
if (BMap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
{
FWidth = Convert.ToInt32((BMap.Width + 3) / 4) * 4;
Int32 bytes = FWidth * BMap.Height;
ImageB = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, ImageB, 0, bytes);
}
//' Unlock the bits.
BMap.UnlockBits(bmpData);
return true;
}
private bool putBitMapData()
{
if(BMap == null) // 图像对象必须存在
{
return false;
};
Rectangle rect = new Rectangle(0, 0, BMap.Width, BMap.Height); //' 为锁定图像范围定义矩形
System.Drawing.Imaging.BitmapData bmpData = BMap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, BMap.PixelFormat);
//' Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
if (BMap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
{
Int32 bytes = BMap.Width * BMap.Height;
System.Runtime.InteropServices.Marshal.Copy(ImageB, 0, ptr, bytes);
}
BMap.UnlockBits(bmpData);
return (true);
}
public void Histgram()
{
int i, j, bytes;
for (i = 0; i < 256; i++) Hist[i] = 0;
for (i = 0; i < Height; i++)
for (j = 0; j < Width; j++)
{
bytes = i * FWidth + j;
Hist[ImageB[bytes]]++;
}
return;
}
public void drawHist(System.Drawing.Graphics e)
{
int i;
double maxH;
maxH = Hist[0];
for (i = 1; i < 256; i++)
if (maxH < Hist[i]) maxH = Hist[i];
//Brush b =new Brush;
maxH = maxH * 1.2;
Pen p = new Pen(Color.Red);
for (i = 0; i < 255; i++)
e.DrawLine(p,i*2+20,500-Convert.ToInt32(Convert.ToDouble(Hist[i])/maxH*480)+20,(i+1)*2+20,500-Convert.ToInt32(Convert.ToDouble(Hist[i+1])/maxH*480)+20);
}
public bool Negative()
{
int i, j;
for(i=0;i<Height;i++)
{
for(j=0;j<Width;j++)
{
ImageB[i * FWidth + j] = Convert.ToByte(255 - ImageB[i * FWidth + j]);
}
}
putBitMapData();
return(true);
}
}
}