using System.Drawing; using System.Drawing.Imaging; namespace GreenshotPlugin.Core { /// /// Wrap an image, make it resizeable /// public class ImageWrapper : IImage { // Underlying image, is used to generate a resized version of it when needed private readonly Image _image; private Image _imageClone; /// /// Factory method /// /// Image /// IImage public static IImage FromImage(Image image) { return image == null ? null : new ImageWrapper(image); } public ImageWrapper(Image image) { // Make sure the orientation is set correctly so Greenshot can process the image correctly ImageHelper.Orientate(image); _image = image; Width = _image.Width; Height = _image.Height; } public void Dispose() { _image.Dispose(); _imageClone?.Dispose(); } /// /// Height of the image, can be set to change /// public int Height { get; set; } /// /// Width of the image, can be set to change. /// public int Width { get; set; } /// /// Size of the image /// public Size Size => new Size(Width, Height); /// /// Pixelformat of the underlying image /// public PixelFormat PixelFormat => Image.PixelFormat; public float HorizontalResolution => Image.HorizontalResolution; public float VerticalResolution => Image.VerticalResolution; public Image Image { get { if (_imageClone == null) { if (_image.Height == Height && _image.Width == Width) { return _image; } } if (_imageClone?.Height == Height && _imageClone?.Width == Width) { return _imageClone; } // Calculate new image clone _imageClone?.Dispose(); _imageClone = ImageHelper.ResizeImage(_image, false, Width, Height, null); return _imageClone; } } } }