c# 封装的图像类和自己bitmap类关联

eibhlin 2012-12-05 07:50:31
作业,之前就会一点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);
}
}
}
...全文
327 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
eibhlin 2013-01-15
  • 打赏
  • 举报
回复
引用 6 楼 yuwenge 的回复:
你老师写的class写的不错。你只需要在你的panel的paint事件中调用display方法,传入graphics就行了。 ps:其实没看懂你想干嘛,另外版主的回答更是不知所云。
谢谢奥~ 我后来解决了~ 就是传值的问题~吼吼~
夜空皓月 2012-12-07
  • 打赏
  • 举报
回复
mImage.ReadImage("E:\\oop\\lena.jpg"); 把这句话移动到menu打开的事件里。 里面"E:\\oop\\lena.jpg"这串东西改成file dialog的path。
卧_槽 2012-12-07
  • 打赏
  • 举报
回复
你老师写的class写的不错。你只需要在你的panel的paint事件中调用display方法,传入graphics就行了。 ps:其实没看懂你想干嘛,另外版主的回答更是不知所云。
YANNI_LEE 2012-12-07
  • 打赏
  • 举报
回复
学习学习~
eibhlin 2012-12-07
  • 打赏
  • 举报
回复
引用 3 楼 caozhy 的回复:
引用 2 楼 eibhlin 的回复:引用 1 楼 caozhy 的回复:google csharp 重载隐式类型转换运算符 版主你好,能说详细一点吗?我是菜鸟 你都Google出了什么?
貌似都是运算符重载问题。。。
threenewbee 2012-12-06
  • 打赏
  • 举报
回复
引用 2 楼 eibhlin 的回复:
引用 1 楼 caozhy 的回复:google csharp 重载隐式类型转换运算符 版主你好,能说详细一点吗?我是菜鸟
你都Google出了什么?
eibhlin 2012-12-06
  • 打赏
  • 举报
回复
引用 1 楼 caozhy 的回复:
google csharp 重载隐式类型转换运算符
版主你好,能说详细一点吗?我是菜鸟
threenewbee 2012-12-05
  • 打赏
  • 举报
回复
google csharp 重载隐式类型转换运算符

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧