C#中使用byte[]数据,生成Bitmap(256色 灰度 BMP位图)源代码
来源:网络来源TAG:生成Bitmap浏览数: 日期:2010-8-4评论:
折叠C# Code复制内容到剪贴板
  1. /// <summary>   
  2.        /// 使用byte[]数据,生成256色灰度 BMP 位图   
  3.        /// </summary>   
  4.        /// <param name="originalImageData"></param>   
  5.        /// <param name="originalWidth"></param>   
  6.        /// <param name="originalHeight"></param>   
  7.        /// <returns></returns>   
  8.        public static Bitmap CreateBitmap(byte[] originalImageData, int originalWidth, int originalHeight)   
  9.        {   
  10.            //指定8位格式,即256色   
  11.            Bitmap resultBitmap = new Bitmap(originalWidth, originalHeight, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);   
  12.   
  13.            //将该位图存入内存中   
  14.            MemoryStream curImageStream = new MemoryStream();   
  15.            resultBitmap.Save(curImageStream, System.Drawing.Imaging.ImageFormat.Bmp);   
  16.            curImageStream.Flush();   
  17.   
  18.            //由于位图数据需要DWORD对齐(4byte倍数),计算需要补位的个数   
  19.            int curPadNum = ((originalWidth * 8 + 31) / 32 * 4) - originalWidth;   
  20.   
  21.            //最终生成的位图数据大小   
  22.            int bitmapDataSize = ((originalWidth * 8 + 31) / 32 * 4) * originalHeight;   
  23.   
  24.            //数据部分相对文件开始偏移,具体可以参考位图文件格式   
  25.            int dataOffset = ReadData(curImageStream, 10, 4);   
  26.   
  27.   
  28.            //改变调色板,因为默认的调色板是32位彩色的,需要修改为256色的调色板   
  29.            int paletteStart = 54;   
  30.            int paletteEnd = dataOffset;   
  31.            int color = 0;   
  32.   
  33.            for (int i = paletteStart; i < paletteEnd; i += 4)   
  34.            {   
  35.                byte[] tempColor = new byte[4];   
  36.                tempColor[0] = (byte)color;   
  37.                tempColor[1] = (byte)color;   
  38.                tempColor[2] = (byte)color;   
  39.                tempColor[3] = (byte)0;   
  40.                color++;   
  41.   
  42.                curImageStream.Position = i;   
  43.                curImageStream.Write(tempColor, 0, 4);   
  44.            }   
  45.   
  46.            //最终生成的位图数据,以及大小,高度没有变,宽度需要调整   
  47.            byte[] destImageData = new byte[bitmapDataSize];   
  48.            int destWidth = originalWidth + curPadNum;   
  49.   
  50.            //生成最终的位图数据,注意的是,位图数据 从左到右,从下到上,所以需要颠倒   
  51.            for (int originalRowIndex = originalHeight - 1; originalRowIndex >= 0; originalRowIndex--)   
  52.            {   
  53.                int destRowIndex = originalHeight - originalRowIndex - 1;   
  54.   
  55.                for (int dataIndex = 0; dataIndex < originalWidth; dataIndex++)   
  56.                {   
  57.                    //同时还要注意,新的位图数据的宽度已经变化destWidth,否则会产生错位   
  58.                    destImageData[destRowIndex * destWidth + dataIndex] = originalImageData[originalRowIndex * originalWidth + dataIndex];   
  59.                }   
  60.            }   
  61.   
  62.   
  63.            //将流的Position移到数据段      
  64.            curImageStream.Position = dataOffset;   
  65.   
  66.            //将新位图数据写入内存中   
  67.            curImageStream.Write(destImageData, 0, bitmapDataSize);   
  68.   
  69.            curImageStream.Flush();   
  70.   
  71.            //将内存中的位图写入Bitmap对象   
  72.            resultBitmap = new Bitmap(curImageStream);   
  73.   
  74.            return resultBitmap;   
  75.        }   
  76.   
  77.        /// <summary>   
  78.        /// 从内存流中指定位置,读取数据   
  79.        /// </summary>   
  80.        /// <param name="curStream"></param>   
  81.        /// <param name="startPosition"></param>   
  82.        /// <param name="length"></param>   
  83.        /// <returns></returns>   
  84.        public static int ReadData(MemoryStream curStream, int startPosition, int length)   
  85.        {   
  86.            int result = -1;   
  87.   
  88.            byte[] tempData = new byte[length];   
  89.            curStream.Position = startPosition;   
  90.            curStream.Read(tempData, 0, length);   
  91.            result = BitConverter.ToInt32(tempData, 0);   
  92.   
  93.            return result;   
  94.        }   
  95.   
  96.        /// <summary>   
  97.        /// 向内存流中指定位置,写入数据   
  98.        /// </summary>   
  99.        /// <param name="curStream"></param>   
  100.        /// <param name="startPosition"></param>   
  101.        /// <param name="length"></param>   
  102.        /// <param name="value"></param>   
  103.        public static void WriteData(MemoryStream curStream, int startPosition, int length, int value)   
  104.        {   
  105.            curStream.Position = startPosition;   
  106.            curStream.Write(BitConverter.GetBytes(value), 0, length);   
  107.        }  
昨天新闻点击排行
一周新闻点击排行
当月新闻点击排行
新闻链接
上一篇文章:c# WinForm标尺与网格
相关评论
正在加载评论列表...
评论表单加载中...