├── .gitattributes ├── .gitignore ├── BluwolfIcons.Demo ├── App.config ├── BluwolfIcons.Demo.csproj ├── Program.cs └── Properties │ └── AssemblyInfo.cs ├── BluwolfIcons.Tests ├── BluwolfIcons.Tests.csproj ├── BmpIconImageTests.cs ├── Helper.cs ├── IconTests.cs ├── PngIconImageTests.cs └── Properties │ └── AssemblyInfo.cs ├── BluwolfIcons.sln ├── BluwolfIcons ├── BluwolfIcons.csproj ├── BluwolfIcons.nuspec ├── BluwolfIcons.ruleset ├── BmpIconImage.cs ├── IIconImage.cs ├── Icon.cs ├── PngIconImage.cs ├── Properties │ └── AssemblyInfo.cs ├── icon.ico ├── icon.png └── nuget.exe ├── LICENSE └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | [Xx]64/ 19 | [Xx]86/ 20 | [Bb]uild/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | artifacts/ 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opendb 80 | *.opensdf 81 | *.sdf 82 | *.cachefile 83 | *.VC.db 84 | 85 | # Visual Studio profiler 86 | *.psess 87 | *.vsp 88 | *.vspx 89 | *.sap 90 | 91 | # TFS 2012 Local Workspace 92 | $tf/ 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | *.DotSettings.user 101 | 102 | # JustCode is a .NET coding add-in 103 | .JustCode 104 | 105 | # TeamCity is a build add-in 106 | _TeamCity* 107 | 108 | # DotCover is a Code Coverage Tool 109 | *.dotCover 110 | 111 | # NCrunch 112 | _NCrunch_* 113 | .*crunch*.local.xml 114 | nCrunchTemp_* 115 | 116 | # MightyMoose 117 | *.mm.* 118 | AutoTest.Net/ 119 | 120 | # Web workbench (sass) 121 | .sass-cache/ 122 | 123 | # Installshield output folder 124 | [Ee]xpress/ 125 | 126 | # DocProject is a documentation generator add-in 127 | DocProject/buildhelp/ 128 | DocProject/Help/*.HxT 129 | DocProject/Help/*.HxC 130 | DocProject/Help/*.hhc 131 | DocProject/Help/*.hhk 132 | DocProject/Help/*.hhp 133 | DocProject/Help/Html2 134 | DocProject/Help/html 135 | 136 | # Click-Once directory 137 | publish/ 138 | 139 | # Publish Web Output 140 | *.[Pp]ublish.xml 141 | *.azurePubxml 142 | 143 | # TODO: Un-comment the next line if you do not want to checkin 144 | # your web deploy settings because they may include unencrypted 145 | # passwords 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # NuGet Packages 150 | *.nupkg 151 | # The packages folder can be ignored because of Package Restore 152 | **/packages/* 153 | # except build/, which is used as an MSBuild target. 154 | !**/packages/build/ 155 | # Uncomment if necessary however generally it will be regenerated when needed 156 | #!**/packages/repositories.config 157 | # NuGet v3's project.json files produces more ignoreable files 158 | *.nuget.props 159 | *.nuget.targets 160 | 161 | # Microsoft Azure Build Output 162 | csx/ 163 | *.build.csdef 164 | 165 | # Microsoft Azure Emulator 166 | ecf/ 167 | rcf/ 168 | 169 | # Microsoft Azure ApplicationInsights config file 170 | ApplicationInsights.config 171 | 172 | # Windows Store app package directory 173 | AppPackages/ 174 | BundleArtifacts/ 175 | 176 | # Visual Studio cache files 177 | # files ending in .cache can be ignored 178 | *.[Cc]ache 179 | # but keep track of directories ending in .cache 180 | !*.[Cc]ache/ 181 | 182 | # Others 183 | ClientBin/ 184 | [Ss]tyle[Cc]op.* 185 | ~$* 186 | *~ 187 | *.dbmdl 188 | *.dbproj.schemaview 189 | *.pfx 190 | *.publishsettings 191 | node_modules/ 192 | orleans.codegen.cs 193 | 194 | # RIA/Silverlight projects 195 | Generated_Code/ 196 | 197 | # Backup & report files from converting an old project file 198 | # to a newer Visual Studio version. Backup files are not needed, 199 | # because we have git ;-) 200 | _UpgradeReport_Files/ 201 | Backup*/ 202 | UpgradeLog*.XML 203 | UpgradeLog*.htm 204 | 205 | # SQL Server files 206 | *.mdf 207 | *.ldf 208 | 209 | # Business Intelligence projects 210 | *.rdl.data 211 | *.bim.layout 212 | *.bim_*.settings 213 | 214 | # Microsoft Fakes 215 | FakesAssemblies/ 216 | 217 | # GhostDoc plugin setting file 218 | *.GhostDoc.xml 219 | 220 | # Node.js Tools for Visual Studio 221 | .ntvs_analysis.dat 222 | 223 | # Visual Studio 6 build log 224 | *.plg 225 | 226 | # Visual Studio 6 workspace options file 227 | *.opt 228 | 229 | # Visual Studio LightSwitch build output 230 | **/*.HTMLClient/GeneratedArtifacts 231 | **/*.DesktopClient/GeneratedArtifacts 232 | **/*.DesktopClient/ModelManifest.xml 233 | **/*.Server/GeneratedArtifacts 234 | **/*.Server/ModelManifest.xml 235 | _Pvt_Extensions 236 | 237 | # LightSwitch generated files 238 | GeneratedArtifacts/ 239 | ModelManifest.xml 240 | 241 | # Paket dependency manager 242 | .paket/paket.exe 243 | 244 | # FAKE - F# Make 245 | .fake/ -------------------------------------------------------------------------------- /BluwolfIcons.Demo/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /BluwolfIcons.Demo/BluwolfIcons.Demo.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {B5B411C5-B470-4FA0-AF3D-B6B3D014F28B} 8 | Exe 9 | Properties 10 | BluwolfIcons.Demo 11 | BluwolfIcons.Demo 12 | v4.6.1 13 | 512 14 | true 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | {EB396D32-149A-4AA3-9720-51F1409CC68A} 58 | BluwolfIcons 59 | 60 | 61 | 62 | 69 | -------------------------------------------------------------------------------- /BluwolfIcons.Demo/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Linq; 4 | using System.Windows.Media.Imaging; 5 | 6 | namespace BluwolfIcons.Demo 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | if (args.Length > 0 && args[0] == "read") 13 | { 14 | var icon = Icon.Load("icon.ico"); 15 | 16 | int saveIndex = 0; 17 | foreach (var image in icon.Images.OfType()) 18 | { 19 | Console.WriteLine($"{image.Width}x{image.Height}x{image.BitsPerPixel}\t{saveIndex}.png"); 20 | 21 | var encoder = new PngBitmapEncoder(); 22 | encoder.Frames.Add(BitmapFrame.Create(image.OriginalImage)); 23 | using (var stream = File.Open($"{saveIndex}.png", FileMode.Create)) 24 | { 25 | encoder.Save(stream); 26 | } 27 | 28 | saveIndex++; 29 | } 30 | 31 | foreach (var image in icon.Images.OfType()) 32 | { 33 | Console.WriteLine($"{image.Width}x{image.Height}x{image.BitsPerPixel}\t{saveIndex}.bmp"); 34 | 35 | var encoder = new BmpBitmapEncoder(); 36 | encoder.Frames.Add(BitmapFrame.Create(image.OriginalImage)); 37 | using (var stream = File.Open($"{saveIndex}.bmp", FileMode.Create)) 38 | { 39 | encoder.Save(stream); 40 | } 41 | 42 | saveIndex++; 43 | } 44 | 45 | icon.Save("newicon.ico"); 46 | Console.ReadKey(); 47 | } 48 | else 49 | { 50 | var images = Directory.EnumerateFiles("./", "*.png"); 51 | 52 | var icon = new Icon(); 53 | foreach (var image in images) 54 | { 55 | var bitmap = new BitmapImage(); 56 | bitmap.BeginInit(); 57 | bitmap.UriSource = new Uri(Path.GetFullPath(image)); 58 | bitmap.CacheOption = BitmapCacheOption.OnLoad; 59 | bitmap.EndInit(); 60 | 61 | icon.Images.Add(new PngIconImage(bitmap)); 62 | icon.Images.Add(new BmpIconImage(bitmap)); 63 | } 64 | 65 | icon.Save("icon.ico"); 66 | Console.WriteLine($"Wrote {icon.Images.Count} icon images to 'icon.ico'."); 67 | Console.ReadKey(); 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /BluwolfIcons.Demo/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("BluwolfIcons.Demo")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("BluwolfIcons.Demo")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("b5b411c5-b470-4fa0-af3d-b6b3d014f28b")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /BluwolfIcons.Tests/BluwolfIcons.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | {C167D7FB-6F59-4883-AA86-CC498F2C7BE4} 7 | Library 8 | Properties 9 | BluwolfIcons.Tests 10 | BluwolfIcons.Tests 11 | v4.6.1 12 | 512 13 | {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 14 | 10.0 15 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 16 | $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages 17 | False 18 | UnitTest 19 | 20 | 21 | true 22 | full 23 | false 24 | bin\Debug\ 25 | DEBUG;TRACE 26 | prompt 27 | 4 28 | 29 | 30 | pdbonly 31 | true 32 | bin\Release\ 33 | TRACE 34 | prompt 35 | 4 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | {eb396d32-149a-4aa3-9720-51f1409cc68a} 64 | BluwolfIcons 65 | 66 | 67 | 68 | 69 | 70 | 71 | False 72 | 73 | 74 | False 75 | 76 | 77 | False 78 | 79 | 80 | False 81 | 82 | 83 | 84 | 85 | 86 | 87 | 94 | -------------------------------------------------------------------------------- /BluwolfIcons.Tests/BmpIconImageTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using System; 3 | using System.Windows.Media; 4 | 5 | namespace BluwolfIcons.Tests 6 | { 7 | [TestClass] 8 | public class BmpIconImageTests 9 | { 10 | [TestMethod] 11 | [ExpectedException(typeof(ArgumentException))] 12 | public void Constructor_ImageTooBigWithoutMap_ThrowsException() 13 | { 14 | var bitmap = Helper.GenerateImage(257, 257, PixelFormats.Bgra32); 15 | var target = new BmpIconImage(bitmap, true); 16 | } 17 | 18 | [TestMethod] 19 | [ExpectedException(typeof(ArgumentException))] 20 | public void SetOriginalImage_ImageTooBigWithoutMap_ThrowsException() 21 | { 22 | var valid = Helper.GenerateImage(16, 16, PixelFormats.Bgra32); 23 | var invalid = Helper.GenerateImage(257, 257, PixelFormats.Bgra32); 24 | 25 | var target = new BmpIconImage(valid, true); 26 | target.OriginalImage = invalid; 27 | } 28 | 29 | [TestMethod] 30 | public void Constructor_BiggerImageWithMap_DoesntThrowException() 31 | { 32 | var bitmap = Helper.GenerateImage(256, 512, PixelFormats.Bgra32); 33 | var target = new BmpIconImage(bitmap, false); 34 | 35 | Assert.AreEqual(target.OriginalImage, bitmap); 36 | } 37 | 38 | [TestMethod] 39 | public void SetOriginalImage_BiggerImageTooBigWithMap_DoesntThrowException() 40 | { 41 | var small = Helper.GenerateImage(16, 16, PixelFormats.Bgra32); 42 | var big = Helper.GenerateImage(256, 512, PixelFormats.Bgra32); 43 | 44 | var target = new BmpIconImage(small, false); 45 | target.OriginalImage = big; 46 | 47 | Assert.AreEqual(target.OriginalImage, big); 48 | } 49 | 50 | [TestMethod] 51 | [ExpectedException(typeof(ArgumentException))] 52 | public void Constructor_ImageTooBigWithMap_ThrowsException() 53 | { 54 | var bitmap = Helper.GenerateImage(513, 513, PixelFormats.Bgra32); 55 | var target = new BmpIconImage(bitmap, false); 56 | } 57 | 58 | [TestMethod] 59 | [ExpectedException(typeof(ArgumentException))] 60 | public void SetOriginalImage_ImageTooBigWithMap_ThrowsException() 61 | { 62 | var valid = Helper.GenerateImage(16, 16, PixelFormats.Bgra32); 63 | var invalid = Helper.GenerateImage(513, 513, PixelFormats.Bgra32); 64 | 65 | var target = new BmpIconImage(valid, false); 66 | target.OriginalImage = invalid; 67 | } 68 | 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /BluwolfIcons.Tests/Helper.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using System.Windows.Media; 3 | using System.Windows.Media.Imaging; 4 | 5 | namespace BluwolfIcons.Tests 6 | { 7 | internal static class Helper 8 | { 9 | public static BitmapSource GenerateImage(int width, int height, PixelFormat format) 10 | { 11 | var stride = width * (format.BitsPerPixel / 8); 12 | byte[] buffer = new byte[stride * height]; 13 | 14 | return BitmapSource.Create(width, height, 90.0, 90.0, format, null, buffer, stride); 15 | } 16 | 17 | public static byte[] ToBytes(this BitmapSource source) 18 | { 19 | var encoder = new BmpBitmapEncoder(); 20 | encoder.Frames.Add(BitmapFrame.Create(source)); 21 | 22 | using (var stream = new MemoryStream()) 23 | { 24 | encoder.Save(stream); 25 | return stream.GetBuffer(); 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /BluwolfIcons.Tests/IconTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using System.IO; 3 | using System.Windows.Media; 4 | 5 | namespace BluwolfIcons.Tests 6 | { 7 | [TestClass] 8 | public class IconTests 9 | { 10 | [TestMethod] 11 | public void Save_GeneratesValidIcon() 12 | { 13 | var dummy1 = Helper.GenerateImage(16, 16, PixelFormats.Bgra32); 14 | var dummy2 = Helper.GenerateImage(256, 256, PixelFormats.Bgra32); 15 | 16 | var icon = new Icon(); 17 | icon.Images.Add(new PngIconImage(dummy1)); 18 | icon.Images.Add(new BmpIconImage(dummy1)); 19 | 20 | icon.Images.Add(new PngIconImage(dummy2)); 21 | icon.Images.Add(new BmpIconImage(dummy2)); 22 | 23 | Icon newIcon = null; 24 | byte[] data = null; 25 | using (var stream = new MemoryStream()) 26 | { 27 | icon.Save(stream); 28 | data = stream.GetBuffer(); 29 | } 30 | 31 | using (var stream = new MemoryStream(data, true)) 32 | { 33 | newIcon = Icon.Load(stream); 34 | } 35 | 36 | Assert.IsNotNull(newIcon); 37 | Assert.IsTrue(newIcon.Images.Count >= 2); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /BluwolfIcons.Tests/PngIconImageTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using System; 3 | using System.IO; 4 | using System.Windows.Media; 5 | using System.Windows.Media.Imaging; 6 | 7 | namespace BluwolfIcons.Tests 8 | { 9 | [TestClass] 10 | public class PngIconImageTests 11 | { 12 | [TestMethod] 13 | [ExpectedException(typeof(ArgumentException))] 14 | public void Constructor_ImageTooBig_ThrowsException() 15 | { 16 | var bitmap = Helper.GenerateImage(257, 257, PixelFormats.Bgra32); 17 | var target = new PngIconImage(bitmap); 18 | } 19 | 20 | [TestMethod] 21 | [ExpectedException(typeof(ArgumentException))] 22 | public void SetOriginalImage_ImageTooBig_ThrowsException() 23 | { 24 | var valid = Helper.GenerateImage(16, 16, PixelFormats.Bgra32); 25 | var invalid = Helper.GenerateImage(257, 257, PixelFormats.Bgra32); 26 | 27 | var target = new PngIconImage(valid); 28 | target.OriginalImage = invalid; 29 | } 30 | 31 | [TestMethod] 32 | public void GetData_PreservesOriginalProperties() 33 | { 34 | var original = Helper.GenerateImage(16, 16, PixelFormats.Bgra32); 35 | 36 | var target = new PngIconImage(original); 37 | var data = target.GetData(); 38 | 39 | using (var stream = new MemoryStream(data, false)) 40 | { 41 | var decoder = new PngBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); 42 | var result = decoder.Frames[0]; 43 | 44 | Assert.AreEqual(result.PixelWidth, original.PixelWidth); 45 | Assert.AreEqual(result.PixelHeight, original.PixelHeight); 46 | Assert.AreEqual(result.Format, original.Format); 47 | } 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /BluwolfIcons.Tests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("BluwolfIcons.Tests")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("BluwolfIcons.Tests")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("c167d7fb-6f59-4883-aa86-cc498f2c7be4")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /BluwolfIcons.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.25123.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BluwolfIcons", "BluwolfIcons\BluwolfIcons.csproj", "{EB396D32-149A-4AA3-9720-51F1409CC68A}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BluwolfIcons.Demo", "BluwolfIcons.Demo\BluwolfIcons.Demo.csproj", "{B5B411C5-B470-4FA0-AF3D-B6B3D014F28B}" 9 | EndProject 10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A957D5AF-75E0-4FE7-B559-B5F129309BDF}" 11 | ProjectSection(SolutionItems) = preProject 12 | LICENSE = LICENSE 13 | README.md = README.md 14 | EndProjectSection 15 | EndProject 16 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BluwolfIcons.Tests", "BluwolfIcons.Tests\BluwolfIcons.Tests.csproj", "{C167D7FB-6F59-4883-AA86-CC498F2C7BE4}" 17 | EndProject 18 | Global 19 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 20 | Debug|Any CPU = Debug|Any CPU 21 | Release|Any CPU = Release|Any CPU 22 | EndGlobalSection 23 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 24 | {EB396D32-149A-4AA3-9720-51F1409CC68A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 25 | {EB396D32-149A-4AA3-9720-51F1409CC68A}.Debug|Any CPU.Build.0 = Debug|Any CPU 26 | {EB396D32-149A-4AA3-9720-51F1409CC68A}.Release|Any CPU.ActiveCfg = Release|Any CPU 27 | {EB396D32-149A-4AA3-9720-51F1409CC68A}.Release|Any CPU.Build.0 = Release|Any CPU 28 | {B5B411C5-B470-4FA0-AF3D-B6B3D014F28B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 29 | {B5B411C5-B470-4FA0-AF3D-B6B3D014F28B}.Debug|Any CPU.Build.0 = Debug|Any CPU 30 | {B5B411C5-B470-4FA0-AF3D-B6B3D014F28B}.Release|Any CPU.ActiveCfg = Release|Any CPU 31 | {B5B411C5-B470-4FA0-AF3D-B6B3D014F28B}.Release|Any CPU.Build.0 = Release|Any CPU 32 | {C167D7FB-6F59-4883-AA86-CC498F2C7BE4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 33 | {C167D7FB-6F59-4883-AA86-CC498F2C7BE4}.Debug|Any CPU.Build.0 = Debug|Any CPU 34 | {C167D7FB-6F59-4883-AA86-CC498F2C7BE4}.Release|Any CPU.ActiveCfg = Release|Any CPU 35 | {C167D7FB-6F59-4883-AA86-CC498F2C7BE4}.Release|Any CPU.Build.0 = Release|Any CPU 36 | EndGlobalSection 37 | GlobalSection(SolutionProperties) = preSolution 38 | HideSolutionNode = FALSE 39 | EndGlobalSection 40 | EndGlobal 41 | -------------------------------------------------------------------------------- /BluwolfIcons/BluwolfIcons.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Release 6 | AnyCPU 7 | {EB396D32-149A-4AA3-9720-51F1409CC68A} 8 | Library 9 | Properties 10 | BluwolfIcons 11 | BluwolfIcons 12 | v4.6.1 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | bluwolficons.ruleset 24 | 25 | 26 | 27 | 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | bluwolficons.ruleset 35 | bin\Release\BluwolfIcons.XML 36 | 37 | 38 | icon.ico 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 81 | -------------------------------------------------------------------------------- /BluwolfIcons/BluwolfIcons.nuspec: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $id$ 5 | $version$ 6 | $title$ 7 | $author$ 8 | $author$ 9 | https://github.com/Davipb/BluwolfIcons/raw/master/LICENSE 10 | https://github.com/Davipb/BluwolfIcons 11 | https://github.com/Davipb/BluwolfIcons/raw/master/BluwolfIcons/icon.png 12 | false 13 | $description$ 14 | No longer loads Duplicate images when loading an Icon, Fixed null reference when creating a BMP Image with Transparency Map 15 | ico icon icons bluwolf wic 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /BluwolfIcons/BluwolfIcons.ruleset: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /BluwolfIcons/BmpIconImage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Linq; 4 | using System.Windows.Media.Imaging; 5 | 6 | namespace BluwolfIcons 7 | { 8 | /// 9 | /// A BMP (Bitmap) image inside an icon. 10 | /// 11 | public sealed class BmpIconImage : IIconImage 12 | { 13 | const int BmpFileHeaderSize = 14; 14 | 15 | private BitmapSource originalImage; 16 | 17 | /// 18 | /// The original image. 19 | /// 20 | /// Thrown when an image too big is assigned. 21 | public BitmapSource OriginalImage 22 | { 23 | get { return originalImage; } 24 | set 25 | { 26 | if (value == null) 27 | throw new ArgumentNullException(nameof(value)); 28 | 29 | if (value.PixelWidth > 256) 30 | throw new ArgumentException("Image can't be wider than 256 pixels", nameof(value)); 31 | 32 | if (GenerateTransparencyMap) 33 | { 34 | if (value.PixelHeight > 256) 35 | throw new ArgumentException("Image without transparency map can't be taller than 256 pixels.", nameof(value)); 36 | } 37 | else 38 | { 39 | if (value.PixelHeight > 512) 40 | throw new ArgumentException("Image with transparency map can't be taller than 512 pixels.", nameof(value)); 41 | } 42 | 43 | originalImage = value; 44 | } 45 | } 46 | 47 | /// 48 | /// This image's width. 49 | /// 50 | public int Width => OriginalImage.PixelWidth; 51 | 52 | /// 53 | /// This image's height. 54 | /// 55 | public int Height 56 | { 57 | get 58 | { 59 | if (GenerateTransparencyMap) 60 | return OriginalImage.PixelHeight; 61 | 62 | // If the image already contains the transparency map, we should report its size as half the real size 63 | // (The transparency map takes 50% of the image) 64 | return OriginalImage.PixelHeight / 2; 65 | } 66 | } 67 | 68 | /// 69 | /// This image's bits per pixel. 70 | /// 71 | public int BitsPerPixel => OriginalImage.Format.BitsPerPixel; 72 | 73 | private bool generateTransparencyMap = true; 74 | /// 75 | /// Whether a transparency map should be generated automatically for this image. 76 | /// Set to false if the image already contains a transparency map. See remarks for more info. 77 | /// 78 | /// 79 | /// BMP images inside icons are stored with an extra transparency map above the actual image data. 80 | /// That transparency map is a 1-bit per pixel AND mask that defines if a pixel is visible or not, 81 | /// allowing for 1-bit transparency. 82 | /// 83 | /// Thrown when is taller than 256 pixels and value is being set to false. 84 | public bool GenerateTransparencyMap 85 | { 86 | get { return generateTransparencyMap; } 87 | set 88 | { 89 | if (value == false && OriginalImage?.PixelHeight > 256) 90 | throw new ArgumentException("Can't generate transparency map for an image taller than 256 pixels.", nameof(value)); 91 | 92 | generateTransparencyMap = value; 93 | } 94 | } 95 | 96 | /// 97 | /// Creates a new BMP icon image, with as its original image, without a transparency map. 98 | /// 99 | /// The original image to use in this icon image. 100 | public BmpIconImage(BitmapSource image) : this(image, true) { } 101 | 102 | /// 103 | /// Creates a new BMP icon image, with as its original image. 104 | /// 105 | /// The original image to use in this icon image. 106 | /// Whether to generate the transparency map or not. 107 | /// Thrown when is null. 108 | public BmpIconImage(BitmapSource image, bool generateMap) 109 | { 110 | if (image == null) 111 | throw new ArgumentNullException(nameof(image)); 112 | 113 | GenerateTransparencyMap = generateMap; 114 | OriginalImage = image; 115 | } 116 | 117 | /// 118 | /// Generates the BMP icon image data for this image. 119 | /// 120 | /// The BMP icon image data for this image. 121 | public byte[] GetData() 122 | { 123 | BitmapSource result = null; 124 | 125 | if (GenerateTransparencyMap) 126 | { 127 | // The transparency map has to be above the actual image, so we just copy the original image's data at the bottom 128 | // All bytes before that will be set to 0, which is what we want (0 == Visible) 129 | byte[] buffer = new byte[OriginalImage.PixelWidth * OriginalImage.PixelHeight * 4 * 2]; 130 | OriginalImage.CopyPixels(buffer, OriginalImage.PixelWidth * 4, buffer.Length / 2); 131 | 132 | result = BitmapSource.Create( 133 | OriginalImage.PixelWidth, OriginalImage.PixelHeight * 2, 134 | OriginalImage.DpiX, OriginalImage.DpiY, 135 | OriginalImage.Format, 136 | OriginalImage.Palette, 137 | buffer, 138 | OriginalImage.PixelWidth * 4); 139 | } 140 | else 141 | { 142 | result = OriginalImage; 143 | } 144 | 145 | using (var stream = new MemoryStream()) 146 | { 147 | var encoder = new BmpBitmapEncoder(); 148 | encoder.Frames.Add(BitmapFrame.Create(result)); 149 | encoder.Save(stream); 150 | 151 | // Remove the BMPFILEHEADER, turning it into a Memory BMP. 152 | return stream.GetBuffer().Skip(BmpFileHeaderSize).ToArray(); 153 | } 154 | } 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /BluwolfIcons/IIconImage.cs: -------------------------------------------------------------------------------- 1 | namespace BluwolfIcons 2 | { 3 | /// 4 | /// An image contained inside an icon. 5 | /// 6 | public interface IIconImage 7 | { 8 | /// 9 | /// This image's width. 10 | /// 11 | int Width { get; } 12 | 13 | /// 14 | /// This image's height. 15 | /// 16 | int Height { get; } 17 | 18 | /// 19 | /// This image's bits per pixel. 20 | /// 21 | int BitsPerPixel { get; } 22 | 23 | /// 24 | /// Generates the icon image data for this image. 25 | /// 26 | /// The data representing this image in the .ico 27 | byte[] GetData(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /BluwolfIcons/Icon.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Windows.Media.Imaging; 6 | 7 | namespace BluwolfIcons 8 | { 9 | /// 10 | /// Represents an icon image file. 11 | /// 12 | public sealed class Icon 13 | { 14 | /// 15 | /// All the images contained in this icon. 16 | /// 17 | public IList Images { get; } = new List(); 18 | 19 | /// 20 | /// Saves this icon to a specified file. 21 | /// 22 | /// The file to save this icon to. 23 | public void Save(string fileName) 24 | { 25 | using (var stream = File.Open(fileName, FileMode.Create)) 26 | { 27 | Save(stream); 28 | } 29 | } 30 | 31 | /// 32 | /// Saves this icon to a specified stream. 33 | /// 34 | /// The stream to save this icon to. 35 | public void Save(Stream stream) 36 | { 37 | if (stream == null) 38 | throw new ArgumentNullException(nameof(stream)); 39 | 40 | if (!stream.CanWrite) 41 | throw new ArgumentException("Stream must support writing.", nameof(stream)); 42 | 43 | using (var writer = new BinaryWriter(stream)) 44 | { 45 | // Reserved, always 0. 46 | writer.Write((ushort)0); 47 | // 1 for ICO, 2 for CUR 48 | writer.Write((ushort)1); 49 | writer.Write((ushort)Images.Count); 50 | 51 | var pendingImages = new Queue(); 52 | var offset = 6 + 16 * Images.Count; // Header: 6; Each Image: 16 53 | 54 | foreach (var image in Images) 55 | { 56 | writer.Write((byte)image.Width); 57 | writer.Write((byte)image.Height); 58 | 59 | // Number of colors in the palette. Since we always save the image ourselves (with no palette), hardcode this to 0 (No palette). 60 | writer.Write((byte)0); 61 | // Reserved, always 0. 62 | writer.Write((byte)0); 63 | // Color planes. Since we save the images ourselves, this is 1. 64 | writer.Write((ushort)1); 65 | writer.Write((ushort)image.BitsPerPixel); 66 | 67 | var data = image.GetData(); 68 | 69 | writer.Write((uint)data.Length); 70 | writer.Write((uint)offset); 71 | 72 | offset += data.Length; 73 | pendingImages.Enqueue(data); 74 | } 75 | 76 | while (pendingImages.Count > 0) 77 | writer.Write(pendingImages.Dequeue()); 78 | 79 | } 80 | } 81 | 82 | /// 83 | /// Loads an icon from a specified file. 84 | /// 85 | /// The file to load the icon from. 86 | /// The loaded icon. 87 | public static Icon Load(string fileName) 88 | { 89 | using (var stream = File.OpenRead(fileName)) 90 | return Load(stream); 91 | } 92 | 93 | /// 94 | /// Loads an icon from a stream. 95 | /// 96 | /// The stream to load the icon from. 97 | /// The loaded icon. 98 | public static Icon Load(Stream stream) 99 | => Load(new IconBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad)); 100 | 101 | /// 102 | /// Loads an icon from a bitmap decoder. 103 | /// 104 | /// The decoder to use when loading the icon. Every unique Frame decoded will be recognized as one individual image. 105 | /// The loaded icon 106 | public static Icon Load(BitmapDecoder decoder) 107 | { 108 | if (decoder == null) 109 | throw new ArgumentNullException(nameof(decoder)); 110 | 111 | var result = new Icon(); 112 | 113 | var added = new List(); 114 | 115 | foreach (var frame in decoder.Frames) 116 | { 117 | var encoder = new BmpBitmapEncoder(); 118 | encoder.Frames.Add(frame); 119 | byte[] data = null; 120 | using (var stream = new MemoryStream()) 121 | { 122 | encoder.Save(stream); 123 | data = stream.GetBuffer(); 124 | } 125 | 126 | bool duplicate = false; 127 | foreach (var alreadyAdded in added) 128 | { 129 | if (data.SequenceEqual(alreadyAdded)) 130 | { 131 | duplicate = true; 132 | break; 133 | } 134 | } 135 | 136 | if (duplicate) 137 | continue; 138 | 139 | added.Add(data); 140 | result.Images.Add(new PngIconImage(frame)); 141 | result.Images.Add(new BmpIconImage(frame)); 142 | } 143 | 144 | return result; 145 | } 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /BluwolfIcons/PngIconImage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Windows.Media.Imaging; 4 | 5 | namespace BluwolfIcons 6 | { 7 | /// 8 | /// A PNG (Portable Network Graphics) image inside an icon. 9 | /// 10 | public sealed class PngIconImage : IIconImage 11 | { 12 | private BitmapSource originalImage; 13 | 14 | /// 15 | /// The original image. 16 | /// 17 | /// Thrown when an image too big is assigned. 18 | public BitmapSource OriginalImage 19 | { 20 | get { return originalImage; } 21 | set 22 | { 23 | if (value == null) 24 | throw new ArgumentNullException(nameof(value)); 25 | 26 | if (value.PixelWidth > 256 || value.PixelHeight > 256) 27 | throw new ArgumentException("Image can't be bigger than 256 x 256.", nameof(value)); 28 | 29 | originalImage = value; 30 | } 31 | } 32 | 33 | /// 34 | /// This image's width. 35 | /// 36 | public int Width => OriginalImage.PixelWidth; 37 | 38 | /// 39 | /// This image's height. 40 | /// 41 | public int Height => OriginalImage.PixelHeight; 42 | 43 | /// 44 | /// This image's bits per pixel. 45 | /// 46 | public int BitsPerPixel => OriginalImage.Format.BitsPerPixel; 47 | 48 | /// 49 | /// Creates a new PNG icon image, with as its original image. 50 | /// 51 | /// The original image to use in this icon image. 52 | /// Thrown when is null. 53 | public PngIconImage(BitmapSource image) 54 | { 55 | if (image == null) 56 | throw new ArgumentNullException(nameof(image)); 57 | 58 | OriginalImage = image; 59 | } 60 | 61 | /// 62 | /// Gets the PNG icon image data for this image. 63 | /// 64 | /// The PNG icon image data for this image. 65 | public byte[] GetData() 66 | { 67 | // PNG images are represented just like in a normal file, so all we do is save to a MemoryStream and return the generated bytes. 68 | using (var stream = new MemoryStream()) 69 | { 70 | var encoder = new PngBitmapEncoder(); 71 | encoder.Frames.Add(BitmapFrame.Create(OriginalImage)); 72 | encoder.Save(stream); 73 | 74 | return stream.GetBuffer(); 75 | } 76 | } 77 | 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /BluwolfIcons/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("BluwolfIcons")] 9 | [assembly: AssemblyDescription("A .NET library for manipulating icon files.")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("Davipb")] 12 | [assembly: AssemblyProduct("Bluwolf Icons")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid("eb396d32-149a-4aa3-9720-51f1409cc68a")] 23 | 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the values or you can default the Build and Revision Numbers 32 | // by using the '*' as shown below: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("1.0.1.0")] 35 | [assembly: AssemblyFileVersion("1.0.1.0")] 36 | 37 | [assembly: CLSCompliant(true)] -------------------------------------------------------------------------------- /BluwolfIcons/icon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Davipb/BluwolfIcons/74b193dd459404d25d668b381b49a2d29e289bfc/BluwolfIcons/icon.ico -------------------------------------------------------------------------------- /BluwolfIcons/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Davipb/BluwolfIcons/74b193dd459404d25d668b381b49a2d29e289bfc/BluwolfIcons/icon.png -------------------------------------------------------------------------------- /BluwolfIcons/nuget.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Davipb/BluwolfIcons/74b193dd459404d25d668b381b49a2d29e289bfc/BluwolfIcons/nuget.exe -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ![Bluwolf Logo](http://i.imgur.com/fzOKZmJ.png) 2 | 3 | Bluwolf Icons is a C# .NET library for manipulating icon images (.ico) made using the WIC (Windows Imaging Component), 4 | the same technology behind WPF. 5 | 6 | **We're on NuGet!** 7 | 8 | `Install-Package BluwolfIcons` 9 | 10 | **Why not just use .NET's built-in Icon support?** 11 | 12 | All the built-in .NET libraries for icon manipulation only support a single icon: 13 | It automatically loads only the best quality image inside the .ico and you can only save one single bitmap as an icon. 14 | Not only that, but they are also built on top of GDI+, which is known to be messy and incomplete. 15 | Bluwolf Icons lets you load and save icons with as many images as you want and is built on top of the newer WIC, the same used in WPF. 16 | 17 | ## Quick Start 18 | 19 | Creating and saving an icon from code: 20 | ```csharp 21 | var icon = new Icon(); 22 | 23 | // This is just an example, any BitmapSource will be accepted 24 | var bitmap = new BitmapImage(); 25 | bitmap.BeginInit(); 26 | bitmap.UriSource = new Uri("MyAwesomeImage.png"); 27 | bitmap.CacheOption = BitmapCacheOption.OnLoad; 28 | bitmap.EndInit(); 29 | 30 | // You can manually choose how each image will be saved as: PNG or BMP 31 | icon.Images.Add(new PngIconImage(bitmap)); 32 | icon.Images.Add(new BmpIconImage(bitmap)); 33 | 34 | // Save to a file 35 | icon.Save("icon.ico"); 36 | 37 | // Save to a stream 38 | using (var stream = new MemoryStream()) 39 | { 40 | icon.Save(stream); 41 | } 42 | ``` 43 | 44 | Loading an icon: 45 | ```csharp 46 | // Load from a file 47 | var fileIcon = Icon.Load("icon.ico"); 48 | 49 | // Load from a stream 50 | Icon streamIcon = null; 51 | using (var stream = new MemoryStream(GetIconData())) 52 | { 53 | streamIcon = Icon.Load(stream); 54 | } 55 | 56 | // Load from a BitmapDecoder 57 | var decoder = new PngBitmapDecoder( 58 | new Uri("MyAwesomeImage.png"), 59 | BitmapCreateOptions.PreservePixelFormat, 60 | BitmapCacheOption.OnLoad); 61 | var decoderIcon = Icon.Load(decoder); 62 | 63 | // Read the icon in code 64 | foreach (var image in fileIcon.Images) 65 | { 66 | Console.WriteLine($"{image.Width}x{image.Height}x{image.BitsPerPixel}"); 67 | } 68 | ``` 69 | 70 | ## FAQ 71 | 72 | **What's the difference between a PNG and a BMP icon image?** 73 | 74 | A .ico icon is just a container that holds various images. 75 | Originally, it could hold only BMP images, but Windows Vista added support for PNGs. 76 | As such, BMPs are the most compatible format, but have less quality and occupy more space. 77 | PNGs are more compressible and have more quality, but not all programs may support it. 78 | 79 | 80 | --------------------------------------------------------------------------------