public StreamHolder ResizeStream(Stream inputStream, string name, int maxWidth)
        {
            // Decode
            DecodedJpeg jpegIn = new JpegDecoder(inputStream).Decode();

            if (!ImageResizer.ResizeNeeded(jpegIn.Image, maxWidth))
            {
                return new StreamHolder(inputStream, name);
            }
            else
            {
                // Resize
                DecodedJpeg jpegOut = new DecodedJpeg(
                    new ImageResizer(jpegIn.Image)
                        .Resize(maxWidth, ResamplingFilters.NearestNeighbor),
                    jpegIn.MetaHeaders); // Retain EXIF details

                // Encode
                var outputStream = new MemoryStream();
                new JpegEncoder(jpegOut, 90, outputStream).Encode();
                // Display
                outputStream.Seek(0, SeekOrigin.Begin);
                return new StreamHolder(outputStream, name);
            }
        }
예제 #2
0
파일: JpegEncoder.cs 프로젝트: JuRogn/OA
 public JpegEncoder(DecodedJpeg decodedJpeg, int quality, Stream outStream)
 {
     this._input = decodedJpeg;
     this._input.Image.ChangeColorSpace(ColorSpace.YCbCr);
     this._quality = quality;
     this._height = this._input.Image.Height;
     this._width = this._input.Image.Width;
     this._outStream = outStream;
     this._dct = new DCT(this._quality);
     this._huf = new HuffmanTable(null);
 }
예제 #3
0
        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            var openPicker = new FileOpenPicker
                                 {
                                     ViewMode = PickerViewMode.Thumbnail,
                                     SuggestedStartLocation = PickerLocationId.PicturesLibrary
                                 };
            openPicker.FileTypeFilter.Add(".jpg");
            openPicker.FileTypeFilter.Add(".jpeg");

            var file = await openPicker.PickSingleFileAsync();
            if (file == null) return;

            using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                // Display input image
                var imageIn = new BitmapImage();
                await imageIn.SetSourceAsync(fileStream);
                InputImage.Source = imageIn;

                // Rewind
                fileStream.Seek(0);

                // Decode
                var jpegIn = new JpegDecoder(fileStream.AsStreamForRead()).Decode();

                if (!ImageResizer.ResizeNeeded(jpegIn.Image, 320))
                {
                    OutputImage.Source = null;
                    OutputText.Text = "No resize necessary.";
                    return;
                }

                // Resize
                var jpegOut = new DecodedJpeg(
                    new ImageResizer(jpegIn.Image)
                        .ResizeToScale(320, ResamplingFilters.NearestNeighbor),
                    jpegIn.MetaHeaders); // Retain EXIF details

                // Encode
                using (var outStream = new InMemoryRandomAccessStream())
                {
                    new JpegEncoder(jpegOut, 90, outStream.AsStreamForWrite()).Encode();

                    // Display 
                    outStream.Seek(0);
                    var image = new BitmapImage();
                    await image.SetSourceAsync(outStream);
                    OutputImage.Source = image;
                }
            }
        }
예제 #4
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog() { Filter = "Image files (*.jpg)|*.jpg" };

            if (ofd.ShowDialog() != true) return;

            Stream fileStream = ofd.File.OpenRead();

            // Display input image
            Stream inStream = new MemoryStream(new BinaryReader(fileStream).ReadBytes((int)fileStream.Length));
            BitmapImage imageIn = new BitmapImage();
            imageIn.SetSource(inStream);
            InputImage.Source = imageIn;

            // Rewind
            fileStream.Seek(0, SeekOrigin.Begin);

            using (fileStream)
            {
                // Decode
                DecodedJpeg jpegIn = new JpegDecoder(fileStream).Decode();

                if (!ImageResizer.ResizeNeeded(jpegIn.Image, 320))
                {
                    OutputImage.Source = null;
                    OutputText.Text = "No resize necessary.";
                    return;
                }

                // Resize
                jpegOut = new DecodedJpeg(
                    new ImageResizer(jpegIn.Image)
                        .ResizeToScale(320, ResamplingFilters.NearestNeighbor),
                    jpegIn.MetaHeaders); // Retain EXIF details

                // Encode
                MemoryStream outStream = new MemoryStream();
                new JpegEncoder(jpegOut, 90, outStream).Encode();

                // Display 
                outStream.Seek(0, SeekOrigin.Begin);
                BitmapImage image = new BitmapImage();
                image.SetSource(outStream);
                OutputImage.Source = image;
            }

        }
예제 #5
0
        /// <summary>
        /// Encodes the data of the specified image and writes the result to
        /// the specified stream.
        /// </summary>
        /// <param name="image">The image, where the data should be get from.
        /// Cannot be null (Nothing in Visual Basic).</param>
        /// <param name="stream">The stream, where the image data should be written to.
        /// Cannot be null (Nothing in Visual Basic).</param>
        /// <exception cref="System.ArgumentNullException">
        /// 	<para><paramref name="image"/> is null (Nothing in Visual Basic).</para>
        /// 	<para>- or -</para>
        /// 	<para><paramref name="stream"/> is null (Nothing in Visual Basic).</para>
        /// </exception>
        public void Encode(ExtendedImage image, Stream stream)
        {
            Guard.NotNull(image, "image");
            Guard.NotNull(stream, "stream");
            
            const int bands = 3;

            int pixelWidth  = image.PixelWidth;
            int pixelHeight = image.PixelHeight;

            byte[] sourcePixels = image.Pixels;

            byte[][,] pixels = new byte[bands][,];

            for (int b = 0; b < bands; b++)
            {
                pixels[b] = new byte[pixelWidth, pixelHeight];
            }

            for (int y = 0; y < pixelHeight; y++)
            {
                for (int x = 0; x < pixelWidth; x++)
                {
                    int offset = (y * pixelWidth + x) * 4;

                    float a = (float)sourcePixels[offset + 3] / 255.0f;

                    pixels[0][x, y] = (byte)(sourcePixels[offset + 0] * a + (1 - a) * _transparentColor.R);
                    pixels[1][x, y] = (byte)(sourcePixels[offset + 1] * a + (1 - a) * _transparentColor.G);
                    pixels[2][x, y] = (byte)(sourcePixels[offset + 2] * a + (1 - a) * _transparentColor.B);
                }
            }

            Image newImage = new Image(new ColorModel { ColorSpace = ColorSpace.RGB, Opaque = false }, pixels);

            if (image.DensityXInt32 > 0 && image.DensityYInt32 > 0)
            {
                newImage.DensityX = image.DensityXInt32;
                newImage.DensityY = image.DensityYInt32;
            }

            // Create a jpg image from the image object.
            DecodedJpeg jpg = new DecodedJpeg(newImage);

            // Create a new encoder and start encoding.
            FluxCoreJpegEncoder fluxCoreJpegEncoder = new FluxCoreJpegEncoder(jpg, _quality, stream);
            fluxCoreJpegEncoder.Encode();
        }