This commit is contained in:
Julien Richard 2022-01-16 15:47:27 +01:00
commit 33dd2b7a27
2 changed files with 62 additions and 41 deletions

View file

@ -1803,5 +1803,49 @@ namespace Greenshot.Base.Core
return returnImage; return returnImage;
} }
/// <summary>
/// Rotate the image
/// </summary>
/// <param name="image">Input image</param>
/// <param name="rotationAngle">Angle in degrees</param>
/// <returns>Rotated image</returns>
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;
}
/// <summary>
/// Returns a bitmap from a WPF bitmap source
/// </summary>
/// <param name="bitmapSource"></param>
/// <returns>Winforms bitmap</returns>
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;
}
} }
} }

View file

@ -31,6 +31,7 @@ using System.Windows.Forms.Integration;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using Emoji.Wpf; using Emoji.Wpf;
using Greenshot.Base.Core;
using Greenshot.Base.Interfaces.Drawing; using Greenshot.Base.Interfaces.Drawing;
using Greenshot.Editor.Helpers; using Greenshot.Editor.Helpers;
using Image = System.Drawing.Image; using Image = System.Drawing.Image;
@ -182,59 +183,35 @@ namespace Greenshot.Editor.Drawing
var rect = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height); var rect = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height);
var iconSize = Math.Min(rect.Width, rect.Height); var iconSize = Math.Min(rect.Width, rect.Height);
if (iconSize > 0) if (iconSize > 0)
{ {
_image.Measure(new Size(iconSize, iconSize)); using var bitmap = GetBitmap(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;
}
graphics.DrawImage(bitmap, Bounds); graphics.DrawImage(bitmap, Bounds);
} }
} }
private Bitmap BitmapFromSource(BitmapSource bitmapSource) private Image GetBitmap(int iconSize)
{ {
var src = new FormatConvertedBitmap(); _image.Measure(new Size(iconSize, iconSize));
src.BeginInit(); _image.Arrange(new Rect(0, 0, iconSize, iconSize));
src.Source = bitmapSource;
src.DestinationFormat = PixelFormats.Bgra32;
src.EndInit();
var bitmap = new Bitmap(src.PixelWidth, src.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var renderTargetBitmap = new RenderTargetBitmap(iconSize, iconSize, 96, 96, PixelFormats.Pbgra32);
var data = bitmap.LockBits(new Rectangle(new Point(0, 0), bitmap.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); renderTargetBitmap.Render(_image);
src.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
bitmap.UnlockBits(data); var bitmap = renderTargetBitmap.ToBitmap();
if (_rotationAngle != 0)
{
var newBitmap = bitmap.Rotate( _rotationAngle);
bitmap.Dispose();
return newBitmap;
}
return bitmap; 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 internal static class PickerExtensions