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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
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();
}
}
}
|