怎么实现将地址变量换成图片变量呢?
这个类是用来获取图像像素,但传递进去的参数是图片文件的地址path,我想让他传递进去一个picturebox.Image,怎么改才能实现啊?或者也可以换种方式,就是将当前picturebox中的图片每经历一次修改就自动存储于一个设定的地址,然后调用这个地址就可以了不用修改下面这个类了。
class userBitmap
{//位图文件文件头
struct userBitmapFileHeader
{
public UInt16 bfType;
public UInt32 bfSize;
public UInt16 bfReserved1;
public UInt16 bfReserved2;
public UInt32 bfOffBits;
}
//位图文件信息头
struct userBitmapInfoHeader
{
public UInt32 biSize;
public UInt32 biWidth; //位图宽度
public UInt32 biHeight; //位图高度
public UInt16 biPlanes;
public UInt16 biBitCount; //位图格式1,4,8,16,24
public UInt32 biCompression;
public UInt32 biSizeImage;
public UInt32 biXPelsPerMeter;
public UInt32 biYPelsPerMeter;
public UInt32 biClrUsed;
public UInt32 biClrImportant;
}
private int[,] pixel8; //数据体
private int[, ,] pixel24; //数据体
private userBitmapFileHeader bfh;
private userBitmapInfoHeader bih;
private byte[] palatte;
//初始化位图 数据 部分
public userBitmap()
{
pixel24 = null;
pixel8 = null;
}
//读取位图;文件路径为 path
public userBitmap(string path)
{
Load(path);
}
//载入位图功能实现
public void Load(string path)
{
FileInfo f = new FileInfo(path);
using (BinaryReader br = new BinaryReader(f.OpenRead()))
{
bfh.bfType = (UInt16)br.ReadInt16();
bfh.bfSize = (UInt32)br.ReadInt32();
bfh.bfReserved1 = (UInt16)br.ReadInt16();
bfh.bfReserved2 = (UInt16)br.ReadInt16();
bfh.bfOffBits = (UInt32)br.ReadInt32();
bih.biSize = (UInt32)br.ReadInt32();
bih.biWidth = (UInt32)br.ReadInt32();
bih.biHeight = (UInt32)br.ReadInt32();
bih.biPlanes = (UInt16)br.ReadInt16();
bih.biBitCount = (UInt16)br.ReadInt16();
bih.biCompression = (UInt32)br.ReadInt32();
bih.biSizeImage = (UInt32)br.ReadInt32();
bih.biXPelsPerMeter = (UInt32)br.ReadInt32();
bih.biYPelsPerMeter = (UInt32)br.ReadInt32();
bih.biClrUsed = (UInt32)br.ReadInt32();
bih.biClrImportant = (UInt32)br.ReadInt32();
//位图深度为8的时候 既256色
if (bih.biBitCount == 8)
{
pixel8 = new int[bih.biWidth, bih.biHeight];
palatte = new byte[(bfh.bfOffBits - 54)];
palatte = br.ReadBytes((int)(bfh.bfOffBits - 54));
for (Int32 j = (Int32)bih.biHeight - 1; j >= 0; j--)
{
for (Int32 i = 0; i < (Int32)bih.biWidth; i++)
//for (Int32 i = (Int32)bih.biWidth - 1; i >= 0; i--)
{
pixel8[i, j] = (int)br.ReadByte();
}
}
}
//24位真彩色位图
else if (bih.biBitCount == 24)
{
pixel24 = new int[bih.biWidth, bih.biHeight, 3];
for (Int32 j = (Int32)bih.biHeight - 1; j >= 0; j--)
{
for (Int32 i = (Int32)bih.biWidth - 1; i >= 0; i--)
{
pixel24[i, j, 0] = (int)br.ReadByte();
pixel24[i, j, 1] = (int)br.ReadByte();
pixel24[i, j, 2] = (int)br.ReadByte();
}
}
}
else
{
MessageBox.Show("Only supported 8 or 24 bits bitmap.");
return;
}
br.Close();
}
}
//获取像素数据
public int[,] GetData()
{
int[,] pix = new int[bih.biWidth, bih.biHeight];
if (bih.biBitCount == 8)
{
for (int j = 0; j < bih.biHeight; j++)
{
for (int i = 0; i < bih.biWidth; i++)
{
pix[i, j] = pixel8[i, j];
}
}
}
else if (bih.biBitCount == 24)
{
for (int j = 0; j < bih.biHeight; j++)
{
for (int i = 0; i < bih.biWidth; i++)
{
pix[i, j] = (int)((pixel24[i, j, 0] + pixel24[i, j, 1] + pixel24[i, j, 2]) / 3);
}
}
}
return pix;
}
//获取图像的宽
public int GetWidth()
{
return (int)bih.biWidth;
}
//获取图像的高
public int GetHeight()
{
return (int)bih.biHeight;
}
}//END CLASS userBitmap
[ 本帖最后由 hxt532084126 于 2013-3-27 15:46 编辑 ]