Skip to content

Instantly share code, notes, and snippets.

@odalet
odalet / LambdaTricks.cs
Last active July 3, 2020 09:15
Lambda Tricks
using System;
using System.Linq;
namespace Rextester
{
// See https://csharp.2000things.com/tag/captured-variable/
internal static class Program
{
public static void Main(string[] args)
{
using System;
using System.Runtime.InteropServices;
// https://stackoverflow.com/questions/66002667/pinvoking-user32-dll-setwindowcompositionattribute-via-powershell
public static class NativeMethods
{
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
@odalet
odalet / RoundedRectangleExtension.cs
Last active January 23, 2022 15:33
Create a rounded rectangle path with ImageSharp
#if !IMAGESHARP_V2
using System;
using System.Collections.Generic;
using System.Numerics;
#endif
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing;
namespace RoundedRectangleTest
{
@odalet
odalet / GetBitmapFromHBitmap.cs
Created February 12, 2022 16:28
HBitmap -> WPF BitmapSource -> Avalonia Bitmap
// First attempt
public static Bitmap GetBitmapFromHBitmap_1(IntPtr nativeHBitmap)
{
var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(
nativeHBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
// There were missing parentheses here (they are important because this is integer arithmetic.
// Also worth noting, the code below only supports 32bpp images. In this case, stride
// is always equal to width, so a micro-optim would be to not compute the stride.
var stride = 4 * ((bitmapSource.PixelWidth * bitmapSource.Format.BitsPerPixel + 31) / 32);