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

GC-14: FromBitmap, FromFile and FromStream methods for FluxJpeg.Core.Image #19

Open
anders9ustafsson opened this issue Jan 20, 2016 · 0 comments

Comments

@anders9ustafsson
Copy link
Collaborator

From @GoogleCodeExporter on January 20, 2016 12:59

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 ily...@gmail.com on 1 Mar 2015 at 3:18

Attachments:

Copied from original issue: #14

@anders9ustafsson anders9ustafsson changed the title FromBitmap, FromFile and FromStream methods for FluxJpeg.Core.Image GC-14: FromBitmap, FromFile and FromStream methods for FluxJpeg.Core.Image Jan 20, 2016
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