Beispiel #1
0
        /// <summary>
        /// Load a new image
        /// </summary>
        private void _miFileOpen_Click(object sender, System.EventArgs e)
        {
            RasterCodecs    _codecs   = null;
            CodecsImageInfo imageInfo = null;

            try
            {
                // initialize the codecs object.
                _codecs = new RasterCodecs();
                // Since we are dealing with large images, we do not want to allocate the entire image. We are only going to load it row by row
                _codecs.Options.Load.AllocateImage    = false;
                _codecs.Options.Load.StoreDataInImage = false;
                _codecs.LoadImage += new EventHandler <CodecsLoadImageEventArgs>(codecs_LoadImage);

                // load the image
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Title       = "Open File";
                openFileDialog.Multiselect = false;
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    //Check if image is valid for this demo
                    imageInfo = _codecs.GetInformation(openFileDialog.FileName, false);
                    if (!IsImageValid(imageInfo))
                    {
                        Messager.ShowError(this, "The input image has to be 8-bit Gray scale, 12-bit Gray scale, 16-bit Gray scale, RGB (color), and TopLeft view perspective." +
                                           " This DEMO is not meant to be used with small or palletized images (like GIF, PNG, or 1-bit images)." +
                                           " Use this DEMO to save large dimension images efficiently using JPEG2000 compression.");
                        return;
                    }

                    using (RasterSaveDialog saveDialog = new RasterSaveDialog(_codecs))
                    {
                        saveDialog.AutoProcess = false;
                        saveDialog.Title       = "Save As";
                        saveDialog.ShowFileOptionsBasicJ2kOptions = true;
                        saveDialog.ShowFileOptionsJ2kOptions      = true;
                        saveDialog.ShowOptions                = true;
                        saveDialog.ShowQualityFactor          = true;
                        saveDialog.ShowFileOptionsProgressive = true;
                        saveDialog.ShowFileOptionsStamp       = true;
                        saveDialog.QualityFactor              = 20;
                        SetupFormats(saveDialog);

                        if (saveDialog.ShowDialog(this) != DialogResult.OK)
                        {
                            return;
                        }

                        _lblFilenameValue.Text          = Path.GetFileName(openFileDialog.FileName);
                        _lblDimensionValue.Text         = String.Format("{0} x {1}", imageInfo.Width, imageInfo.Height);
                        _lblImageBitsPerPixelValue.Text = imageInfo.BitsPerPixel.ToString();

                        //Get the selected compression type
                        CodecsCompression selectedCompression;
                        if (saveDialog.Format == RasterImageFormat.J2k)
                        {
                            selectedCompression = CodecsCompression.J2k;
                        }
                        else
                        {
                            selectedCompression = CodecsCompression.Jp2;
                        }

                        RasterByteOrder     rasterByteOrder     = ((saveDialog.BitsPerPixel == 12) || (saveDialog.BitsPerPixel == 16)) ? RasterByteOrder.Gray : RasterByteOrder.Bgr;
                        CodecsLoadByteOrder codecsLoadByteOrder = ((saveDialog.BitsPerPixel == 12) || (saveDialog.BitsPerPixel == 16)) ? CodecsLoadByteOrder.Gray : CodecsLoadByteOrder.Bgr;
                        bytesPerLine = CalculateBytesPerLine(saveDialog.BitsPerPixel, imageInfo.Width);

                        _codecs.Options.Jpeg.Save.QualityFactor = saveDialog.QualityFactor;
                        _codecs.Options.Jpeg.Save.Passes        = saveDialog.Passes;

                        _codecs.Options.Jpeg.Save.SaveWithStamp     = saveDialog.WithStamp;
                        _codecs.Options.Jpeg.Save.StampWidth        = saveDialog.StampWidth;
                        _codecs.Options.Jpeg.Save.StampHeight       = saveDialog.StampHeight;
                        _codecs.Options.Jpeg.Save.StampBitsPerPixel = saveDialog.StampBitsPerPixel;

                        _codecs.Options.Jpeg2000.Save.CompressionControl        = saveDialog.FileJ2kOptions.CompressionControl;
                        _codecs.Options.Jpeg2000.Save.CompressionRatio          = saveDialog.FileJ2kOptions.CompressionRatio;
                        _codecs.Options.Jpeg2000.Save.DecompositionLevels       = saveDialog.FileJ2kOptions.DecompositionLevels;
                        _codecs.Options.Jpeg2000.Save.DerivedQuantization       = saveDialog.FileJ2kOptions.DerivedQuantization;
                        _codecs.Options.Jpeg2000.Save.ImageAreaHorizontalOffset = saveDialog.FileJ2kOptions.ImageAreaHorizontalOffset;
                        _codecs.Options.Jpeg2000.Save.ImageAreaVerticalOffset   = saveDialog.FileJ2kOptions.ImageAreaVerticalOffset;
                        _codecs.Options.Jpeg2000.Save.ProgressingOrder          = saveDialog.FileJ2kOptions.ProgressingOrder;
                        _codecs.Options.Jpeg2000.Save.ReferenceTileHeight       = saveDialog.FileJ2kOptions.ReferenceTileHeight;
                        _codecs.Options.Jpeg2000.Save.ReferenceTileWidth        = saveDialog.FileJ2kOptions.ReferenceTileWidth;
                        _codecs.Options.Jpeg2000.Save.RegionOfInterest          = saveDialog.FileJ2kOptions.RegionOfInterest;
                        _codecs.Options.Jpeg2000.Save.RegionOfInterestRectangle = saveDialog.FileJ2kOptions.RegionOfInterestRectangle;
                        _codecs.Options.Jpeg2000.Save.RegionOfInterestWeight    = saveDialog.FileJ2kOptions.RegionOfInterestWeight;
                        _codecs.Options.Jpeg2000.Save.TargetFileSize            = saveDialog.FileJ2kOptions.TargetFileSize;
                        _codecs.Options.Jpeg2000.Save.TileHorizontalOffset      = saveDialog.FileJ2kOptions.TileHorizontalOffset;
                        _codecs.Options.Jpeg2000.Save.TileVerticalOffset        = saveDialog.FileJ2kOptions.TileVerticalOffset;
                        _codecs.Options.Jpeg2000.Save.UseColorTransform         = saveDialog.FileJ2kOptions.UseColorTransform;
                        _codecs.Options.Jpeg2000.Save.UseEphMarker        = saveDialog.FileJ2kOptions.UseEphMarker;
                        _codecs.Options.Jpeg2000.Save.UseRegionOfInterest = saveDialog.FileJ2kOptions.UseRegionOfInterest;
                        _codecs.Options.Jpeg2000.Save.UseSopMarker        = saveDialog.FileJ2kOptions.UseSopMarker;

                        bCancel      = false;
                        bCompressing = true;
                        UpdateMyControls();

                        //Start Compressing
                        using (outputFile = File.Create(saveDialog.FileName))
                        {
                            _codecs.StartCompress(imageInfo.Width, imageInfo.Height, saveDialog.BitsPerPixel, rasterByteOrder, RasterViewPerspective.TopLeft, bytesPerLine, IntPtr.Zero, 0, selectedCompression, MyCodecsCompressDataCallback);
                            _codecs.Load(openFileDialog.FileName, saveDialog.BitsPerPixel, codecsLoadByteOrder, 1, 1);
                            _codecs.StopCompress();

                            _lblStatusValue.Text = bCancel ? "Aborted" : "Complete";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Messager.ShowError(this, ex);
                _lblStatusValue.Text = "Error";
            }
            finally
            {
                if (_codecs != null)
                {
                    _codecs.Dispose();
                }

                if (imageInfo != null)
                {
                    imageInfo.Dispose();
                }
                bCompressing = false;
                UpdateMyControls();
            }
        }