Example #1
0
        /// <summary>
        /// Handles the SaveClick event of the _mdImageDialog control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void _mdImageDialog_SaveClick(object sender, EventArgs e)
        {
            try
            {
                var rockContext = new RockContext();
                BinaryFileService binaryFileService = new BinaryFileService(rockContext);

                // load image from database
                var binaryFile = binaryFileService.Get(CropBinaryFileId ?? 0);
                if (binaryFile != null)
                {
                    var croppedBinaryFile = new BinaryFile();
                    binaryFileService.Add(croppedBinaryFile);
                    croppedBinaryFile.IsTemporary      = true;
                    croppedBinaryFile.BinaryFileTypeId = binaryFile.BinaryFileTypeId;
                    croppedBinaryFile.MimeType         = binaryFile.MimeType;
                    croppedBinaryFile.FileName         = binaryFile.FileName;
                    croppedBinaryFile.Description      = binaryFile.Description;

                    using (var sourceStream = binaryFile.ContentStream)
                    {
                        if (sourceStream != null)
                        {
                            croppedBinaryFile.ContentStream = CropImage(sourceStream, binaryFile.MimeType);
                        }
                    }

                    rockContext.SaveChanges();

                    this.BinaryFileId = croppedBinaryFile.Id;
                }

                // Raise the FileSaved event if one is defined by another control.
                if (FileSaved != null)
                {
                    FileSaved(this, e);
                    _lSaveStatus.Visible = true;
                }

                _hfOriginalBinaryFileId.Value = this.BinaryFileId.ToStringSafe();
                _mdImageDialog.Hide();
            }
            catch (ImageResizer.Plugins.Basic.SizeLimits.SizeLimitException)
            {
                // shouldn't happen because we resize it below the limit in CropImage(), but just in case
                var sizeLimits = new ImageResizer.Plugins.Basic.SizeLimits();
                _nbImageWarning.Visible = true;
                _nbImageWarning.Text    = string.Format("The image size exceeds the maximum resolution of {0}x{1}. Press cancel and try selecting a smaller image.", sizeLimits.TotalSize.Width, sizeLimits.TotalSize.Height);
                _mdImageDialog.Show();
            }
        }
Example #2
0
        /// <summary>
        /// Handles the SaveClick event of the _mdImageDialog control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void _mdImageDialog_SaveClick( object sender, EventArgs e )
        {
            try
            {
                var rockContext = new RockContext();
                BinaryFileService binaryFileService = new BinaryFileService(rockContext);

                // load image from database
                var binaryFile = binaryFileService.Get( CropBinaryFileId ?? 0 );
                if ( binaryFile != null )
                {
                    byte[] croppedImage = CropImage( binaryFile.Data.Content, binaryFile.MimeType );

                    BinaryFile croppedBinaryFile = new BinaryFile();
                    croppedBinaryFile.IsTemporary = true;
                    croppedBinaryFile.BinaryFileTypeId = binaryFile.BinaryFileTypeId;
                    croppedBinaryFile.MimeType = binaryFile.MimeType;
                    croppedBinaryFile.FileName = binaryFile.FileName;
                    croppedBinaryFile.Description = binaryFile.Description;
                    croppedBinaryFile.Data = new BinaryFileData();
                    croppedBinaryFile.Data.Content = croppedImage;

                    binaryFileService.Add( croppedBinaryFile );
                    rockContext.SaveChanges();

                    this.BinaryFileId = croppedBinaryFile.Id;
                }

                // Raise the FileSaved event if one is defined by another control.
                if ( FileSaved != null )
                {
                    FileSaved( this, e );
                }

                _mdImageDialog.Hide();
            }
            catch ( ImageResizer.Plugins.Basic.SizeLimits.SizeLimitException )
            {
                // shouldn't happen because we resize it below the limit in CropImage(), but just in case
                var sizeLimits = new ImageResizer.Plugins.Basic.SizeLimits();
                _nbImageWarning.Visible = true;
                _nbImageWarning.Text = string.Format( "The image size exceeds the maximum resolution of {0}x{1}. Press cancel and try selecting a smaller image.", sizeLimits.TotalSize.Width, sizeLimits.TotalSize.Height );
                _mdImageDialog.Show();
            }
        }