Skip to content

Commit

Permalink
Icons Improve
Browse files Browse the repository at this point in the history
  • Loading branch information
ricaun committed Dec 2, 2023
1 parent 0d1fc69 commit f33f4c9
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Update to support `net7.0-windows` and `net8.0-windows`
- SetImage works with `Resources` without assembly name.
- Change `System.Drawing` to a separate namespace and class.
- Icons `GetBitmapSource` return the biggest frame.
### Updated
- Update `Build` project
- Update `Example` project
Expand All @@ -20,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Test `StackTraceUtils`
- Test `Resources` without assembly name.
- Test `Drawing` Resources.
- Test `ResourceTests`, `ResourcePngTests` and `ResourcesFramesTests`

## [0.5.7] / 2023-11-17
### Features
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<Version>0.6.0-rc</Version>
<Version>0.6.0-rc.2</Version>
</PropertyGroup>
</Project>
8 changes: 8 additions & 0 deletions ricaun.Revit.UI.Tests/Resources/DrawingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,13 @@ public void GetBitmapSource_Icon()
Assert.IsNotNull(image.GetBitmapSource());
}

[TestCase(32)]
public void GetBitmapSource_Icon_Width(int width)
{
Icon image = Images.Resources.Revit;
var source = image.GetBitmapSource();
Assert.AreEqual(width, source.Width);
}

}
}
Binary file not shown.
30 changes: 30 additions & 0 deletions ricaun.Revit.UI.Tests/Resources/ResourcePngTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using NUnit.Framework;
using System;

namespace ricaun.Revit.UI.Tests.Resources
{
public class ResourcePngTests
{
const string ResourceNamePng = "Resources/Images/Revit32.png";

[Test]
public void GetBitmapSource_NotNull_Png()
{
Console.WriteLine(ResourceNamePng.GetBitmapSource());
Assert.IsNotNull(ResourceNamePng.GetBitmapSource());
Assert.IsNotNull(("/" + ResourceNamePng).GetBitmapSource());
}

[TestCase(15, 15)]
[TestCase(16, 16)]
[TestCase(31, 31)]
[TestCase(32, 32)]
[TestCase(48, 32)]
[TestCase(64, 32)]
public void GetBitmapFrame_Width_ScaleDown(int width, int expected)
{
var source = ResourceNamePng.GetBitmapSource().GetBitmapFrame(width);
Assert.AreEqual(expected, (int)Math.Round(source.Width));
}
}
}
28 changes: 19 additions & 9 deletions ricaun.Revit.UI.Tests/Resources/ResourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace ricaun.Revit.UI.Tests.Resources
public class ResourceTests
{
const string ResourceNameIco = "Resources/Images/Revit.ico";
const string ResourceNamePng = "Resources/Images/Revit32.png";

[Test]
public void GetBitmapSource_NotNull()
Expand All @@ -16,14 +15,6 @@ public void GetBitmapSource_NotNull()
Assert.IsNotNull(("/" + ResourceNameIco).GetBitmapSource());
}

[Test]
public void GetBitmapSource_NotNull_Png()
{
Console.WriteLine(ResourceNamePng.GetBitmapSource());
Assert.IsNotNull(ResourceNamePng.GetBitmapSource());
Assert.IsNotNull(("/" + ResourceNamePng).GetBitmapSource());
}

[Test]
public void GetBitmapSource_NotNull_Component()
{
Expand All @@ -39,5 +30,24 @@ public void GetBitmapSource_Null_NotExist()
var source = component.GetBitmapSource();
Assert.IsNull(source);
}

[TestCase(32)]
public void GetBitmapSource_Width(int width)
{
var source = ResourceNameIco.GetBitmapSource();
Assert.AreEqual(width, source.Width);
}

[TestCase(0, 16)]
[TestCase(10, 10)]
[TestCase(16, 16)]
[TestCase(32, 32)]
[TestCase(64, 32)]
[TestCase(128, 32)]
public void GetBitmapFrame_Width(int width, int expected)
{
var source = ResourceNameIco.GetBitmapSource().GetBitmapFrame(width);
Assert.AreEqual(expected, source.Width);
}
}
}
39 changes: 39 additions & 0 deletions ricaun.Revit.UI.Tests/Resources/ResourcesFramesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using NUnit.Framework;
using System;

namespace ricaun.Revit.UI.Tests.Resources
{
public class ResourcesFramesTests
{
const string ResourceNameIcoFrames = "Resources/Images/Revit21Frames.ico";

[Test]
public void Frames_GetBitmapSource_NotNull()
{
Console.WriteLine(ResourceNameIcoFrames.GetBitmapSource());
Assert.IsNotNull(ResourceNameIcoFrames.GetBitmapSource());
Assert.IsNotNull(("/" + ResourceNameIcoFrames).GetBitmapSource());
}

[TestCase(256)]
public void Frames_GetBitmapSource_Width(int width)
{
var source = ResourceNameIcoFrames.GetBitmapSource();
Assert.AreEqual(width, source.Width);
}

[TestCase(0, 16)]
[TestCase(10, 10)]
[TestCase(16, 16)]
[TestCase(32, 32)]
[TestCase(64, 64)]
[TestCase(128, 128)]
[TestCase(256, 256)]
[TestCase(512, 256)]
public void Frames_GetBitmapFrame_Width(int width, int expected)
{
var source = ResourceNameIcoFrames.GetBitmapSource().GetBitmapFrame(width);
Assert.AreEqual(expected, source.Width);
}
}
}
2 changes: 2 additions & 0 deletions ricaun.Revit.UI.Tests/ricaun.Revit.UI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
</Otherwise>
</Choose>
<ItemGroup>
<None Remove="Resources\Images\Revit21Frames.ico" />
<None Remove="Resources\Images\Revit32.png" />
</ItemGroup>

Expand Down Expand Up @@ -73,6 +74,7 @@

<ItemGroup>
<Resource Include="Resources\Images\Revit.ico" />
<Resource Include="Resources\Images\Revit21Frames.ico" />
<Resource Include="Resources\Images\Revit32.png" />
</ItemGroup>

Expand Down
50 changes: 32 additions & 18 deletions ricaun.Revit.UI/BitmapExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static BitmapFrame UriToBitmapFrame(string uriString)
{
var uri = new Uri(uriString, UriKind.RelativeOrAbsolute);
var decoder = BitmapDecoder.Create(uri, BitmapCreateOptions.None, BitmapCacheOption.Default);
return decoder.Frames[0];
return decoder.Frames.OrderBy(e => e.Width).LastOrDefault();
}

/// <summary>
Expand Down Expand Up @@ -87,38 +87,52 @@ public static ImageSource Scale(this ImageSource imageSource, double scale)
/// </summary>
/// <param name="imageSource"></param>
/// <param name="width"></param>
/// <param name="action"></param>
/// <param name="downloadCompleted"></param>
/// <returns></returns>
public static TImageSource GetBitmapFrame<TImageSource>(this TImageSource imageSource, int width = 16, Action<TImageSource> action = null) where TImageSource : ImageSource
/// <remarks>When <paramref name="width"/> is zero, return the smallest width frame.</remarks>
public static TImageSource GetBitmapFrame<TImageSource>(this TImageSource imageSource, int width = 0, Action<TImageSource> downloadCompleted = null) where TImageSource : ImageSource
{
TImageSource ScaleDownIfWidthIsGreater(TImageSource imageSource, int width)
{
if (width <= 0)
return imageSource;

if (imageSource.Width > width)
imageSource = imageSource.Scale(width / imageSource.Width) as TImageSource;

return imageSource;
}

if (imageSource is BitmapFrame bitmapFrame)
{
BitmapFrame GetBitmapFrameByWidth(BitmapFrame bitmapFrame, int width)
{
var frames = bitmapFrame.Decoder.Frames;
var frame = frames
.OrderBy(e => e.Width)
.FirstOrDefault(e => e.Width >= width);

return frame;
}

if (bitmapFrame.IsDownloading)
{
bitmapFrame.DownloadCompleted += (s, e) =>
{
var frames = bitmapFrame.Decoder.Frames;
var frame = frames.FirstOrDefault(e => e.Width == width);
if (GetBitmapFrameByWidth(bitmapFrame, width) is TImageSource frame)
imageSource = frame;

if (frame != null)
imageSource = frame as TImageSource;
imageSource = ScaleDownIfWidthIsGreater(imageSource, width);

if (imageSource.Width > width)
imageSource = imageSource.Scale(width / imageSource.Width) as TImageSource;

action?.Invoke(imageSource);
downloadCompleted?.Invoke(imageSource);
};
}

var frames = bitmapFrame.Decoder.Frames;
var frame = frames.FirstOrDefault(e => e.Width == width);

if (frame != null)
imageSource = frame as TImageSource;
if (GetBitmapFrameByWidth(bitmapFrame, width) is TImageSource frame)
imageSource = frame;
}

if (imageSource.Width > width)
imageSource = imageSource.Scale(width / imageSource.Width) as TImageSource;
imageSource = ScaleDownIfWidthIsGreater(imageSource, width);

return imageSource;
}
Expand Down
3 changes: 2 additions & 1 deletion ricaun.Revit.UI/Drawing/BitmapDrawingExtension.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Windows.Media.Imaging;

namespace ricaun.Revit.UI.Drawing
Expand Down Expand Up @@ -40,7 +41,7 @@ public static BitmapSource GetBitmapSource(this System.Drawing.Icon icon)
var stream = new MemoryStream();
icon.Save(stream);
var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.Default);
return decoder.Frames[0];
return decoder.Frames.OrderBy(e => e.Width).LastOrDefault();
}

/// <summary>
Expand Down

0 comments on commit f33f4c9

Please sign in to comment.