Skip to content

Instantly share code, notes, and snippets.

@LGM-AdrianHum
LGM-AdrianHum / ListBoxStyle.xaml
Created October 11, 2017 23:56
Changing WPF Listbox SelectedItem text color and highlight/background Color
<Window x:Class="ListBoxStyle.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:ListBoxStyle"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="_ListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
@LGM-AdrianHum
LGM-AdrianHum / ExtractIpAddress.cs
Created August 28, 2023 00:44
IP Tools : A regex static function to extract IP addresses from strings (good for DNS services that return extra data with IP addresses.
private static string ExtractIpAddress(this string input) => string.Join("", Regex.Matches(input, @"[\d\.]+"));
@LGM-AdrianHum
LGM-AdrianHum / CustomIconHelper.cs
Last active August 18, 2023 06:28
Generate an image source that can be used to give an image to the application dynamically.
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 = "")
@LGM-AdrianHum
LGM-AdrianHum / SetImageSourceFromResource.cs
Last active August 6, 2023 22:26
WPF: Using the pack://application:,,, notation to load a resource and set it as an imagesource in code behind.
public void SetImageSourceFromResource()
{
try
{
// Replace "YourNamespace" with the namespace where the image resource is located
Uri imageUri = new Uri("pack://application:,,,/YourNamespace;component/MyImage.png");
// Create a BitmapImage and set it as the source for the Image control
BitmapImage bitmapImage = new BitmapImage(imageUri);
myImageControl.Source = bitmapImage;
@LGM-AdrianHum
LGM-AdrianHum / MeasureStringFont.cs
Created April 23, 2023 11:11
Measure String Size for Given Font
private float getTextSize(string text)
{
Font font = new Font("Courier New", 10.0F);
Image fakeImage = new Bitmap(1, 1);
Graphics graphics = Graphics.FromImage(fakeImage);
SizeF size = graphics.MeasureString(text, font);
return size.Width;
}
@LGM-AdrianHum
LGM-AdrianHum / JunctionPoint.cs
Last active April 16, 2023 18:27
Create, Delete and Examine Junction Points in C#
// File: RollThroughLibrary/CreateMaps/JunctionPoint.cs
// User: Adrian Hum/
//
// Created: 2017-11-19 2:46 PM
// Modified: 2017-11-19 6:10 PM
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.InteropServices;
@LGM-AdrianHum
LGM-AdrianHum / s3HashCalculator.cs
Last active January 12, 2023 01:49
Calculate the AWS S3 Checksum
public static async Task<string> HashOf(string filename, int chunkSizeInMb)
{
var returnMd5 = string.Empty;
var chunkSize = chunkSizeInMb * 1024 * 1024;
await Task.Run(() =>
{
using (var crypto = new System.Security.Cryptography.MD5CryptoServiceProvider())
{
var hashLength = crypto.HashSize / 8;
@LGM-AdrianHum
LGM-AdrianHum / queue_examine.cs
Created January 12, 2023 01:35
Examine C# Queue<t> at element N
using System.Linq;
Queue<T> queue = new Queue<T>();
T result;
result = queue.ElementAt(2);
result = queue.ElementAtOrDefault(2);
@LGM-AdrianHum
LGM-AdrianHum / TextBlockExtension.cs
Created September 22, 2022 13:47
An extension for textblocks in WPF to get rid of annoying spaces between runs.
// ----------------------------------------------------------------------------------
// <copyright file="TextBlockExtension.cs" >
// This file is part of the MyS3Uploader distribution
//
// Copyright (c) 2022
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// </copyright>
// <summary>
@LGM-AdrianHum
LGM-AdrianHum / StringCipher.cs
Created July 28, 2022 02:21
Symmetric Encrypt And Decrypt Strings
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace SearchIdentityExample.Models {
public static class StringCipher
{
// This constant is used to determine the keysize of the encryption algorithm in bits.