internal static Image FormatSnapshot(string imagePath, SprSnapshotFormat format) { var imgFile = new Bitmap(imagePath); using (var g = Graphics.FromImage(imgFile)) { g.CompositingQuality = CompositingQuality.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.SmoothingMode = SmoothingMode.HighQuality; g.DrawImage(imgFile, 0, 0, imgFile.Width, imgFile.Height); } var savePath = imagePath; var imgStream = new MemoryStream(); // Must stay open! if (format == SprSnapshotFormat.Png) { savePath = Path.ChangeExtension(imagePath, "png"); imgFile.Save(imgStream, ImageFormat.Png); } else if (format == SprSnapshotFormat.Jpg) { var jpgEncoder = ImageCodecInfo.GetImageDecoders().First(codec => codec.FormatID == ImageFormat.Jpeg.Guid); if (jpgEncoder != null) { // Set the image quality var encodeQuality = Encoder.Quality; var encodeParams = new EncoderParameters(1); var qualityParam = new EncoderParameter(encodeQuality, 95L); encodeParams.Param[0] = qualityParam; savePath = Path.ChangeExtension(imagePath, "jpg"); imgFile.Save(imgStream, jpgEncoder, encodeParams); } } imgFile.Dispose(); return(Image.FromStream(imgStream)); }
internal static Image FormatSnapshot(string imagePath, SprSnapshotFormat format) { var imgFile = new Bitmap(imagePath); using (var g = Graphics.FromImage(imgFile)) { g.CompositingQuality = CompositingQuality.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.SmoothingMode = SmoothingMode.HighQuality; g.DrawImage(imgFile, 0, 0, imgFile.Width, imgFile.Height); } var savePath = imagePath; var imgStream = new MemoryStream(); // Must stay open! if (format == SprSnapshotFormat.Png) { savePath = Path.ChangeExtension(imagePath, "png"); imgFile.Save(imgStream, ImageFormat.Png); } else if(format == SprSnapshotFormat.Jpg) { var jpgEncoder = ImageCodecInfo.GetImageDecoders().First(codec => codec.FormatID == ImageFormat.Jpeg.Guid); if (jpgEncoder != null) { // Set the image quality var encodeQuality = Encoder.Quality; var encodeParams = new EncoderParameters(1); var qualityParam = new EncoderParameter(encodeQuality, 95L); encodeParams.Param[0] = qualityParam; savePath = Path.ChangeExtension(imagePath, "jpg"); imgFile.Save(imgStream, jpgEncoder, encodeParams); } } imgFile.Dispose(); return Image.FromStream(imgStream); }