protected void ProcessUploadSuccess() { string sourceImageClientFileName = this._SourceImageClientFileName; if (this.HasImage) { // Unload the current image this.UnloadImage(false); } // Delete old files if (File.Exists(this.TemporarySourceImageFilePath)) { File.Delete(this.TemporarySourceImageFilePath); } if (this._ImageUploadPreProcessingFilter == null) { // Just copy the source image File.Copy(this.UploadFilePath, this.TemporarySourceImageFilePath, true); } try { if (this._ImageUploadPreProcessingFilter != null) { // Pre-process the just uploaded image using (LoadedImage sourceImage = ImageArchiver.LoadImage(this.UploadFilePath)) { // Use PNG to preserve transparency FormatEncoderParams format = new PngFormatEncoderParams(); using (System.Drawing.Image tempImage = this._ImageUploadPreProcessingFilter.GetProcessedImage(sourceImage, sourceImage.Resolution, format)) { ImageArchiver.SaveImageToFileSystem(tempImage, this.TemporarySourceImageFilePath, format); // Optimization: save server resources... this.popupPictureTrimmer1.SetLoadImageData_ImageSize(tempImage.Size); this.popupPictureTrimmer1.SetLoadImageData_ImageResolution(sourceImage.Resolution); this.popupPictureTrimmer1.SetLoadImageData_ImageFormatId(sourceImage.FormatId); } } } // Load the image in the PictureTrimmer control this.popupPictureTrimmer1.LoadImageFromFileSystem(this.TemporarySourceImageFilePath, this._OutputResolution, this.CropConstraint); } catch (InvalidImageSizeException ex) { // Invalid image size ex.ToString(); // Display the invalid image size message this.SetCurrentStatusMessage(this.StatusMessage_InvalidImageSize, true); // EVENT: Upload error (invalid image size) this.OnUploadError(EventArgs.Empty); } catch { // Invalid image // Display the invalid image message this.SetCurrentStatusMessage(this.StatusMessage_InvalidImage, true); // EVENT: Upload error (invalid image) this.OnUploadError(EventArgs.Empty); } if (this.HasImage) { // Restore the source image client file name (changed in the UnloadImage method) this._SourceImageClientFileName = sourceImageClientFileName; // The new image has been uploaded this._ImageUploaded = true; this._ImageEdited = false; // Update the preview this._UpdatePreview = true; if (this.ImageUpload != null) { // EVENT: Image upload string pictureTrimmerTID = this.popupPictureTrimmer1.TemporaryFileId; ImageUploadEventArgs args = new ImageUploadEventArgs(this._OutputResolution, this.CropConstraint, this.PostProcessingFilter, this.PreviewFilter); this.OnImageUpload(args); if (this.HasImage) { if (this.popupPictureTrimmer1.TemporaryFileId != pictureTrimmerTID) { // The image has been reloeaded outside the control if (this.AutoOpenImageEditPopupAfterUpload) { // Open the image edit popup if necessary this.OpenImageEditPopup(); } // Exit !!! return; } } else { // The image has been unloaded, exit. return; } bool reloadImage = false; if (args.OutputResolutionChanged) { this._OutputResolution = args.OutputResolution; reloadImage = true; } if (args.CropConstraintChanged) { this._CropConstraint = args.CropConstraint; reloadImage = true; } if (args.PostProcessingFilterChanged) { this._PostProcessingFilter = args.PostProcessingFilter; // No need to reload if only the post processing filter has changed // AND - the updatePreview is surely already TRUE } if (args.PreviewFilterChanged) { this._PreviewFilter = args.PreviewFilter; // No need to reload if only the preview filter has changed // AND - the updatePreview is surely already TRUE } if (args.ReloadImageSet) { // Forced to reload the source image reloadImage = true; } if (reloadImage) { // Reload the image if (!args.ReloadImageSet) { // Standard reload, use the current source image size, resolutaion and format to save memory this.popupPictureTrimmer1.SetLoadImageData_ImageSize(this.SourceImageSize); this.popupPictureTrimmer1.SetLoadImageData_ImageResolution(this.SourceImageResolution); this.popupPictureTrimmer1.SetLoadImageData_ImageFormatId(this.SourceImageFormatId); } this.popupPictureTrimmer1.LoadImageFromFileSystem(this.TemporarySourceImageFilePath, this._OutputResolution, this.CropConstraint); } } // Invoke the OpenImageEditPopup after the event, so the eventhandler may change // the AutoOpenImageEditPopupAfterUpload property if (this.AutoOpenImageEditPopupAfterUpload) { // Open the image edit popup this.OpenImageEditPopup(); } } }
protected void SaveImage() { string savedImageFileName = ""; FormatEncoderParams formatEncoderParams = null; object source = null; switch (this.ddlSourceType.SelectedIndex) { case 0: // File system source = this.imgLoaded.ImageUrl; break; case 1: // Byte array source = System.IO.File.ReadAllBytes(Server.MapPath(this.imgLoaded.ImageUrl)); break; case 2: // Stream source = System.IO.File.OpenRead(Server.MapPath(this.imgLoaded.ImageUrl)); // Note: The stream will be automatically closed/disposed by the LoadedImage class break; } using (LoadedImage loadedImage = ImageArchiver.LoadImage(source)) { switch (this.ddlImageFormat.SelectedValue) { case "Auto": formatEncoderParams = ImageArchiver.GetDefaultFormatEncoderParams(); break; case "JPEG": formatEncoderParams = new JpegFormatEncoderParams(int.Parse(this.txtJpegQuality.Text)); break; case "GIF": GifFormatEncoderParams gifFormatEncoderParams = new GifFormatEncoderParams(); formatEncoderParams = gifFormatEncoderParams; gifFormatEncoderParams.QuantizeImage = this.cbGifQuantize.Checked; if (this.cbGifQuantize.Checked) { gifFormatEncoderParams.MaxColors = int.Parse(this.ddlGifMaxColors.SelectedValue); } break; case "PNG": PngFormatEncoderParams pngformatEncoderParams = new PngFormatEncoderParams(); formatEncoderParams = pngformatEncoderParams; pngformatEncoderParams.ConvertToIndexed = this.cbPngConvertToIndex.Checked; if (this.cbPngConvertToIndex.Checked) { pngformatEncoderParams.MaxColors = int.Parse(this.ddlPngMaxColors.SelectedValue); } break; } savedImageFileName = "~/repository/output/Ex_A_103" + formatEncoderParams.FileExtension; // IMPORTANT NOTE: Apply a Noop (No Operation) filter to prevent a known GDI+ problem with transparent images // Example: http://forums.asp.net/t/1235100.aspx switch (this.ddlOutputType.SelectedIndex) { case 0: // File system new NoopFilter().SaveProcessedImageToFileSystem(loadedImage.Image, savedImageFileName, formatEncoderParams); break; case 1: // Byte array byte[] imageBytes = new NoopFilter().SaveProcessedImageToByteArray(loadedImage.Image, formatEncoderParams); System.IO.File.WriteAllBytes(Server.MapPath(savedImageFileName), imageBytes); break; case 2: // Stream using (System.IO.Stream stream = System.IO.File.OpenWrite(Server.MapPath(savedImageFileName))) { new NoopFilter().SaveProcessedImageToStream(loadedImage.Image, stream, formatEncoderParams); } break; } } // Update the displayed image (add a timestamp parameter to the query URL to ensure that the image is reloaded by the browser) this.imgSaved.ImageUrl = savedImageFileName + "?timestamp=" + DateTime.UtcNow.Ticks.ToString(); this.phOutputPreview.Visible = true; // Display extension and size of both files this.litLoadedImageFileDetails.Text = System.IO.Path.GetExtension(this.imgLoaded.ImageUrl).ToUpper() + " file (" + (new System.IO.FileInfo(Server.MapPath(this.imgLoaded.ImageUrl))).Length.ToString() + " bytes)"; this.litSavedImageFileDetails.Text = System.IO.Path.GetExtension(savedImageFileName).ToUpper() + " file (" + (new System.IO.FileInfo(Server.MapPath(savedImageFileName))).Length.ToString() + " bytes)"; }
protected void SaveImage() { string savedImageFileName = ""; FormatEncoderParams formatEncoderParams = null; object source = null; switch (this.ddlSourceType.SelectedIndex) { case 0: // File system source = this.imgLoaded.ImageUrl; break; case 1: // Byte array source = System.IO.File.ReadAllBytes(Server.MapPath(this.imgLoaded.ImageUrl)); break; case 2: // Stream source = System.IO.File.OpenRead(Server.MapPath(this.imgLoaded.ImageUrl)); // Note: The stream will be automatically closed/disposed by the LoadedImage class break; } using (LoadedImage loadedImage = ImageArchiver.LoadImage(source)) { switch(this.ddlImageFormat.SelectedValue) { case "Auto": formatEncoderParams = ImageArchiver.GetDefaultFormatEncoderParams(); break; case "JPEG": formatEncoderParams = new JpegFormatEncoderParams(int.Parse(this.txtJpegQuality.Text)); break; case "GIF": GifFormatEncoderParams gifFormatEncoderParams = new GifFormatEncoderParams(); formatEncoderParams = gifFormatEncoderParams; gifFormatEncoderParams.QuantizeImage = this.cbGifQuantize.Checked; if (this.cbGifQuantize.Checked) { gifFormatEncoderParams.MaxColors = int.Parse(this.ddlGifMaxColors.SelectedValue); } break; case "PNG": PngFormatEncoderParams pngformatEncoderParams = new PngFormatEncoderParams(); formatEncoderParams = pngformatEncoderParams; pngformatEncoderParams.ConvertToIndexed = this.cbPngConvertToIndex.Checked; if (this.cbPngConvertToIndex.Checked) { pngformatEncoderParams.MaxColors = int.Parse(this.ddlPngMaxColors.SelectedValue); } break; } savedImageFileName = "~/repository/output/Ex_A_103" + formatEncoderParams.FileExtension; // IMPORTANT NOTE: Apply a Noop (No Operation) filter to prevent a known GDI+ problem with transparent images // Example: http://forums.asp.net/t/1235100.aspx switch (this.ddlOutputType.SelectedIndex) { case 0: // File system new NoopFilter().SaveProcessedImageToFileSystem(loadedImage.Image, savedImageFileName, formatEncoderParams); break; case 1: // Byte array byte[] imageBytes = new NoopFilter().SaveProcessedImageToByteArray(loadedImage.Image, formatEncoderParams); System.IO.File.WriteAllBytes(Server.MapPath(savedImageFileName), imageBytes); break; case 2: // Stream using (System.IO.Stream stream = System.IO.File.OpenWrite(Server.MapPath(savedImageFileName))) { new NoopFilter().SaveProcessedImageToStream(loadedImage.Image, stream, formatEncoderParams); } break; } } // Update the displayed image (add a timestamp parameter to the query URL to ensure that the image is reloaded by the browser) this.imgSaved.ImageUrl = savedImageFileName + "?timestamp=" + DateTime.UtcNow.Ticks.ToString(); this.phOutputPreview.Visible = true; // Display extension and size of both files this.litLoadedImageFileDetails.Text = System.IO.Path.GetExtension(this.imgLoaded.ImageUrl).ToUpper() + " file (" + (new System.IO.FileInfo(Server.MapPath(this.imgLoaded.ImageUrl))).Length.ToString() + " bytes)"; this.litSavedImageFileDetails.Text = System.IO.Path.GetExtension(savedImageFileName).ToUpper() + " file (" + (new System.IO.FileInfo(Server.MapPath(savedImageFileName))).Length.ToString() + " bytes)"; }