aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.RemoteDesktop/Frames/RasterFrame.cs
blob: a9cbea6f259e1021cba71cebca39a7906c26bf18 (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<
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.Clipping;

namespace Tango.RemoteDesktop.Frames
{
    /// <summary>
    /// Represents a raster frame encapsulating a standard GDI bitmap.
    /// </summary>
    /// <seealso cref="Tango.RemoteDesktop.Frame" />
    public class RasterFrame : Frame
    {
        private Bitmap _bitmap;

        /// <summary>
        /// Gets the left position of this frame.
        /// Will always be 0 unless <see cref="OptimizeBounds"/> was called.
        /// </summary>
        public int Left { get; private set; }

        /// <summary>
        /// Gets the top position of this frame.
        /// Will always be 0 unless <see cref="OptimizeBounds"/> was called.
        /// </summary>
        public int Top { get; private set; }

        /// <summary>
        /// Gets the frame width.
        /// </summary>
        public override int Width
        {
            get { return _bitmap.Width; }
        }

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

        /// <summary>
        /// Initializes a new instance of the <see cref="RasterFrame"/> class.
        /// </summary>
        /// <param name="bitmap">The bitmap.</param>
        public RasterFrame(Bitmap bitmap)
        {
            _bitmap = bitmap;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RasterFrame"/> class.
        /// </summary>
        /// <param name="bitmap">The bitmap.</param>
        /// <param name="left">The left position of the frame.</param>
        /// <param name="top">The top position of the frame.</param>
        public RasterFrame(Bitmap bitmap, int left, int top) : this(bitmap)
        {
            Left = left;
            Top = top;
        }

        /// <summary>
        /// Returns a GDI bitmap representing the frame.
        /// </summary>
        /// <returns></returns>
        public override Bitmap ToBitmap()
        {
            return _bitmap;
        }

        /// <summary>
        /// Scales the frame by the specified coefficient.
        /// </summary>
        /// <param name="coefficient">The coefficient.</param>
        /// <returns></returns>
        public RasterFrame Scale(float coefficient)
        {
            var scaled = new Bitmap(_bitmap, new Size((int)(Width * coefficient), (int)(Height * coefficient)));
            _bitmap.Dispose();
            _bitmap = scaled;
            return this;
        }

        /// <summary>
        /// Applies this frame onto an existing bitmap.
        /// </summary>
        /// <param name="bitmap">The bitmap.</param>
        public override void Apply(Bitmap bitmap)
        {
            if (Top == 0 && Left == 0)
            {
                base.Apply(bitmap);
            }
            else
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.DrawImage(_bitmap, new Rectangle(Left, Top, Width, Height));
                }
            }
        }

        /// <summary>
        /// Draws the specified image on to this frame.
        /// </summary>
        /// <param name="bitmap">The bitmap.</param>
        public virtual void DrawImage(Bitmap bitmap, Point position)
        {
            using (Graphics g = Graphics.FromImage(_bitmap))
            {
                g.DrawImage(bitmap, new Rectangle(position, bitmap.Size));
            }
        }

        /// <summary>
        /// Optimizes the bounds of this frame by removing unnecessary margins.
        /// </summary>
        /// <returns></returns>
        public RasterFrame OptimizeBounds()
        {
            var result = BitmapCliper.ClipBitmap(_bitmap);
            _bitmap.Dispose();
            Left = result.Bounds.Left;
            Top = result.Bounds.Top;
            _bitmap = result.Bitmap;
            return this;
        }

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