private static bool SaveNonJpeg(Image imageToSave, ImageFormat imageFormat, Stream stream) { bool needsDispose = false; // Removing transparency if it's not supported in the output if (imageFormat != ImageFormat.Png && Image.IsAlphaPixelFormat(imageToSave.PixelFormat)) { imageToSave = ImageOperator.Clone(imageToSave, PixelFormat.Format24bppRgb); needsDispose = true; } try { imageToSave.Save(stream, imageFormat); return(true); } catch (Exception ex) { log.Error("Failed to save image, " + ex.Message); return(false); } finally { if (needsDispose && imageToSave != null) { imageToSave.Dispose(); imageToSave = null; } } }
private Bitmap CreateNegative(Image sourceImage) { Bitmap clone = ImageOperator.Clone(sourceImage) as Bitmap; ColorMatrix invertMatrix = new ColorMatrix(new float[][] { new float[] { -1, 0, 0, 0, 0 }, new float[] { 0, -1, 0, 0, 0 }, new float[] { 0, 0, -1, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 1, 1, 1, 1, 1 } }); ImageOperator.ApplyColorMatrix(clone, invertMatrix); return(clone); }
private static bool SaveJpeg(Image imageToSave, ImageFormat imageFormat, int jpegQuality, Stream stream) { bool foundEncoder = false; foreach (ImageCodecInfo imageCodec in ImageCodecInfo.GetImageEncoders()) { if (imageCodec.FormatID != imageFormat.Guid) { continue; } foundEncoder = true; try { EncoderParameters parameters = new EncoderParameters(1); parameters.Param[0] = new EncoderParameter(Encoder.Quality, jpegQuality); // Removing transparency if it's not supported in the output if (Image.IsAlphaPixelFormat(imageToSave.PixelFormat)) { Image nonAlphaImage = ImageOperator.Clone(imageToSave, PixelFormat.Format24bppRgb); nonAlphaImage.Save(stream, imageCodec, parameters); nonAlphaImage.Dispose(); nonAlphaImage = null; } else { imageToSave.Save(stream, imageCodec, parameters); } return(true); } catch (Exception ex) { log.Error("Failed to save jpeg, " + ex.Message); } } if (!foundEncoder) { log.Error("No JPG encoder found."); } return(false); }