Example #1
0
        internal void SaveAsImage(Stream stream, int width, int height, ImageWriterFormat format)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream", "'stream' cannot be null (Nothing in Visual Basic)");
            }
            if (width <= 0)
            {
                throw new ArgumentOutOfRangeException("width", width, "'width' cannot be less than or equal to zero");
            }
            if (height <= 0)
            {
                throw new ArgumentOutOfRangeException("height", height, "'height' cannot be less than or equal to zero");
            }
            byte[] data = null;
            try
            {
                data = new byte[width * height * 4];
                GetData(data);

                var writer = new ImageWriter();
                writer.Write(data, width, height, 4, format, stream);
            }
            finally
            {
                if (data != null)
                {
                    data = null;
                }
            }
        }
Example #2
0
        public void Write(byte[] bytes, int x, int y, int comp, ImageWriterFormat format, Stream dest)
        {
            try {
                _stream = dest;
                fixed(byte *b = &bytes[0])
                {
                    switch (format)
                    {
                    case ImageWriterFormat.Bmp:
                        Imaging.stbi_write_bmp_to_func(WriteCallback, null, x, y, comp, b);
                        break;

                    case ImageWriterFormat.Tga:
                        Imaging.stbi_write_tga_to_func(WriteCallback, null, x, y, comp, b);
                        break;

                    case ImageWriterFormat.Jpg:
                        Imaging.stbi_write_jpg_to_func(WriteCallback, null, x, y, comp, b, 90);
                        break;

                    case ImageWriterFormat.Png:
                        Imaging.stbi_write_png_to_func(WriteCallback, null, x, y, comp, b, x * comp);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException("format", format, null);
                    }
                }
            }
            finally {
                _stream = null;
            }
        }
Example #3
0
        public void Write(Image image, ImageWriterFormat format, Stream dest)
        {
            if (image?.Data == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            try
            {
                _stream = dest;
                fixed(byte *b = &image.Data[0])
                {
                    switch (format)
                    {
                    case ImageWriterFormat.Bmp:
                        Stb.stbi_write_bmp_to_func(WriteCallback, null, image.Width, image.Height, image.Comp, b);
                        break;

                    case ImageWriterFormat.Tga:
                        Stb.stbi_write_tga_to_func(WriteCallback, null, image.Width, image.Height, image.Comp, b);
                        break;

                    case ImageWriterFormat.Hdr:
                    {
                        var f = new float[image.Data.Length];
                        for (var i = 0; i < image.Data.Length; ++i)
                        {
                            f[i] = image.Data[i] / 255.0f;
                        }

                        fixed(float *fptr = f)
                        {
                            Stb.stbi_write_hdr_to_func(WriteCallback, null, image.Width, image.Height, image.Comp, fptr);
                        }
                    }
                    break;

                    case ImageWriterFormat.Png:
                        Stb.stbi_write_png_to_func(WriteCallback, null, image.Width, image.Height, image.Comp, b, image.Width * image.Comp);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(format), format, null);
                    }
                }
            }
            finally
            {
                _stream = null;
            }
        }
Example #4
0
        private unsafe void SaveAsImage(Stream stream, int width, int height, ImageWriterFormat format)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream", "'stream' cannot be null (Nothing in Visual Basic)");
            }
            if (width <= 0)
            {
                throw new ArgumentOutOfRangeException("width", width, "'width' cannot be less than or equal to zero");
            }
            if (height <= 0)
            {
                throw new ArgumentOutOfRangeException("height", height, "'height' cannot be less than or equal to zero");
            }
            Color[] data = null;
            try
            {
                data = GetColorData();

                // Write
                fixed(Color *ptr = &data[0])
                {
                    var writer = new ImageWriter();

                    switch (format)
                    {
                    case ImageWriterFormat.Jpg:
                        writer.WriteJpg(ptr, width, height, StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream, 90);
                        break;

                    case ImageWriterFormat.Png:
                        writer.WritePng(ptr, width, height, StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream);
                        break;
                    }
                }
            }
            finally
            {
                if (data != null)
                {
                    data = null;
                }
            }
        }