blob: 34e3d9ea24ba16bc3f6d5df6923457ddf6004e03 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Threading.Tasks;
public static class BitmapExtensions
{
/// <summary>
/// Saves the specified bitmap as a JPEG file with the specified quality (0-100).
/// </summary>
/// <param name="bitmap">The bitmap.</param>
/// <param name="filePath">The file path.</param>
/// <param name="quality">The quality.</param>
public static void SaveJpeg(this Bitmap bitmap, String filePath, int quality)
{
var encoder = ImageCodecInfo.GetImageEncoders().First(c => c.FormatID == ImageFormat.Jpeg.Guid);
var encParams = new EncoderParameters() { Param = new[] { new EncoderParameter(Encoder.Quality, quality) } };
bitmap.Save(filePath, encoder, encParams);
}
public static Bitmap ConvertTo24Bit(this Bitmap bitmap)
{
var bmp = new Bitmap(bitmap.Width, bitmap.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
using (var gr = Graphics.FromImage(bmp))
gr.DrawImage(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
return bmp;
}
}
|