Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements over Image, Bitmap and Icon classes #9

Merged
merged 38 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
8971ff6
fix test name
damiansalvia May 28, 2024
319815d
redefine Image class based on SKBitmap and add missing props/methods;…
damiansalvia Jun 5, 2024
7f0f3e9
make Bitmap inherit from Image class and add missing props/methods
damiansalvia Jun 5, 2024
d4282ca
init HorizontalResolution/VerticalResolution props with default resol…
damiansalvia Jun 5, 2024
8da7bef
add missing props/methods in Icon class according to new defintiion o…
damiansalvia Jun 5, 2024
2aaa42f
take the largest icon by default (and not the lowest) as System.Drawi…
damiansalvia Jun 5, 2024
eb2e37d
add test for Bitmap.MakeTransparent method
damiansalvia Jun 5, 2024
4b15917
fix Icon.ExtractAssociatedIcon to take the largest icon
damiansalvia Jun 5, 2024
fe02e35
fix ExtractIcon to consider size prior to index
damiansalvia Jun 5, 2024
f0b4b5e
add ExtractIcon test cases
damiansalvia Jun 5, 2024
48660bd
implement Image.GetBounds, add GraphicsUnit enum
damiansalvia Jun 6, 2024
7ae345a
fix typo in Color.Transparent
damiansalvia Jun 6, 2024
6645619
add ImageLockMode enum
damiansalvia Jun 6, 2024
b6dbcb4
add PixelFormat enum; implement PixelFormat in Bitmap/Image classes
damiansalvia Jun 6, 2024
c1827be
add RotateFlipType enum, implement Image.RotateFlip method
damiansalvia Jun 6, 2024
5b98308
remove unused parameter from ToPixelFormat
damiansalvia Jun 7, 2024
ac7e3f9
add ImageFlags enum, add Image.Flags basic implementation
damiansalvia Jun 7, 2024
672fa78
minor fix on PixelFormat property's summary
damiansalvia Jun 7, 2024
87ec06e
mark Bitmap.SetResolution as not supported
damiansalvia Jun 7, 2024
dea319e
add Image.GetThumbnailImage
damiansalvia Jun 7, 2024
2ff98f7
add ColorPalette class, implement Image.Palette property
damiansalvia Jun 7, 2024
3d2b4c2
minor improvement
damiansalvia Jun 10, 2024
68a4bf1
add image test cases
damiansalvia Jun 10, 2024
f50653b
fix Dispose pattern
damiansalvia Jun 17, 2024
222406d
make Bitmap/Icon sealed
damiansalvia Jun 17, 2024
c94a88e
Fix typo.
fedeazzato Jun 17, 2024
6cf0b8c
Fix summary open tag declaration
fedeazzato Jun 17, 2024
ec4c06e
Rename properties to match naming convention.
fedeazzato Jun 17, 2024
0c962dc
define Image as abstract
damiansalvia Jun 17, 2024
41b7f09
remove Bitmap destructor (aleady defined in Image class)
damiansalvia Jun 17, 2024
a117fc6
remove CreateInstance method (just use Bitmap constructor)
damiansalvia Jun 17, 2024
a5e125e
add using directive for temp Image instances
damiansalvia Jun 17, 2024
891dc28
simplify SKRect to SKRectI
damiansalvia Jun 18, 2024
c91fd4c
Define GetResourceStream in Image class (instead of Bitmap class)
damiansalvia Jun 18, 2024
bb90c29
fix Bitmap(Type,string) description
damiansalvia Jun 18, 2024
870125e
define Svg class, remove SKBitmap references from Image class and use…
damiansalvia Jun 21, 2024
2a7e006
define SV as sealed
damiansalvia Jun 21, 2024
1e36251
Rename m_Image to InnerImage for property naming consistency.
fedeazzato Jun 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
314 changes: 264 additions & 50 deletions src/Common/Bitmap.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Common/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public static string KnownColorToName(Color color)

#region KnownColors

public static Color Translarent => new(SKColors.Transparent);
public static Color Transparent => new(SKColors.Transparent);
public static Color AliceBlue => new(SKColors.AliceBlue);
public static Color AntiqueWhite => new(SKColors.AntiqueWhite);
public static Color Aqua => new(SKColors.Aqua);
Expand Down
42 changes: 42 additions & 0 deletions src/Common/GraphicsUnit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace GeneXus.Drawing;

/// <summary>
/// Specifies the unit of measure for the given data.
/// </summary>
public enum GraphicsUnit
{
/// <summary>
/// Specifies the world unit as the unit of measure.
/// </summary>
World = 0,

/// <summary>
/// Specifies 1/75 inch as the unit of measure.
/// </summary>
Display = 1,

/// <summary>
/// Specifies a device pixel as the unit of measure.
/// </summary>
Pixel = 2,

/// <summary>
/// Specifies a printer's point (1/72 inch) as the unit of measure.
/// </summary>
Point = 3,

/// <summary>
/// Specifies the inch as the unit of measure.
/// </summary>
Inch = 4,

/// <summary>
/// Specifies the document unit (1/300 inch) as the unit of measure.
/// </summary>
Document = 5,

/// <summary>
/// Specifies the millimeter as the unit of measure.
/// </summary>
Millimeter = 6
}
98 changes: 81 additions & 17 deletions src/Common/Icon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using GeneXus.Drawing.Imaging;
using SkiaSharp;

namespace GeneXus.Drawing;

[Serializable]
public class Icon : IDisposable, ICloneable
public sealed class Icon : IDisposable, ICloneable
{
internal readonly Bitmap m_bitmap;
internal readonly List<IconEntry> m_entries;
internal int m_index { get; private set; } = 0;
internal int EntryIndex { get; private set; }

private Icon(Bitmap bitmap, List<IconEntry> entries, float width, float height)
{
Expand All @@ -30,21 +31,21 @@ private Icon(Bitmap bitmap, List<IconEntry> entries, float width, float height)
if (diff >= pivot)
continue;
pivot = diff;
m_index = i;
EntryIndex = i;
}
}

/// <summary>
/// Initializes a new instance of the <see cref='Icon'/> class from a <see cref='Bitmap'/> instance.
/// </summary>
public Icon(Bitmap bitmap)
: this(bitmap, new List<IconEntry>().Append(new IconEntry { Width = (byte)bitmap.Width, Height = (byte)bitmap.Height }).ToList(), int.MinValue, int.MinValue) { }
: this(bitmap, new() { new IconEntry { Width = (byte)bitmap.Width, Height = (byte)bitmap.Height } }, -1, -1) { }

/// <summary>
/// Initializes a new instance of the <see cref='Icon'/> class from the specified file name.
/// </summary>
public Icon(string filename)
: this(filename, int.MinValue, int.MinValue) { }
: this(filename, byte.MaxValue, byte.MaxValue) { }

/// <summary>
/// Initializes a new instance of the <see cref='Icon'/> class of the specified <see cref='Size'/> from the specified file.
Expand All @@ -62,7 +63,7 @@ public Icon(string filename, float width, float height)
/// Initializes a new instance of the <see cref='Icon'/> class from the specified data stream.
/// </summary>
public Icon(Stream stream)
: this(stream, int.MinValue, int.MinValue) { }
: this(stream, byte.MaxValue, byte.MaxValue) { }

/// <summary>
/// Initializes a new instance of the <see cref='Icon'/> class of the specified size from the specified stream.
Expand All @@ -80,7 +81,7 @@ public Icon(Stream stream, float width, float height)
/// Initializes a new instance of the <see cref='Icon'/> class by copy.
/// </summary>
public Icon(Icon original)
: this(original, int.MinValue, int.MinValue) { }
: this(original, original.Width, original.Height) { }

/// <summary>
/// Initializes a new instance of the <see cref='Icon'/> class by copy and attempts to find a version of the
Expand All @@ -99,7 +100,7 @@ public Icon(Icon original, float width, float height)
/// <summary>
/// Cleans up resources for this <see cref='Icon'/>.
/// </summary>
~Icon() => Dispose();
~Icon() => Dispose(false);

/// <summary>
/// Creates a human-readable string that represents this <see cref='Icon'/>.
Expand All @@ -112,7 +113,13 @@ public Icon(Icon original, float width, float height)
/// <summary>
/// Cleans up resources for this <see cref='Icon'/>.
/// </summary>
public void Dispose() => m_bitmap.Dispose();
public void Dispose()
{
GC.SuppressFinalize(this);
Dispose(true);
}

private void Dispose(bool disposing) => m_bitmap.Dispose();

#endregion

Expand All @@ -134,34 +141,91 @@ public object Clone()
#region Properties

/// <summary>
/// Gets the width of this <see cref='Icon'/>.
/// Gets the Windows handle for this <see cref='Icon'/>. This is not a copy of the handle; do not free it.
/// </summary>
public int Width => m_entries[m_index].Width;
public IntPtr Handle => m_bitmap.Handle;

/// <summary>
/// Gets the height of this <see cref='Icon'/>.
/// </summary>
public int Height => m_entries[m_index].Height;
public int Height => m_entries[EntryIndex].Height;

/// <summary>
/// Gets the size of this <see cref='Icon'/>.
/// </summary>
public Size Size => new(Width, Height);

/// <summary>
/// Gets the width of this <see cref='Icon'/>.
/// </summary>
public int Width => m_entries[EntryIndex].Width;

#endregion


#region Factory

/// <summary>
/// Creates a GDI+ <see cref='Icon'/> from the specified Windows handle to an icon (HICON).
/// </summary>
public static Icon FromHandle(IntPtr handle)
{
var info = new SKImageInfo(100, 100);
var skBitmap = new SKBitmap();
skBitmap.InstallPixels(info, handle, info.RowBytes);
var bitmap = new Bitmap(skBitmap, ImageFormat.Ico);
return new Icon(bitmap);
}

#endregion


#region Methods

/// <summary>
/// Converts this <see cref='Icon'/> to a <see cref='Bitmap'/>.
/// Returns an icon representation of an image that is contained in the specified file.
/// </summary>
public static Icon ExtractAssociatedIcon(string filePath)
=> ExtractIcon(filePath, -1); // NOTE: https://stackoverflow.com/a/37419253

/// <summary>
/// Extracts a specified icon from the given <paramref name="filePath"/>.
/// </summary>
public Bitmap ToBitmap() => new(m_Resized);
public static Icon ExtractIcon(string filePath, int id, bool smallIcon = false)
=> ExtractIcon(filePath, id, smallIcon ? 8 : ushort.MaxValue);

/// <summary>
/// Extracts a specified icon from the given <paramref name="filePath"/> and specified size.
/// </summary>
public static Icon ExtractIcon(string filePath, int id, int size)
{
if (size is <= 0 or > ushort.MaxValue)
throw new ArgumentOutOfRangeException(nameof(size));

if (new[] { ".dll", ".exe" }.Contains(Path.GetExtension(filePath)))
throw new ArgumentException("portable executable (PE) file format is not supported.", nameof(filePath));

var icon = new Icon(filePath, size, size);
if (icon.Width == size && icon.Height == size)
id = int.MaxValue; // set to undefined

if (id >= 0 && id < icon.m_entries.Count)
icon.EntryIndex = id; // set defined index

return icon;
}

/// <summary>
/// Saves this <see cref='Icon'/> to the specified output <see cref='Stream'/>.
/// </summary>
public void Save(Stream stream) => new Bitmap(m_Resized).Save(stream, SKEncodedImageFormat.Png, 100);
public void Save(Stream stream)
=> ToBitmap().Save(stream, ImageFormat.Png, 100);

/// <summary>
/// Converts this <see cref='Icon'/> to a <see cref='Bitmap'/>.
/// </summary>
public Bitmap ToBitmap()
=> new(Resized, ImageFormat.Png);

#endregion

Expand Down Expand Up @@ -214,11 +278,11 @@ private static List<IconEntry> ReadIco(Stream stream)
return entries;
}

private SKBitmap m_Resized
private SKBitmap Resized
{
get
{
var original = m_bitmap.Clone() as Bitmap ?? throw new Exception("Cloning bitmap failed");
var original = m_bitmap.Clone() as Bitmap ?? throw new Exception("cloning bitmap failed.");
var resized = new SKBitmap(Width, Height);
original.m_bitmap.ScalePixels(resized, SKFilterQuality.High);
return resized;
Expand Down
Loading