Last active
August 18, 2023 06:28
-
-
Save LGM-AdrianHum/09d09678b33d7f8bda31bfd787b0b336 to your computer and use it in GitHub Desktop.
Generate an image source that can be used to give an image to the application dynamically.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Windows; | |
using System.Windows.Media; | |
using System.Windows.Media.Imaging; | |
using SamNolan.Emoji.WPF; //Remeber to get this from nuget | |
public static class CustomIconHelper | |
{ | |
public static ImageSource CreateCustomIcon(Color ledColor, double reflectionOpacity = 0, string imageResourceName = "") | |
{ | |
int iconSize = 64; | |
double circleSize = iconSize - 10; | |
Color reflectionColor = Colors.White; // Change this to the desired reflection color | |
// Create a DrawingVisual | |
DrawingVisual drawingVisual = new DrawingVisual(); | |
using (DrawingContext drawingContext = drawingVisual.RenderOpen()) | |
{ | |
// Draw the bezel | |
SolidColorBrush bezelBrush = new SolidColorBrush(Colors.White); | |
Pen bezelPen = new Pen(new SolidColorBrush(Colors.Gray), 2); | |
drawingContext.DrawEllipse(bezelBrush, bezelPen, new Point(iconSize / 2, iconSize / 2), circleSize / 2 + 5, circleSize / 2 + 5); | |
// Draw the LED background | |
SolidColorBrush ledBrush = new SolidColorBrush(ledColor); | |
drawingContext.DrawEllipse(ledBrush, null, new Point(iconSize / 2, iconSize / 2), circleSize / 2, circleSize / 2); | |
if (!string.IsNullOrEmpty(imageResourceName)) | |
{ | |
// Draw the reflection ellipse | |
SolidColorBrush reflectionBrush = new SolidColorBrush(reflectionColor); | |
reflectionBrush.Opacity = reflectionOpacity; // Use the passed opacity directly | |
double reflectionEllipseSize = circleSize / 3; // Adjust the size of the reflection ellipse | |
drawingContext.DrawEllipse(reflectionBrush, null, new Point(iconSize / 6, iconSize / 6), reflectionEllipseSize / 2, reflectionEllipseSize / 2); | |
// Load the image overlay resource | |
ImageSource imageOverlay = LoadImageFromResource(imageResourceName); | |
if (imageOverlay != null) | |
{ | |
// Draw the image overlay | |
drawingContext.DrawImage(imageOverlay, new Rect(0, 0, iconSize, iconSize)); | |
} | |
else | |
{ | |
// Draw the application's default icon as fallback | |
drawingContext.DrawImage(Application.Current.MainWindow.Icon, new Rect(0, 0, iconSize, iconSize)); | |
} | |
} | |
} | |
// Render the DrawingVisual to a RenderTargetBitmap | |
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap( | |
iconSize, iconSize, // Width, Height | |
96, 96, // DPI (dots per inch) | |
PixelFormats.Pbgra32 | |
); | |
renderTargetBitmap.Render(drawingVisual); | |
// Convert RenderTargetBitmap to BitmapImage | |
BitmapImage bitmapImage = new BitmapImage(); | |
using (var stream = new MemoryStream()) | |
{ | |
BitmapEncoder encoder = new PngBitmapEncoder(); | |
encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap)); | |
encoder.Save(stream); | |
stream.Seek(0, SeekOrigin.Begin); | |
bitmapImage.BeginInit(); | |
bitmapImage.CacheOption = BitmapCacheOption.OnLoad; | |
bitmapImage.StreamSource = stream; | |
bitmapImage.EndInit(); | |
} | |
return bitmapImage; | |
} | |
public static ImageSource CreateEmojiIcon(string emojiUnicode, double reflectionOpacity = 0, string imageResourceName = "") | |
{ | |
int iconSize = 64; | |
double circleSize = iconSize - 10; | |
// Create a DrawingVisual | |
DrawingVisual drawingVisual = new DrawingVisual(); | |
using (DrawingContext drawingContext = drawingVisual.RenderOpen()) | |
{ | |
// Draw the bezel | |
SolidColorBrush bezelBrush = new SolidColorBrush(Colors.White); | |
Pen bezelPen = new Pen(new SolidColorBrush(Colors.Gray), 2); | |
drawingContext.DrawEllipse(bezelBrush, bezelPen, new Point(iconSize / 2, iconSize / 2), circleSize / 2 + 5, circleSize / 2 + 5); | |
// Draw the LED background with the emoji icon | |
var emoji = Emoji.GetEmoji(emojiUnicode); | |
if (emoji != null) | |
{ | |
drawingContext.DrawText( | |
new FormattedText(emoji.GetUnicodeString(), System.Globalization.CultureInfo.InvariantCulture, | |
FlowDirection.LeftToRight, new Typeface("Segoe UI Emoji"), 32, Brushes.Black), new Point(10, 10)); | |
} | |
// Draw the reflection ellipse (if applicable) | |
// Load the image overlay resource (if applicable) | |
// Draw the image overlay (if applicable) | |
} | |
// Render the DrawingVisual to a RenderTargetBitmap | |
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap( | |
iconSize, iconSize, // Width, Height | |
96, 96, // DPI (dots per inch) | |
PixelFormats.Pbgra32 | |
); | |
renderTargetBitmap.Render(drawingVisual); | |
// Convert RenderTargetBitmap to BitmapImage | |
BitmapImage bitmapImage = new BitmapImage(); | |
using (var stream = new MemoryStream()) | |
{ | |
BitmapEncoder encoder = new PngBitmapEncoder(); | |
encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap)); | |
encoder.Save(stream); | |
stream.Seek(0, SeekOrigin.Begin); | |
bitmapImage.BeginInit(); | |
bitmapImage.CacheOption = BitmapCacheOption.OnLoad; | |
bitmapImage.StreamSource = stream; | |
bitmapImage.EndInit(); | |
} | |
return bitmapImage; | |
} | |
private static ImageSource LoadImageFromResource(string resourceName) | |
{ | |
try | |
{ | |
var uri = new Uri($"pack://application:,,,/YourProjectName;component/{resourceName}"); | |
var source = new BitmapImage(uri); | |
source.Freeze(); | |
return source; | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"Error loading image '{resourceName}': {ex.Message}"); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment