This commit is contained in:
Julien Richard 2022-01-17 09:52:06 +01:00
commit fde1539ac5
2 changed files with 30 additions and 65 deletions

View file

@ -156,7 +156,7 @@ namespace Greenshot.Base.Core
PropertyItem item = image.GetPropertyItem(ExifOrientationId); PropertyItem item = image.GetPropertyItem(ExifOrientationId);
ExifOrientations orientation = (ExifOrientations) item.Value[0]; ExifOrientations orientation = (ExifOrientations)item.Value[0];
// Orient the image. // Orient the image.
switch (orientation) switch (orientation)
{ {
@ -187,7 +187,7 @@ namespace Greenshot.Base.Core
} }
// Set the orientation to be normal, as we rotated the image. // Set the orientation to be normal, as we rotated the image.
item.Value[0] = (byte) ExifOrientations.TopLeft; item.Value[0] = (byte)ExifOrientations.TopLeft;
image.SetPropertyItem(item); image.SetPropertyItem(item);
} }
catch (Exception orientEx) catch (Exception orientEx)
@ -355,7 +355,7 @@ namespace Greenshot.Base.Core
// Bottom Left // Bottom Left
// Top Right // Top Right
// Bottom Right // Bottom Right
using (IFastBitmap fastBitmap = FastBitmap.Create((Bitmap) image)) using (IFastBitmap fastBitmap = FastBitmap.Create((Bitmap)image))
{ {
// find biggest area // find biggest area
foreach (Point checkPoint in checkPoints) foreach (Point checkPoint in checkPoints)
@ -419,7 +419,7 @@ namespace Greenshot.Base.Core
try try
{ {
byte[] srcBuf = new byte[iconStream.Length]; byte[] srcBuf = new byte[iconStream.Length];
iconStream.Read(srcBuf, 0, (int) iconStream.Length); iconStream.Read(srcBuf, 0, (int)iconStream.Length);
int iCount = BitConverter.ToInt16(srcBuf, 4); int iCount = BitConverter.ToInt16(srcBuf, 4);
for (int iIndex = 0; iIndex < iCount; iIndex++) for (int iIndex = 0; iIndex < iCount; iIndex++)
{ {
@ -568,8 +568,8 @@ namespace Greenshot.Base.Core
using (var path = new GraphicsPath()) using (var path = new GraphicsPath())
{ {
Random random = new Random(); Random random = new Random();
int horizontalRegions = (int) Math.Round((float) sourceImage.Width / horizontalToothRange); int horizontalRegions = (int)Math.Round((float)sourceImage.Width / horizontalToothRange);
int verticalRegions = (int) Math.Round((float) sourceImage.Height / verticalToothRange); int verticalRegions = (int)Math.Round((float)sourceImage.Height / verticalToothRange);
Point topLeft = new Point(0, 0); Point topLeft = new Point(0, 0);
Point topRight = new Point(sourceImage.Width, 0); Point topRight = new Point(sourceImage.Width, 0);
@ -1028,7 +1028,7 @@ namespace Greenshot.Base.Core
} }
Rectangle shadowRectangle = new Rectangle(new Point(shadowSize, shadowSize), sourceBitmap.Size); Rectangle shadowRectangle = new Rectangle(new Point(shadowSize, shadowSize), sourceBitmap.Size);
ApplyColorMatrix((Bitmap) sourceBitmap, Rectangle.Empty, returnImage, shadowRectangle, maskMatrix); ApplyColorMatrix((Bitmap)sourceBitmap, Rectangle.Empty, returnImage, shadowRectangle, maskMatrix);
// blur "shadow", apply to whole new image // blur "shadow", apply to whole new image
if (useGdiBlur) if (useGdiBlur)
@ -1069,7 +1069,7 @@ namespace Greenshot.Base.Core
/// <returns>Negative bitmap</returns> /// <returns>Negative bitmap</returns>
public static Bitmap CreateNegative(Image sourceImage) public static Bitmap CreateNegative(Image sourceImage)
{ {
Bitmap clone = (Bitmap) Clone(sourceImage); Bitmap clone = (Bitmap)Clone(sourceImage);
ColorMatrix invertMatrix = new ColorMatrix(new[] ColorMatrix invertMatrix = new ColorMatrix(new[]
{ {
new float[] new float[]
@ -1289,7 +1289,7 @@ namespace Greenshot.Base.Core
sourceImage.VerticalResolution); sourceImage.VerticalResolution);
using (ImageAttributes adjustAttributes = CreateAdjustAttributes(brightness, contrast, gamma)) using (ImageAttributes adjustAttributes = CreateAdjustAttributes(brightness, contrast, gamma))
{ {
ApplyImageAttributes((Bitmap) sourceImage, Rectangle.Empty, newBitmap, Rectangle.Empty, adjustAttributes); ApplyImageAttributes((Bitmap)sourceImage, Rectangle.Empty, newBitmap, Rectangle.Empty, adjustAttributes);
} }
return newBitmap; return newBitmap;
@ -1302,7 +1302,7 @@ namespace Greenshot.Base.Core
/// <returns>Bitmap with grayscale</returns> /// <returns>Bitmap with grayscale</returns>
public static Image CreateGrayscale(Image sourceImage) public static Image CreateGrayscale(Image sourceImage)
{ {
Bitmap clone = (Bitmap) Clone(sourceImage); Bitmap clone = (Bitmap)Clone(sourceImage);
ColorMatrix grayscaleMatrix = new ColorMatrix(new[] ColorMatrix grayscaleMatrix = new ColorMatrix(new[]
{ {
new[] new[]
@ -1352,7 +1352,7 @@ namespace Greenshot.Base.Core
{ {
if (sourceImage is Metafile) if (sourceImage is Metafile)
{ {
return (Image) sourceImage.Clone(); return (Image)sourceImage.Clone();
} }
return CloneArea(sourceImage, Rectangle.Empty, PixelFormat.DontCare); return CloneArea(sourceImage, Rectangle.Empty, PixelFormat.DontCare);
@ -1599,7 +1599,7 @@ namespace Greenshot.Base.Core
toCount &= 0xffffff; toCount &= 0xffffff;
} }
using IFastBitmap bb = FastBitmap.Create((Bitmap) sourceImage); using IFastBitmap bb = FastBitmap.Create((Bitmap)sourceImage);
for (int y = 0; y < bb.Height; y++) for (int y = 0; y < bb.Height; y++)
{ {
for (int x = 0; x < bb.Width; x++) for (int x = 0; x < bb.Width; x++)
@ -1636,11 +1636,11 @@ namespace Greenshot.Base.Core
int destX = 0; int destX = 0;
int destY = 0; int destY = 0;
var nPercentW = newWidth / (float) sourceImage.Width; var nPercentW = newWidth / (float)sourceImage.Width;
var nPercentH = newHeight / (float) sourceImage.Height; var nPercentH = newHeight / (float)sourceImage.Height;
if (maintainAspectRatio) if (maintainAspectRatio)
{ {
if ((int) nPercentW == 1) if ((int)nPercentW == 1)
{ {
nPercentW = nPercentH; nPercentW = nPercentH;
if (canvasUseNewSize) if (canvasUseNewSize)
@ -1648,7 +1648,7 @@ namespace Greenshot.Base.Core
destX = Math.Max(0, Convert.ToInt32((newWidth - sourceImage.Width * nPercentW) / 2)); destX = Math.Max(0, Convert.ToInt32((newWidth - sourceImage.Width * nPercentW) / 2));
} }
} }
else if ((int) nPercentH == 1) else if ((int)nPercentH == 1)
{ {
nPercentH = nPercentW; nPercentH = nPercentW;
if (canvasUseNewSize) if (canvasUseNewSize)
@ -1656,7 +1656,7 @@ namespace Greenshot.Base.Core
destY = Math.Max(0, Convert.ToInt32((newHeight - sourceImage.Height * nPercentH) / 2)); destY = Math.Max(0, Convert.ToInt32((newHeight - sourceImage.Height * nPercentH) / 2));
} }
} }
else if ((int) nPercentH != 0 && nPercentH < nPercentW) else if ((int)nPercentH != 0 && nPercentH < nPercentW)
{ {
nPercentW = nPercentH; nPercentW = nPercentH;
if (canvasUseNewSize) if (canvasUseNewSize)
@ -1690,12 +1690,12 @@ namespace Greenshot.Base.Core
if (maintainAspectRatio && canvasUseNewSize) if (maintainAspectRatio && canvasUseNewSize)
{ {
newImage = CreateEmpty(newWidth, newHeight, sourceImage.PixelFormat, backgroundColor, sourceImage.HorizontalResolution, sourceImage.VerticalResolution); newImage = CreateEmpty(newWidth, newHeight, sourceImage.PixelFormat, backgroundColor, sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
matrix?.Scale((float) newWidth / sourceImage.Width, (float) newHeight / sourceImage.Height, MatrixOrder.Append); matrix?.Scale((float)newWidth / sourceImage.Width, (float)newHeight / sourceImage.Height, MatrixOrder.Append);
} }
else else
{ {
newImage = CreateEmpty(destWidth, destHeight, sourceImage.PixelFormat, backgroundColor, sourceImage.HorizontalResolution, sourceImage.VerticalResolution); newImage = CreateEmpty(destWidth, destHeight, sourceImage.PixelFormat, backgroundColor, sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
matrix?.Scale((float) destWidth / sourceImage.Width, (float) destHeight / sourceImage.Height, MatrixOrder.Append); matrix?.Scale((float)destWidth / sourceImage.Width, (float)destHeight / sourceImage.Height, MatrixOrder.Append);
} }
using (Graphics graphics = Graphics.FromImage(newImage)) using (Graphics graphics = Graphics.FromImage(newImage))
@ -1804,29 +1804,6 @@ 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> /// <summary>
/// Returns a bitmap from a WPF bitmap source /// Returns a bitmap from a WPF bitmap source
/// </summary> /// </summary>

View file

@ -50,7 +50,6 @@ namespace Greenshot.Editor.Drawing
[NonSerialized] private static ElementHost _emojiPickerHost; [NonSerialized] private static ElementHost _emojiPickerHost;
[NonSerialized] private static Picker _emojiPicker; [NonSerialized] private static Picker _emojiPicker;
[NonSerialized] private System.Windows.Controls.Image _image;
[NonSerialized] private bool _justCreated = true; [NonSerialized] private bool _justCreated = true;
[NonSerialized] private Image _cachedImage = null; [NonSerialized] private Image _cachedImage = null;
@ -63,10 +62,7 @@ namespace Greenshot.Editor.Drawing
set set
{ {
_emoji = value; _emoji = value;
if (_image != null) ResetCachedBitmap();
{
global::Emoji.Wpf.Image.SetSource(_image, Emoji);
}
} }
} }
@ -141,10 +137,6 @@ namespace Greenshot.Editor.Drawing
{ {
CreateDefaultAdorners(); CreateDefaultAdorners();
// Create WPF control that will be used to render the emoji
_image = new System.Windows.Controls.Image();
global::Emoji.Wpf.Image.SetSource(_image, Emoji);
PropertyChanged += OnPropertyChanged; PropertyChanged += OnPropertyChanged;
} }
@ -215,23 +207,19 @@ namespace Greenshot.Editor.Drawing
private Image ComputeBitmap(int iconSize) private Image ComputeBitmap(int iconSize)
{ {
_image.Measure(new Size(iconSize, iconSize)); // Create WPF control that will be used to render the emoji
_image.Arrange(new Rect(0, 0, iconSize, iconSize)); var image = new System.Windows.Controls.Image();
global::Emoji.Wpf.Image.SetSource(image, Emoji);
image.RenderTransformOrigin = new System.Windows.Point(0.5, 0.5);
image.RenderTransform = new RotateTransform(_rotationAngle);
image.Measure(new Size(iconSize, iconSize));
image.Arrange(new Rect(0, 0, iconSize, iconSize));
var renderTargetBitmap = new RenderTargetBitmap(iconSize, iconSize, 96, 96, PixelFormats.Pbgra32); var renderTargetBitmap = new RenderTargetBitmap(iconSize, iconSize, 96, 96, PixelFormats.Pbgra32);
renderTargetBitmap.Render(_image); renderTargetBitmap.Render(image);
var bitmap = renderTargetBitmap.ToBitmap(); return renderTargetBitmap.ToBitmap();
if (_rotationAngle != 0)
{
var newBitmap = bitmap.Rotate( _rotationAngle);
bitmap.Dispose();
return newBitmap;
}
return bitmap;
} }
private void ResetCachedBitmap() private void ResetCachedBitmap()