Skip to content

Instantly share code, notes, and snippets.

@emoacht
emoacht / BitmapMetadataEditor.cs
Created May 26, 2014 11:22
Edit Exif metadata.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Media.Imaging;
/// <summary>
/// Edit Exif metadata.
/// </summary>
@emoacht
emoacht / EnumerableMergeSort.cs
Created June 17, 2014 14:41
A draft of enumerable merge sort method
private const int countThreshold = 64;
public static IEnumerable<TSource> MergeSort<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
if (source == null)
throw new ArgumentNullException("source");
var countFull = source.Count();
if (countFull < countThreshold)
return source.OrderBy(keySelector);
@emoacht
emoacht / WindowsEightChromeColor.cs
Last active August 29, 2015 14:03
Get window's chome color (title bar color) in Windows 8.1.
using System;
using System.Runtime.InteropServices;
using System.Windows.Media;
public static class WindowsEightChromeColor
{
public static Color? GetChromeColor()
{
bool isEnabled;
var hr1 = DwmIsCompositionEnabled(out isEnabled);
@emoacht
emoacht / VisualTreeHelperAddition.cs
Last active August 29, 2015 14:03
Get all descendent DependencyObjects of a DependencyObject.
public static class VisualTreeHelperAddition
{
public static IEnumerable<T> GetDescendents<T>(this DependencyObject parent) where T : DependencyObject
{
if (parent == null)
yield break;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
@emoacht
emoacht / PerMonitorDpiProperty.cs
Created July 25, 2014 06:37
Attached property to make a Window Per-Monitor DPI aware.
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
public class PerMonitorDpiProperty : DependencyObject
{
public static PerMonitorDpiProperty GetAttachedProperty(DependencyObject obj)
{
@emoacht
emoacht / TestMethod.cs
Last active August 29, 2015 14:11
Difference of ?? (null-coalescing) operator in C# and If operator in VB
public void TestMethod(IEnumerable<string> names)
{
List<string> nameList = names as List<string> ?? names.ToList(); //OK
int nameCount = (names as List<string> ?? names.ToList()).Count; //OK
//Case 1
nameList.ForEach(x => Console.WriteLine(x)); //OK
//Case 2
(names as List<string> ?? names.ToList()).ForEach(x => Console.WriteLine(x)); //OK
@emoacht
emoacht / CallerMemberNameAttribute.cs
Last active August 29, 2015 14:12
CallerMemberNameAttribute for .NET Framework earlier than 4.5
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
public class CallerMemberNameAttribute : Attribute
{
}
}
@emoacht
emoacht / BatteryStatus.cs
Last active August 29, 2015 14:13
Check battery status.
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
public class BatteryStatus : INotifyPropertyChanged
{
public BatteryChargeStatus BatteryChargeStatus => SystemInformation.PowerStatus.BatteryChargeStatus; // Flag
public int BatteryFullLifetime => SystemInformation.PowerStatus.BatteryFullLifetime;
@emoacht
emoacht / StreamCopy.cs
Created January 28, 2015 13:12
Copy a file with Stream.
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
public class StreamCopy
{
private const int bufferSize = 1024 * 1024; // 1MiB
@emoacht
emoacht / BytesExtension.cs
Last active August 29, 2015 14:15
Extension method for Byte array
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
public static class BytesExtension
{
public static byte[] SequenceReplace(this byte[] source, byte[] oldValue, byte[] newValue, int maxCount = -1)
{
var sourceIndices = SequenceIndicesOf(source, oldValue, maxCount).ToArray();