aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.RemoteDesktop/Frame.cs
blob: 105a991342cd0f6230eca3c6da195460b177d8b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using Tango.RemoteDesktop.Frames;

namespace Tango.RemoteDesktop
{
    /// <summary>
    /// Represents an image frame base class.
    /// </summary>
    /// <seealso cref="Tango.RemoteDesktop.IFrame" />
    public abstract class Frame : IFrame
    {
        /// <summary>
        /// Gets the frame width.
        /// </summary>
        public abstract int Width { get; }

        /// <summary>
        /// Gets the frame height.
        /// </summary>
        public abstract int Height { get; }

        /// <summary>
        /// Returns a GDI bitmap representing the frame.
        /// </summary>
        /// <returns></returns>
        public abstract Bitmap ToBitmap();

        /// <summary>
        /// Applies this frame onto an existing bitmap.
        /// </summary>
        /// <param name="bitmap">The bitmap.</param>
        public virtual void Apply(Bitmap bitmap)
        {
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.DrawImage(ToBitmap(), new Rectangle(0, 0, bitmap.Width, bitmap.Height));
            }
        }

        /// <summary>
        /// Applies this frame onto an existing raster frame.
        /// </summary>
        /// <param name="frame">The frame.</param>
        public virtual void Apply(RasterFrame frame)
        {
            Apply(frame.ToBitmap());
        }

        /// <summary>
        /// Returns a WPF BitmapSource representing the frame.
        /// </summary>
        /// <returns></returns>
        public BitmapSource ToBitmapSource()
        {
            MemoryStream ms = new MemoryStream();
            ToBitmap().Save(ms, ImageFormat.Bmp);
            ms.Position = 0;
            var bitmapImage = new BitmapImage();
            bitmapImage.BeginInit();
            bitmapImage.StreamSource = ms;
            bitmapImage.EndInit();
            bitmapImage.Freeze();
            return bitmapImage;
        }

        /// <summary>
        /// Returns an instance of <see cref="IFrameEncoder" /> ready to encode this frame.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T ToEncoder<T>() where T : IFrameEncoder
        {
            return (T)Activator.CreateInstance(typeof(T), new object[] { this });
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        public abstract void Dispose();
    }
}