当前位置:编程学习 > C#/ASP.NET >>

关于C# GDAL读图到byte[],然后图像输出到picturebox的问题

一开始用的方法是可以的,但两个for循环的效率实在不敢恭维
        private void btnCompare_Click(object sender, EventArgs e)
        {
            Gdal.AllRegister();
            Dataset ds = Gdal.Open(arrFilePath[0].ToString(), Access.GA_ReadOnly);

            int n = ds.RasterCount;
            Band redBand = null;

            redBand = ds.GetRasterBand(1);

            int width = redBand.XSize;
            int height = redBand.YSize;

            Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb);

            byte[] r = new byte[width * height];

            redBand.ReadRaster(0, 0, ds.RasterXSize, ds.RasterYSize, r, width, height, 0, 0);

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    Color newColor = Color.FromArgb(Convert.ToInt32(r[i + j * width] * 255), Convert.ToInt32(r[i + j * width] * 255), Convert.ToInt32(r[i + j * width] * 255));
                    bitmap.SetPixel(i, j, newColor);
                }
            }

            pictureBox1.Image = bitmap;

        }

----------------------------------------------------------------------------------------------

后来想用MemoryStream的方式显示,可是碰到FromStream参数异常的问题

        private void btnCompare_Click(object sender, EventArgs e)
        {
            Gdal.AllRegister();
            Dataset ds = Gdal.Open(arrFilePath[0].ToString(), Access.GA_ReadOnly);

            int n = ds.RasterCount;
            Band redBand = null;

            redBand = ds.GetRasterBand(1);

            int width = redBand.XSize;
            int height = redBand.YSize;

            byte[] r = new byte[width * height];

            redBand.ReadRaster(0, 0, ds.RasterXSize, ds.RasterYSize, r, width, height, 0, 0);

            MemoryStream ms = new MemoryStream(); //新建内存流
            ms.Write(r, 0, r.Length); //附值
            pictureBox1.Image = Image.FromStream(ms); //读取流中内容
        }

现在有r[]数组,里面存了从gdal里读的图像数据,现在我想知道有什么办法能让它快速在picturebox中显示,要效率 C# GDAL  Bitmap MemoryStream --------------------编程问答-------------------- byte*  
BitmapData
用指针操作 --------------------编程问答-------------------- 要考虑不同色深的图片啊,偏移量不一样 --------------------编程问答-------------------- 一开始用的指针,不想用那个方法 --------------------编程问答--------------------
引用 3 楼 taimao888 的回复:
一开始用的指针,不想用那个方法
why --------------------编程问答-------------------- 这东西明显要先LockBits,然后用指针 填充数据。 

注意Bitmap扫描行的大小必须是4的倍数。

你代码里r[i + j * width] * 255  这里还乘以个255和解啊,那个肯定结果不对了啊。  --------------------编程问答-------------------- 给你完整的代码,绝对实现你的目的。另,本人很需要分

        BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, imageWidth, imageHeight), ImageLockMode.ReadWrite, pixelFormat);

        try
        {
            int stride = bitmapData.Stride;
            IntPtr buf = bitmapData.Scan0;

            ds.ReadRaster(xOff, yOff, width, height, buf, imageWidth, imageHeight, dataType,
                channelCount, bandMap, pixelSpace, stride, 1);
        }
        finally
        {
            bitmap.UnlockBits(bitmapData);
        }

        bitmap.Save(filename); --------------------编程问答-------------------- 不多,学习了
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,