using System.Drawing; using System.Drawing.Imaging; namespace Developpez.Dotnet.Drawing { /// /// Fournit des méthodes d'extension pour la manipulation d'images Bitmap /// public static class BitmapExtensions { /// /// Rogne l'image d'origine en ne conservant que la zone spécifiée, et renvoie la nouvelle image /// /// L'image à rogner /// La zone de l'image à conserver /// L'image rognée public static Bitmap Crop(this Bitmap bitmap, Rectangle cropRectangle) { return bitmap.Clone(cropRectangle, bitmap.PixelFormat); } /// /// Rogne l'image d'origine en ne conservant que la zone spécifiée, et renvoie la nouvelle image /// /// L'image à rogner /// La zone de l'image à conserver /// L'image rognée public static Bitmap Crop(this Bitmap bitmap, RectangleF cropRectangle) { return bitmap.Clone(cropRectangle, bitmap.PixelFormat); } /// /// Rogne l'image d'origine en ne conservant que la zone spécifiée, et renvoie la nouvelle image /// /// L'image à rogner /// La position horizontale de la zone à conserver /// La position verticale de la zone à conserver /// La largeur de la zone à conserver /// La hauteur de la zone à conserver /// L'image rognée public static Bitmap Crop(this Bitmap bitmap, int x, int y, int width, int height) { Rectangle cropRectangle = new Rectangle(x, y, width, height); return bitmap.Crop(cropRectangle); } /// /// Rogne l'image d'origine en ne conservant que la zone spécifiée, et renvoie la nouvelle image /// /// L'image à rogner /// La position horizontale de la zone à conserver /// La position verticale de la zone à conserver /// La largeur de la zone à conserver /// La hauteur de la zone à conserver /// L'image rognée public static Bitmap Crop(this Bitmap bitmap, float x, float y, float width, float height) { RectangleF cropRectangle = new RectangleF(x, y, width, height); return bitmap.Crop(cropRectangle); } /// /// Applique une matrice de transformation de couleurs à l'image. /// /// L'image à transformer /// La matrice de transformation de couleurs /// L'image transformée. public static Image TransformColors(this Image image, ColorMatrix colorMatrix) { Bitmap newBitmap = new Bitmap(image.Width, image.Height); using (Graphics g = Graphics.FromImage(newBitmap)) { ImageAttributes attributes = new ImageAttributes(); attributes.SetColorMatrix(colorMatrix); g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes); } return newBitmap; } } }