c# 封装的图像类和自己bitmap类关联
作业,之前就会一点c++,老师给了底模板,他自己写了一个classImages类~让我们在上面加代码,但是让人郁闷的是写的程序貌似在构造函数中加载一副图片,但是我不想这么做~ 我想通过一个打开的菜单控件自己打开所需要的图像,再在panel上面画,相关资料用system.draeing.bitmap建一个对象,图像是通过这个对象传进来的~我想问一下怎么把这个对象和写的ClassIMge这个类关联起来??纠结很久了。。。就是弄不出来~ 恳请大侠们帮帮忙。。。。
这是主form:
下面的是老师给的类:
这是主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);
}
}
}
...全文
相关推荐
C#图形图像编程基础.doc
C#图像处理基础:1. Bitmap类:Bitmap类是用于显示、操作和保存BMP、JPG、GIF等图像格式的类。2.
C#图形图像处理实战教程
GDI+是GDI的增强版,它提供了更丰富的图形功能,支持复杂的图形绘制任务。在C#中,通过System.Drawing命名空间,开发者可以方便地调用GDI+提供的接口,进行图像处理、绘图操作以及颜色管理等。它不仅是实现简单图形绘制的基础,也是构建复杂图形用户界面的关键技术。在.NET框架中,Graphics类是进行图形绘制操作的核心组件。它提供了大量的方法来绘制各种基本图形,如线条、矩形、椭圆和曲线等,以及复杂的图形对象如位图和图像。
编程资料 - C#图形、图像处理(含GDI OWC ZedGragh)
#### 三、Bitmap类详解- **封装**: `Bitmap`类封装了GDI+位图,位图由图形图像及其属性的像素数据组成。
C# 播放Flv(图像操作)
**加载图像**:可以使用Bitmap类的FromFile方法加载FLV帧为图像对象。2.
C#_GDI+编程教程.doc
1.3 图像处理GDI+提供了Bitmap、Image等类来处理图像。Bitmap类可以加载、显示和修改BMP、JPEG、PNG等多种图像格式。


