zoukankan      html  css  js  c++  java
  • 用指针读BMP图像

    实践中经常用到,故写下备用。

    /// <summary>
    /// 将8bit的BMP图像读入数组文件
    /// </summary>
    /// <param name="filePath">图像文件路径</param>
    /// <returns>图像像素数组</returns>
    private byte[,] ReadImage(string filePath)
    {
        Bitmap MyBitmap = (Bitmap)Bitmap.FromFile(filePath);
        BitmapData data = MyBitmap.LockBits(new Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
        byte[,] pixel = new byte[data.Height, data.Width];
        unsafe
        {
            int i, j;
            byte* ptr = (byte*)(data.Scan0);
            for (i = 0; i < data.Height; i++)
            {
                for (j = 0; j < data.Width; j++)
                {
                    pixel[i, j] = ptr[0];
                    ptr++;
                }
                ptr += data.Stride - data.Width;
            }
        }
        MyBitmap.UnlockBits(data);
        return pixel;
    }

  • 相关阅读:
    delete、truncate、drop的区别
    Java闭包
    visio 画网格图
    GPU服务器中了木马病毒
    visio 同时标注上下标
    自动缩放字体
    latex 图片
    多GPU训练
    texstudio 外部查看器错误
    Linux lsof命令详解
  • 原文地址:https://www.cnblogs.com/guyichang/p/2724956.html
Copyright © 2011-2022 走看看