• 用指针读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;
    }

  • 相关阅读:
    English trip V1
    English trip M1
    every day a practice —— morning(5)
    English Voice of <<All Of Me>>
    bzoj 3561 DZY Loves Math VI
    luogu P4322 [JSOI2016]最佳团体
    luogu P3264 [JLOI2015]管道连接
    bzoj 5084 hashit
    luogu P6091 原根
    bzoj 5206 [Jsoi2017]原力
  • 原文地址:https://www.cnblogs.com/guyichang/p/2724956.html
Copyright © 2020-2023  润新知