作业,之前就会一点c++,老师给了底模板,他自己写了一个classImages类~让我们在上面加代码,但是让人郁闷的是写的程序貌似在构造函数中加载一副图片,但是我不想这么做~ 我想通过一个打开的菜单控件自己打开所需要的图像,再在panel上面画,相关资料用system.draeing.bitmap建一个对象,图像是通过这个对象传进来的~我想问一下怎么把这个对象和写的ClassIMge这个类关联起来??纠结很久了就是弄不出来~ 恳请大侠们帮帮忙。
这是主form:
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);
}
}
}
这是主form:
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);
}
}
}
mImage.ReadImage("E:\\oop\\lena.jpg");
把这句话移动到menu打开的事件里。
里面"E:\\oop\\lena.jpg"这串东西改成file dialog的path。