Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FromBitmap, FromFile and FromStream methods for FluxJpeg.Core.Image #14

Open
GoogleCodeExporter opened this issue Jun 8, 2015 · 0 comments

Comments

@GoogleCodeExporter
Copy link

I haven't found means to contact the project owners directly, so I'm attaching 
a patched Image.cs to this issue with added methods for those who are wondering 
how to use JpegEncoder with Bitmaps generated by means of .NET code, and for 
those who wish to load raster data from GDI+ supported image formats:

        public static Image FromStream(Stream inStream)
        {
            using (Bitmap bmp = new Bitmap(Bitmap.FromStream(inStream)))
            {
                return FromBitmap(bmp);
            }
        }

        public static Image FromFile(string filePath)
        {
            using (Bitmap bmp = new Bitmap(Bitmap.FromFile(filePath)))
            {
                return FromBitmap(bmp);
            }
        }

        public static Image FromBitmap(Bitmap bitmap)
        {
            int width = bitmap.Width;
            int height = bitmap.Height;
            int bands = 3;
            byte[][,] raster = new byte[bands][,];

            for (int i = 0; i < bands; i++)
            {
                raster[i] = new byte[width, height];
            }

            BitmapData bd = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            int[] pixels = new int[bd.Width * bd.Height];
            Marshal.Copy(bd.Scan0, pixels, 0, pixels.Length);

            bitmap.UnlockBits(bd);

            for (int row = 0; row < height; row++)
            {
                for (int column = 0; column < width; column++)
                {
                    int pixel = pixels[width * row + column];
                    raster[0][column, row] = (byte)(pixel >> 16);
                    raster[1][column, row] = (byte)(pixel >> 8);
                    raster[2][column, row] = (byte)pixel;
                }
            }

            ColorModel model = new ColorModel { colorspace = ColorSpace.RGB };

            return new Image(model, raster);
        }

Best wishes,
Ilya Melamed

Original issue reported on code.google.com by [email protected] on 1 Mar 2015 at 3:18

Attachments:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant