Bitmap To Byte Rgb - CSharp System.Drawing

CSharp examples for System.Drawing:Image Convert

Description

Bitmap To Byte Rgb

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Runtime.InteropServices;
using System.Linq;
using System.IO;//from w w  w .j  av a2 s . com
using System.Drawing.Imaging;
using System.Drawing;
using System.Collections.Generic;
using System;

public class Main{
        public unsafe static byte[, ,] BitmapToByteRgb(Bitmap bmp)
        {
            int width = bmp.Width,
                height = bmp.Height;
            //byte[, ,] result = new byte[3, height, width];
            var result = new byte[height, width, 3];
            var bd = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly,
                PixelFormat.Format24bppRgb);
            try
            {
                byte* curpos;
                for (var h = 0; h < height; h++)
                {
                    curpos = ((byte*)bd.Scan0) + h * bd.Stride;
                    for (var w = 0; w < width; w++)
                    {
                        result[h, w, 2] = *(curpos++);
                        result[h, w, 1] = *(curpos++);
                        result[h, w, 0] = *(curpos++);
                    }
                }
            }
            finally
            {
                bmp.UnlockBits(bd);
            }
            return result;
        }
}

Related Tutorials