Exemplo n.º 1
0
        public void getAsEncodedStream(Stream imageStream, string type = null, int quality = 90)
        {
            if (type != null) {
                type = this.type != "" ? this.type : JPEG.MIME;
            }

            if (type == JPEG.MIME) // Encode as JPEG
            {
                byte[][,] raster = _bm.ToRaster();

                FluxJpeg.Core.Image jpegImage = new FluxJpeg.Core.Image(new ColorModel {
                    colorspace = ColorSpace.RGB
                }, raster);

                JpegEncoder jpegEncoder = new JpegEncoder(jpegImage, quality, imageStream);
                jpegEncoder.Encode();

                // TODO: restore jpeg headers here
            }
            else if (type == PNG.MIME) // Encode as PNG
            {
                PngEncoder pngEncoder = new PngEncoder(_bm.Pixels, _bm.PixelWidth, _bm.PixelHeight, false, PngEncoder.FILTER_NONE, Deflater.BEST_COMPRESSION);
                byte[] pngBuffer = pngEncoder.pngEncode();
                imageStream.Write(pngBuffer, 0, pngBuffer.Length);
            }
            else
            {
                Error(this, null);
            }
        }
Exemplo n.º 2
0
Arquivo: Image.cs Projeto: 6eee/moxie
        public MemoryStream getAsEncodedStream(string type = null, int quality = 90)
        {
            MemoryStream imageStream = new MemoryStream();

            if (type != null) {
                type = this.type != "" ? this.type : JPEG.MIME;
            }

            if (type == JPEG.MIME) // Encode as JPEG
            {
                byte[][,] raster = _bm.ToRaster();

                FluxJpeg.Core.Image jpegImage = new FluxJpeg.Core.Image(new ColorModel {
                    colorspace = ColorSpace.RGB
                }, raster);

                JpegEncoder jpegEncoder = new JpegEncoder(jpegImage, quality, imageStream);
                jpegEncoder.Encode();

                if (_img != null) {
                    // strip off any headers that might be left by encoder, etc
                    imageStream = new MemoryStream(((JPEG)_img).stripHeaders(imageStream));

                    if (_preserveHeaders) {
                        imageStream = new MemoryStream(((JPEG)_img).insertHeaders(imageStream));
                    }
                }
            }
            else if (type == PNG.MIME) // Encode as PNG
            {
                PngEncoder pngEncoder = new PngEncoder(_bm.Pixels, _bm.PixelWidth, _bm.PixelHeight, false, PngEncoder.FILTER_NONE, Deflater.BEST_COMPRESSION);
                byte[] pngBuffer = pngEncoder.pngEncode();
                imageStream.Write(pngBuffer, 0, pngBuffer.Length);
            }
            else
            {
                Error(this, null);
                return null;
            }
            return imageStream;
        }