Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2484 - 1
using System;
2
using System.Collections.Generic;
3
using System.Drawing;
4
using System.Runtime.InteropServices;
5
using System.Windows;
6
using System.Windows.Interop;
7
using System.Windows.Media.Imaging;
8
using WindowsPoint = System.Windows.Point;
9
using DrawingPoint = System.Drawing.Point;
10
 
11
namespace Touchless.Shared.Extensions
12
{
13
    public static class Extensions
14
    {
15
        public static IEnumerable<T> ForEach<T>(this IEnumerable<T> items, Action<T> action)
16
        {
17
            foreach (T item in items)
18
            {
19
                action(item);
20
            }
21
 
22
            return items;
23
        }
24
 
25
        public static void IfNotNull<T>(this T item, Action<T> action)
26
        {
27
            if (item != null)
28
            {
29
                action(item);
30
            }
31
        }
32
 
33
        public static WindowsPoint ToWindowsPoint(this DrawingPoint p)
34
        {
35
            return new WindowsPoint
36
                       {
37
                           X = p.X,
38
                           Y = p.Y
39
                       };
40
        }
41
 
42
        public static DrawingPoint ToDrawingPoint(this WindowsPoint p)
43
        {
44
            return new DrawingPoint
45
                       {
46
                           X = (int) p.X,
47
                           Y = (int) p.Y
48
                       };
49
        }
50
 
51
        [DllImport("gdi32")]
52
        private static extern int DeleteObject(IntPtr o);
53
 
54
        public static BitmapSource ToBitmapSource(this Bitmap source)
55
        {
56
            BitmapSource bs = null;
57
 
58
            IntPtr ip = source.GetHbitmap();
59
            try
60
            {
61
                bs = Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,
62
                                                           BitmapSizeOptions.FromEmptyOptions());
63
            }
64
            finally
65
            {
66
                DeleteObject(ip);
67
            }
68
 
69
            return bs;
70
        }
71
    }
72
}