Beispiel #1
0
    public bool d = true;//debug mode

    public byte[] imageToByteArray(System.Drawing.Image imageIn)
    {
        int        t1 = Environment.TickCount;
        var        o  = System.Drawing.GraphicsUnit.Pixel;
        RectangleF r1 = imageIn.GetBounds(ref o);
        Rectangle  r2 = new Rectangle((int)r1.X, (int)r1.Y, (int)r1.Width, (int)r1.Height);

        System.Drawing.Imaging.BitmapData omg = ((Bitmap)imageIn).LockBits(r2, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        byte[] rgbValues = new byte[r2.Width * r2.Height * 4];
        Marshal.Copy((IntPtr)omg.Scan0, rgbValues, 0, rgbValues.Length);
        ((Bitmap)imageIn).UnlockBits(omg);
        Debug.Log("i2ba time: " + (Environment.TickCount - t1));
        //flip
        byte[] nbytes    = new byte[rgbValues.Length];//new byte
        var    RGBwidth  = r2.Width * 4;
        var    RGBheight = r2.Height;

        for (var y = 0; y < RGBheight; y++)
        {
            for (var x = 0; x < RGBwidth; x++)
            {
                nbytes[x + RGBwidth * y] = rgbValues[x + RGBwidth * (RGBheight - 1 - y)];
            }
        }
        return(nbytes);
    }