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)"; }
/// <summary> /// Processes the source image and returns a byte array containing the processed image encoded with a specific image format.</summary> /// <param name="formatEncoderParams">The image format of the saved image.</param> /// <returns>An array of bytes containing the processed image.</returns> public byte[] SaveProcessedImageToByteArray(FormatEncoderParams formatEncoderParams) { ImageProcessingJob job = this.GetImageProcessingJob(); return job.SaveProcessedImageToByteArray(this.TemporarySourceImageFilePath, formatEncoderParams); }
/// <summary> /// Processes the source image and saves the output in a Stream with a specific image format.</summary> /// <param name="destStream">The Stream in which the image will be saved.</param> /// <param name="formatEncoderParams">The image format of the saved image.</param> public void SaveProcessedImageToStream(Stream destStream, FormatEncoderParams formatEncoderParams) { ImageProcessingJob job = this.GetImageProcessingJob(); job.SaveProcessedImageToStream(this.TemporarySourceImageFilePath, destStream, formatEncoderParams); }
/// <summary> /// Processes the source image and saves the output in the file system with a specific image format.</summary> /// <param name="destFilePath">The file path of the saved image.</param> /// <param name="formatEncoderParams">The image format of the saved image.</param> public void SaveProcessedImageToFileSystem(string destFilePath, FormatEncoderParams formatEncoderParams) { ImageProcessingJob job = this.GetImageProcessingJob(); job.SaveProcessedImageToFileSystem(this.TemporarySourceImageFilePath, destFilePath, formatEncoderParams); }
/// <summary> /// Returns the output image processed by the control. /// BackgroundColor and quantization are applied according to the specified FormatEncoderParams.</summary> /// <param name="hintFormatEncoderParams">The image format that will be used then to save image.</param> /// <returns>A Bitmap image processed by the control.</returns> public Bitmap GetProcessedImage(FormatEncoderParams hintFormatEncoderParams) { ImageProcessingJob job = this.GetImageProcessingJob(); return job.GetProcessedImage(this.TemporarySourceImageFilePath, hintFormatEncoderParams); }