From c3fe6c5b50aceb1b584b9b16e2e28b1651446f58 Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Mon, 1 Mar 2021 20:10:47 +1300 Subject: [PATCH] Refactor image utils into own class --- DisplayMagician/DisplayMagician.csproj | 1 + DisplayMagician/ImageUtils.cs | 127 ++++++++++ .../UIForms/ImageListViewRenderers.cs | 239 +----------------- 3 files changed, 134 insertions(+), 233 deletions(-) create mode 100644 DisplayMagician/ImageUtils.cs diff --git a/DisplayMagician/DisplayMagician.csproj b/DisplayMagician/DisplayMagician.csproj index 97fa9b0..3126af2 100644 --- a/DisplayMagician/DisplayMagician.csproj +++ b/DisplayMagician/DisplayMagician.csproj @@ -106,6 +106,7 @@ + True diff --git a/DisplayMagician/ImageUtils.cs b/DisplayMagician/ImageUtils.cs new file mode 100644 index 0000000..394b811 --- /dev/null +++ b/DisplayMagician/ImageUtils.cs @@ -0,0 +1,127 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Drawing.Imaging; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DisplayMagician +{ + public static class ImageUtils + { + public static Image RoundCorners(Image StartImage, int CornerRadius) + { + CornerRadius *= 2; + Bitmap RoundedImage = new Bitmap(StartImage.Width, StartImage.Height); + using (Graphics g = Graphics.FromImage(RoundedImage)) + { + g.Clear(Color.Transparent); + g.SmoothingMode = SmoothingMode.HighQuality; + Brush brush = new TextureBrush(StartImage); + GraphicsPath gp = new GraphicsPath(); + gp.AddArc(0, 0, CornerRadius, CornerRadius, 180, 90); + gp.AddArc(0 + RoundedImage.Width - CornerRadius, 0, CornerRadius, CornerRadius, 270, 90); + gp.AddArc(0 + RoundedImage.Width - CornerRadius, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90); + gp.AddArc(0, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90); + g.FillPath(brush, gp); + return RoundedImage; + } + } + + public static void DrawRoundedRectangle(Graphics graphics, Pen pen, Rectangle bounds, int cornerRadius) + { + if (graphics == null) + throw new ArgumentNullException("graphics"); + if (pen == null) + throw new ArgumentNullException("pen"); + + using (GraphicsPath path = RoundedRect(bounds, cornerRadius)) + { + graphics.DrawPath(pen, path); + } + } + + public static void FillRoundedRectangle(Graphics graphics, Brush brush, Rectangle bounds, int cornerRadius) + { + if (graphics == null) + throw new ArgumentNullException("graphics"); + if (brush == null) + throw new ArgumentNullException("brush"); + + using (GraphicsPath path = RoundedRect(bounds, cornerRadius)) + { + graphics.FillPath(brush, path); + } + } + + public static GraphicsPath RoundedRect(Rectangle bounds, int radius) + { + int diameter = radius * 2; + Size size = new Size(diameter, diameter); + Rectangle arc = new Rectangle(bounds.Location, size); + GraphicsPath path = new GraphicsPath(); + + if (radius == 0) + { + path.AddRectangle(bounds); + return path; + } + + // top left arc + path.AddArc(arc, 180, 90); + + // top right arc + arc.X = bounds.Right - diameter; + path.AddArc(arc, 270, 90); + + // bottom right arc + arc.Y = bounds.Bottom - diameter; + path.AddArc(arc, 0, 90); + + // bottom left arc + arc.X = bounds.Left; + path.AddArc(arc, 90, 90); + + path.CloseFigure(); + return path; + } + + public static Image MakeGrayscale(Image original) + { + //create a blank bitmap the same size as original + Image newBitmap = new Bitmap(original.Width, original.Height); + + //get a graphics object from the new image + using (Graphics g = Graphics.FromImage(newBitmap)) + { + + //create the grayscale ColorMatrix + ColorMatrix colorMatrix = new ColorMatrix( + new float[][] + { + new float[] {.3f, .3f, .3f, 0, 0}, + new float[] {.59f, .59f, .59f, 0, 0}, + new float[] {.11f, .11f, .11f, 0, 0}, + new float[] {0, 0, 0, 1, 0}, + new float[] {0, 0, 0, 0, 1} + }); + + //create some image attributes + using (ImageAttributes attributes = new ImageAttributes()) + { + + //set the color matrix attribute + attributes.SetColorMatrix(colorMatrix); + + //draw the original image on the new image + //using the grayscale color matrix + g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), + 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes); + } + } + return newBitmap; + } + } +} diff --git a/DisplayMagician/UIForms/ImageListViewRenderers.cs b/DisplayMagician/UIForms/ImageListViewRenderers.cs index bc7876e..ed4a0d7 100644 --- a/DisplayMagician/UIForms/ImageListViewRenderers.cs +++ b/DisplayMagician/UIForms/ImageListViewRenderers.cs @@ -88,7 +88,7 @@ namespace DisplayMagician.UIForms } // Draw the image - Image img = RoundCorners(item.GetCachedImage(CachedImageType.Thumbnail),20); + Image img = ImageUtils.RoundCorners(item.GetCachedImage(CachedImageType.Thumbnail),20); if (img != null) { Rectangle pos = Utility.GetSizedImageBounds(img, new Rectangle(bounds.Location + itemPadding, ImageListView.ThumbnailSize)); @@ -102,7 +102,7 @@ namespace DisplayMagician.UIForms { // THe shortcut is invalid // so we make the image grayscale - Image grayImg = MakeGrayscale(img); + Image grayImg = ImageUtils.MakeGrayscale(img); g.DrawImage(grayImg, pos); // Draw a warning triangle over it @@ -116,7 +116,7 @@ namespace DisplayMagician.UIForms using (Pen pOuterBorder = new Pen(ImageListView.Colors.ImageOuterBorderColor)) { //g.DrawRectangle(pOuterBorder, pos); - DrawRoundedRectangle(g, pOuterBorder, pos,9); + ImageUtils.DrawRoundedRectangle(g, pOuterBorder, pos,9); } } } @@ -145,24 +145,6 @@ namespace DisplayMagician.UIForms } } - public Image RoundCorners(Image StartImage, int CornerRadius) - { - CornerRadius *= 2; - Bitmap RoundedImage = new Bitmap(StartImage.Width, StartImage.Height); - using (Graphics g = Graphics.FromImage(RoundedImage)) - { - g.Clear(Color.Transparent); - g.SmoothingMode = SmoothingMode.HighQuality; - Brush brush = new TextureBrush(StartImage); - GraphicsPath gp = new GraphicsPath(); - gp.AddArc(0, 0, CornerRadius, CornerRadius, 180, 90); - gp.AddArc(0 + RoundedImage.Width - CornerRadius, 0, CornerRadius, CornerRadius, 270, 90); - gp.AddArc(0 + RoundedImage.Width - CornerRadius, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90); - gp.AddArc(0, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90); - g.FillPath(brush, gp); - return RoundedImage; - } - } // Draws the selection rectangle. public override void DrawSelectionRectangle(Graphics g, Rectangle selection) @@ -180,100 +162,6 @@ namespace DisplayMagician.UIForms selection.Width, selection.Height); } } - - public void DrawRoundedRectangle(Graphics graphics, Pen pen, Rectangle bounds, int cornerRadius) - { - if (graphics == null) - throw new ArgumentNullException("graphics"); - if (pen == null) - throw new ArgumentNullException("pen"); - - using (GraphicsPath path = RoundedRect(bounds, cornerRadius)) - { - graphics.DrawPath(pen, path); - } - } - - public void FillRoundedRectangle(Graphics graphics, Brush brush, Rectangle bounds, int cornerRadius) - { - if (graphics == null) - throw new ArgumentNullException("graphics"); - if (brush == null) - throw new ArgumentNullException("brush"); - - using (GraphicsPath path = RoundedRect(bounds, cornerRadius)) - { - graphics.FillPath(brush, path); - } - } - - public GraphicsPath RoundedRect(Rectangle bounds, int radius) - { - int diameter = radius * 2; - Size size = new Size(diameter, diameter); - Rectangle arc = new Rectangle(bounds.Location, size); - GraphicsPath path = new GraphicsPath(); - - if (radius == 0) - { - path.AddRectangle(bounds); - return path; - } - - // top left arc - path.AddArc(arc, 180, 90); - - // top right arc - arc.X = bounds.Right - diameter; - path.AddArc(arc, 270, 90); - - // bottom right arc - arc.Y = bounds.Bottom - diameter; - path.AddArc(arc, 0, 90); - - // bottom left arc - arc.X = bounds.Left; - path.AddArc(arc, 90, 90); - - path.CloseFigure(); - return path; - } - - public static Image MakeGrayscale(Image original) - { - //create a blank bitmap the same size as original - Image newBitmap = new Bitmap(original.Width, original.Height); - - //get a graphics object from the new image - using (Graphics g = Graphics.FromImage(newBitmap)) - { - - //create the grayscale ColorMatrix - ColorMatrix colorMatrix = new ColorMatrix( - new float[][] - { - new float[] {.3f, .3f, .3f, 0, 0}, - new float[] {.59f, .59f, .59f, 0, 0}, - new float[] {.11f, .11f, .11f, 0, 0}, - new float[] {0, 0, 0, 1, 0}, - new float[] {0, 0, 0, 0, 1} - }); - - //create some image attributes - using (ImageAttributes attributes = new ImageAttributes()) - { - - //set the color matrix attribute - attributes.SetColorMatrix(colorMatrix); - - //draw the original image on the new image - //using the grayscale color matrix - g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), - 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes); - } - } - return newBitmap; - } } #pragma warning disable CS3009 // Base type is not CLS-compliant @@ -354,7 +242,7 @@ namespace DisplayMagician.UIForms } // Draw the image - Image img = RoundCorners(item.GetCachedImage(CachedImageType.Thumbnail), 20); + Image img = ImageUtils.RoundCorners(item.GetCachedImage(CachedImageType.Thumbnail), 20); if (img != null) { Rectangle pos = Utility.GetSizedImageBounds(img, new Rectangle(bounds.Location + itemPadding, ImageListView.ThumbnailSize)); @@ -368,7 +256,7 @@ namespace DisplayMagician.UIForms { // THe shortcut is invalid // so we make the image grayscale - Image grayImg = MakeGrayscale(img); + Image grayImg = ImageUtils.MakeGrayscale(img); g.DrawImage(grayImg, pos); // Draw a warning triangle over it @@ -382,7 +270,7 @@ namespace DisplayMagician.UIForms using (Pen pOuterBorder = new Pen(ImageListView.Colors.ImageOuterBorderColor)) { //g.DrawRectangle(pOuterBorder, pos); - DrawRoundedRectangle(g, pOuterBorder, pos, 9); + ImageUtils.DrawRoundedRectangle(g, pOuterBorder, pos, 9); } } } @@ -411,25 +299,6 @@ namespace DisplayMagician.UIForms } } - public Image RoundCorners(Image StartImage, int CornerRadius) - { - CornerRadius *= 2; - Bitmap RoundedImage = new Bitmap(StartImage.Width, StartImage.Height); - using (Graphics g = Graphics.FromImage(RoundedImage)) - { - g.Clear(Color.Transparent); - g.SmoothingMode = SmoothingMode.HighQuality; - Brush brush = new TextureBrush(StartImage); - GraphicsPath gp = new GraphicsPath(); - gp.AddArc(0, 0, CornerRadius, CornerRadius, 180, 90); - gp.AddArc(0 + RoundedImage.Width - CornerRadius, 0, CornerRadius, CornerRadius, 270, 90); - gp.AddArc(0 + RoundedImage.Width - CornerRadius, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90); - gp.AddArc(0, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90); - g.FillPath(brush, gp); - return RoundedImage; - } - } - // Draws the selection rectangle. public override void DrawSelectionRectangle(Graphics g, Rectangle selection) { @@ -446,102 +315,6 @@ namespace DisplayMagician.UIForms selection.Width, selection.Height); } } - - public void DrawRoundedRectangle(Graphics graphics, Pen pen, Rectangle bounds, int cornerRadius) - { - if (graphics == null) - throw new ArgumentNullException("graphics"); - if (pen == null) - throw new ArgumentNullException("pen"); - - using (GraphicsPath path = RoundedRect(bounds, cornerRadius)) - { - graphics.DrawPath(pen, path); - } - } - - public void FillRoundedRectangle(Graphics graphics, Brush brush, Rectangle bounds, int cornerRadius) - { - if (graphics == null) - throw new ArgumentNullException("graphics"); - if (brush == null) - throw new ArgumentNullException("brush"); - - using (GraphicsPath path = RoundedRect(bounds, cornerRadius)) - { - graphics.FillPath(brush, path); - } - } - - public GraphicsPath RoundedRect(Rectangle bounds, int radius) - { - int diameter = radius * 2; - Size size = new Size(diameter, diameter); - Rectangle arc = new Rectangle(bounds.Location, size); - GraphicsPath path = new GraphicsPath(); - - if (radius == 0) - { - path.AddRectangle(bounds); - return path; - } - - // top left arc - path.AddArc(arc, 180, 90); - - // top right arc - arc.X = bounds.Right - diameter; - path.AddArc(arc, 270, 90); - - // bottom right arc - arc.Y = bounds.Bottom - diameter; - path.AddArc(arc, 0, 90); - - // bottom left arc - arc.X = bounds.Left; - path.AddArc(arc, 90, 90); - - path.CloseFigure(); - return path; - } - - public static Image MakeGrayscale(Image original) - { - //create a blank bitmap the same size as original - Image newBitmap = new Bitmap(original.Width, original.Height); - - //get a graphics object from the new image - using (Graphics g = Graphics.FromImage(newBitmap)) - { - - //create the grayscale ColorMatrix - ColorMatrix colorMatrix = new ColorMatrix( - new float[][] - { - new float[] {.3f, .3f, .3f, 0, 0}, - new float[] {.59f, .59f, .59f, 0, 0}, - new float[] {.11f, .11f, .11f, 0, 0}, - new float[] {0, 0, 0, 1, 0}, - new float[] {0, 0, 0, 0, 1} - }); - - //create some image attributes - using (ImageAttributes attributes = new ImageAttributes()) - { - - //set the color matrix attribute - attributes.SetColorMatrix(colorMatrix); - - //draw the original image on the new image - //using the grayscale color matrix - g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), - 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes); - } - } - return newBitmap; - } } - - }