Skip to content

Instantly share code, notes, and snippets.

@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 / 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.
@LGM-AdrianHum
LGM-AdrianHum / IsScreensaverRunning.cs
Created June 23, 2022 00:17
Determine if screensaver is running or machine is locked.
using System;
using System.Runtime.InteropServices;
namespace BrutalDev.Helpers
{
public static class NativeMethods
{
// Used to check if the screen saver is running
[DllImport("user32.dll", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
@LGM-AdrianHum
LGM-AdrianHum / GetBaseUrl.cs
Last active May 28, 2022 16:58
Get the base URL of a web api or MVC page.
public string GetBaseUrl()
{
var appUrl = HttpRuntime.AppDomainAppVirtualPath;
return $"{Request?.Url.Scheme}://{Request?.Url.Authority}/{appUrl.TrimStart('/')}";
}