标题:怎么实现将地址变量换成图片变量呢?
取消只看楼主
hxt532084126
Rank: 1
等 级:新手上路
帖 子:34
专家分:5
注 册:2012-8-26
结帖率:78.57%
已结贴  问题点数:30 回复次数:2 
怎么实现将地址变量换成图片变量呢?
这个类是用来获取图像像素,但传递进去的参数是图片文件的地址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 编辑 ]
搜索更多相关主题的帖子: 图片 public 
2013-03-27 15:24
hxt532084126
Rank: 1
等 级:新手上路
帖 子:34
专家分:5
注 册:2012-8-26
得分:0 
回复 2楼 yhlvht
那给定地址后,给怎么将这个地址赋值给一个字符串变量呢?

[ 本帖最后由 hxt532084126 于 2013-3-27 20:35 编辑 ]
2013-03-27 20:17
hxt532084126
Rank: 1
等 级:新手上路
帖 子:34
专家分:5
注 册:2012-8-26
得分:0 
回复 4楼 yhlvht
成功了,谢谢!!学了两个礼拜c#,什么都不懂,万分感谢!!!
2013-03-27 20:46



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-402077-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.106286 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved