C#使用内存和指针方式将字节数组转换为Bitmap

        /// <summary>
        /// 指针方式转
        /// </summary>
        /// <param name="Width">图像的宽</param>
        /// <param name="Height">图像的高</param>
        /// <param name="pointer">指针</param>
        private void Mono8ToBitmap(int Width,int Height,IntPtr pointer)
        {
            Bitmap bmp = new Bitmap(Width, Height, Width * 1, PixelFormat.Format8bppIndexed, pointer);
            ColorPalette cp = bmp.Palette;
            // init palette
            for (int i = 0; i < 256; i++)
            {
                cp.Entries[i] = Color.FromArgb(i, i, i);
            }
            // set palette back
            bmp.Palette = cp;
            pictureBox1.Invalidate();
        }

        private void BytesToBitmap(int Width,int Height)
        {
            //一,内存复制方式:
            Bitmap myimg = new Bitmap(Width, Height, PixelFormat.Format8bppIndexed);
            BitmapData mydata;
            mydata = myimg.LockBits(new Rectangle(0, 0, myimg.Width, myimg.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            System.Runtime.InteropServices.Marshal.Copy(b, 0, mydata.Scan0, b.Length);
            myimg.UnlockBits(mydata);
            pictureBox1.Invalidate();
        }

 

上一篇:asp.net web api中的版本管理


下一篇:蓝桥c++2013真题:前缀判断(代码填空)