2021-06-27 21:35:07 +12:00
|
|
|
|
// <copyright file="ImagingHelper.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
|
|
|
|
|
2021-10-26 08:45:49 +13:00
|
|
|
|
namespace SystemTrayMenu.Helper
|
2021-06-26 23:24:56 +12:00
|
|
|
|
{
|
|
|
|
|
using System.Drawing;
|
2021-10-11 03:44:06 +13:00
|
|
|
|
using System.Drawing.Drawing2D;
|
2021-06-26 23:24:56 +12:00
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides helper methods for imaging.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class ImagingHelper
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts a PNG image to a icon (ico).
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input">The input stream.</param>
|
|
|
|
|
/// <param name="output">The output stream.</param>
|
|
|
|
|
/// <param name="size">The size (16x16 px by default).</param>
|
|
|
|
|
/// <param name="preserveAspectRatio">Preserve the aspect ratio.</param>
|
|
|
|
|
/// <returns>Wether or not the icon was succesfully generated.</returns>
|
|
|
|
|
public static bool ConvertToIcon(Stream input, Stream output, int size = 16, bool preserveAspectRatio = false)
|
|
|
|
|
{
|
2021-11-14 07:44:32 +13:00
|
|
|
|
Bitmap inputBitmap = (Bitmap)Image.FromStream(input);
|
2021-06-26 23:24:56 +12:00
|
|
|
|
if (inputBitmap != null)
|
|
|
|
|
{
|
|
|
|
|
int width, height;
|
|
|
|
|
if (preserveAspectRatio)
|
|
|
|
|
{
|
|
|
|
|
width = size;
|
|
|
|
|
height = inputBitmap.Height / inputBitmap.Width * size;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
width = height = size;
|
|
|
|
|
}
|
2021-06-27 21:35:07 +12:00
|
|
|
|
|
2021-11-17 12:13:46 +13:00
|
|
|
|
Bitmap newBitmap = new(inputBitmap, new Size(width, height));
|
2021-06-26 23:24:56 +12:00
|
|
|
|
if (newBitmap != null)
|
|
|
|
|
{
|
|
|
|
|
// save the resized png into a memory stream for future use
|
2021-11-17 12:13:46 +13:00
|
|
|
|
using MemoryStream memoryStream = new();
|
2021-10-11 04:33:54 +13:00
|
|
|
|
newBitmap.Save(memoryStream, ImageFormat.Png);
|
2021-06-26 23:24:56 +12:00
|
|
|
|
|
2021-11-17 12:13:46 +13:00
|
|
|
|
BinaryWriter iconWriter = new(output);
|
2021-10-11 04:33:54 +13:00
|
|
|
|
if (output != null && iconWriter != null)
|
|
|
|
|
{
|
|
|
|
|
// 0-1 reserved, 0
|
|
|
|
|
iconWriter.Write((byte)0);
|
|
|
|
|
iconWriter.Write((byte)0);
|
2021-06-26 23:24:56 +12:00
|
|
|
|
|
2021-10-11 04:33:54 +13:00
|
|
|
|
// 2-3 image type, 1 = icon, 2 = cursor
|
|
|
|
|
iconWriter.Write((short)1);
|
2021-06-26 23:24:56 +12:00
|
|
|
|
|
2021-10-11 04:33:54 +13:00
|
|
|
|
// 4-5 number of images
|
|
|
|
|
iconWriter.Write((short)1);
|
2021-06-26 23:24:56 +12:00
|
|
|
|
|
2021-10-11 04:33:54 +13:00
|
|
|
|
// image entry 1
|
|
|
|
|
// 0 image width
|
|
|
|
|
iconWriter.Write((byte)width);
|
2021-06-26 23:24:56 +12:00
|
|
|
|
|
2021-10-11 04:33:54 +13:00
|
|
|
|
// 1 image height
|
|
|
|
|
iconWriter.Write((byte)height);
|
2021-06-26 23:24:56 +12:00
|
|
|
|
|
2021-10-11 04:33:54 +13:00
|
|
|
|
// 2 number of colors
|
|
|
|
|
iconWriter.Write((byte)0);
|
2021-06-26 23:24:56 +12:00
|
|
|
|
|
2021-10-11 04:33:54 +13:00
|
|
|
|
// 3 reserved
|
|
|
|
|
iconWriter.Write((byte)0);
|
2021-06-26 23:24:56 +12:00
|
|
|
|
|
2021-10-11 04:33:54 +13:00
|
|
|
|
// 4-5 color planes
|
|
|
|
|
iconWriter.Write((short)0);
|
2021-06-26 23:24:56 +12:00
|
|
|
|
|
2021-10-11 04:33:54 +13:00
|
|
|
|
// 6-7 bits per pixel
|
|
|
|
|
iconWriter.Write((short)32);
|
2021-06-26 23:24:56 +12:00
|
|
|
|
|
2021-10-11 04:33:54 +13:00
|
|
|
|
// 8-11 size of image data
|
|
|
|
|
iconWriter.Write((int)memoryStream.Length);
|
2021-06-26 23:24:56 +12:00
|
|
|
|
|
2021-10-11 04:33:54 +13:00
|
|
|
|
// 12-15 offset of image data
|
|
|
|
|
iconWriter.Write(6 + 16);
|
2021-06-26 23:24:56 +12:00
|
|
|
|
|
2021-10-11 04:33:54 +13:00
|
|
|
|
// write image data
|
|
|
|
|
// png data must contain the whole png data file
|
|
|
|
|
iconWriter.Write(memoryStream.ToArray());
|
2021-06-26 23:24:56 +12:00
|
|
|
|
|
2021-10-11 04:33:54 +13:00
|
|
|
|
iconWriter.Flush();
|
2021-06-26 23:24:56 +12:00
|
|
|
|
|
2021-10-11 04:33:54 +13:00
|
|
|
|
return true;
|
2021-06-26 23:24:56 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-27 21:35:07 +12:00
|
|
|
|
|
2021-06-26 23:24:56 +12:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-06-27 21:35:07 +12:00
|
|
|
|
|
2021-06-26 23:24:56 +12:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts a PNG image to a icon (ico).
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="inputPath">The input path.</param>
|
|
|
|
|
/// <param name="outputPath">The output path.</param>
|
|
|
|
|
/// <param name="size">The size (16x16 px by default).</param>
|
|
|
|
|
/// <param name="preserveAspectRatio">Preserve the aspect ratio.</param>
|
|
|
|
|
/// <returns>Wether or not the icon was succesfully generated.</returns>
|
|
|
|
|
public static bool ConvertToIcon(string inputPath, string outputPath, int size = 16, bool preserveAspectRatio = false)
|
|
|
|
|
{
|
2021-11-17 12:13:46 +13:00
|
|
|
|
using FileStream inputStream = new(inputPath, FileMode.Open);
|
|
|
|
|
using FileStream outputStream = new(outputPath, FileMode.OpenOrCreate);
|
2021-10-11 04:33:54 +13:00
|
|
|
|
return ConvertToIcon(inputStream, outputStream, size, preserveAspectRatio);
|
2021-06-26 23:24:56 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|