From 33dd2b7a27fd00330ecdc10a2d9f7035494871d6 Mon Sep 17 00:00:00 2001 From: Julien Richard Date: Sun, 16 Jan 2022 15:47:27 +0100 Subject: [PATCH] Update --- src/Greenshot.Base/Core/ImageHelper.cs | 44 ++++++++++++++ .../Drawing/EmojiContainer.cs | 59 ++++++------------- 2 files changed, 62 insertions(+), 41 deletions(-) diff --git a/src/Greenshot.Base/Core/ImageHelper.cs b/src/Greenshot.Base/Core/ImageHelper.cs index 60d846eac..e0d9dc731 100644 --- a/src/Greenshot.Base/Core/ImageHelper.cs +++ b/src/Greenshot.Base/Core/ImageHelper.cs @@ -1803,5 +1803,49 @@ namespace Greenshot.Base.Core return returnImage; } + + + /// + /// Rotate the image + /// + /// Input image + /// Angle in degrees + /// Rotated image + public static Image Rotate(this Image image, float rotationAngle) + { + var bitmap = new Bitmap(image.Width, image.Height); + + using var gfx = Graphics.FromImage(bitmap); + gfx.InterpolationMode = InterpolationMode.HighQualityBicubic; + + gfx.TranslateTransform((float)bitmap.Width / 2, (float)bitmap.Height / 2); + gfx.RotateTransform(rotationAngle); + gfx.TranslateTransform(-(float)bitmap.Width / 2, -(float)bitmap.Height / 2); + + gfx.DrawImage(image, new Point(0, 0)); + + return bitmap; + } + + /// + /// Returns a bitmap from a WPF bitmap source + /// + /// + /// Winforms bitmap + public static Bitmap ToBitmap(this System.Windows.Media.Imaging.BitmapSource bitmapSource) + { + var src = new System.Windows.Media.Imaging.FormatConvertedBitmap(); + src.BeginInit(); + src.Source = bitmapSource; + src.DestinationFormat = System.Windows.Media.PixelFormats.Bgra32; + src.EndInit(); + + var bitmap = new Bitmap(src.PixelWidth, src.PixelHeight, PixelFormat.Format32bppArgb); + var data = bitmap.LockBits(new Rectangle(new Point(0, 0), bitmap.Size), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); + src.CopyPixels(System.Windows.Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); + bitmap.UnlockBits(data); + + return bitmap; + } } } \ No newline at end of file diff --git a/src/Greenshot.Editor/Drawing/EmojiContainer.cs b/src/Greenshot.Editor/Drawing/EmojiContainer.cs index ea4faf6b7..334088deb 100644 --- a/src/Greenshot.Editor/Drawing/EmojiContainer.cs +++ b/src/Greenshot.Editor/Drawing/EmojiContainer.cs @@ -31,6 +31,7 @@ using System.Windows.Forms.Integration; using System.Windows.Media; using System.Windows.Media.Imaging; using Emoji.Wpf; +using Greenshot.Base.Core; using Greenshot.Base.Interfaces.Drawing; using Greenshot.Editor.Helpers; using Image = System.Drawing.Image; @@ -182,59 +183,35 @@ namespace Greenshot.Editor.Drawing var rect = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height); var iconSize = Math.Min(rect.Width, rect.Height); - if (iconSize > 0) { - _image.Measure(new Size(iconSize, iconSize)); - _image.Arrange(new Rect(0, 0, iconSize, iconSize)); - - var renderTargetBitmap = new RenderTargetBitmap(iconSize, iconSize, 96, 96, PixelFormats.Pbgra32); - renderTargetBitmap.Render(_image); - - using var bitmap = BitmapFromSource(renderTargetBitmap); - - if (_rotationAngle != 0) - { - using var newBitmap = RotateImage(bitmap, _rotationAngle); - graphics.DrawImage(newBitmap, Bounds); - return; - } - + using var bitmap = GetBitmap(iconSize); graphics.DrawImage(bitmap, Bounds); } } - private Bitmap BitmapFromSource(BitmapSource bitmapSource) + private Image GetBitmap(int iconSize) { - var src = new FormatConvertedBitmap(); - src.BeginInit(); - src.Source = bitmapSource; - src.DestinationFormat = PixelFormats.Bgra32; - src.EndInit(); + _image.Measure(new Size(iconSize, iconSize)); + _image.Arrange(new Rect(0, 0, iconSize, iconSize)); - var bitmap = new Bitmap(src.PixelWidth, src.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb); - var data = bitmap.LockBits(new Rectangle(new Point(0, 0), bitmap.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); - src.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); - bitmap.UnlockBits(data); + var renderTargetBitmap = new RenderTargetBitmap(iconSize, iconSize, 96, 96, PixelFormats.Pbgra32); + renderTargetBitmap.Render(_image); + + var bitmap = renderTargetBitmap.ToBitmap(); + + if (_rotationAngle != 0) + { + var newBitmap = bitmap.Rotate( _rotationAngle); + bitmap.Dispose(); + + return newBitmap; + } return bitmap; } - private static Image RotateImage(Image img, float rotationAngle) - { - var bitmap = new Bitmap(img.Width, img.Height); - - using var gfx = Graphics.FromImage(bitmap); - gfx.InterpolationMode = InterpolationMode.HighQualityBicubic; - - gfx.TranslateTransform((float)bitmap.Width / 2, (float)bitmap.Height / 2); - gfx.RotateTransform(rotationAngle); - gfx.TranslateTransform(-(float)bitmap.Width / 2, -(float)bitmap.Height / 2); - - gfx.DrawImage(img, new Point(0, 0)); - - return bitmap; - } + } internal static class PickerExtensions