| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 620 人关注过本帖
标题:怎么实现将地址变量换成图片变量呢?
只看楼主 加入收藏
hxt532084126
Rank: 1
等 级:新手上路
帖 子:34
专家分:5
注 册:2012-8-26
结帖率:78.57%
收藏
已结贴  问题点数:30 回复次数:4 
怎么实现将地址变量换成图片变量呢?
这个类是用来获取图像像素,但传递进去的参数是图片文件的地址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
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:30 
Image im = pictureBox1.Image;  //获取pictureBox中的Image
im.Save(path);    //将Image另存为指定路径
剩下的你应该懂的
2013-03-27 18:19
hxt532084126
Rank: 1
等 级:新手上路
帖 子:34
专家分:5
注 册:2012-8-26
收藏
得分:0 
回复 2楼 yhlvht
那给定地址后,给怎么将这个地址赋值给一个字符串变量呢?

[ 本帖最后由 hxt532084126 于 2013-3-27 20:35 编辑 ]
2013-03-27 20:17
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:0 
比如你想存在D盘,文件名1.bmp,那么
string path = "D:\\1.bmp";
Image im = pictureBox1.Image;
im.Save(path);   
这个path既用于另存图片的路径,也是你用于获取图片像素那个类所用到的路径
2013-03-27 20:39
hxt532084126
Rank: 1
等 级:新手上路
帖 子:34
专家分:5
注 册:2012-8-26
收藏
得分:0 
回复 4楼 yhlvht
成功了,谢谢!!学了两个礼拜c#,什么都不懂,万分感谢!!!
2013-03-27 20:46
快速回复:怎么实现将地址变量换成图片变量呢?
数据加载中...
 
   



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

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