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
{
///
/// Represents a raster frame encapsulating a standard GDI bitmap.
///
///
public class RasterFrame : Frame
{
private Bitmap _bitmap;
///
/// Gets the left position of this frame.
/// Will always be 0 unless was called.
///
public int Left { get; private set; }
///
/// Gets the top position of this frame.
/// Will always be 0 unless was called.
///
public int Top { get; private set; }
///
/// Gets the frame width.
///
public override int Width
{
get { return _bitmap.Width; }
}
///
/// Gets the frame height.
///
public override int Height
{
get { return _bitmap.Height; }
}
///
/// Initializes a new instance of the class.
///
/// The bitmap.
public RasterFrame(Bitmap bitmap)
{
_bitmap = bitmap;
}
///
/// Initializes a new instance of the class.
///
/// The bitmap.
/// The left position of the frame.
/// The top position of the frame.
public RasterFrame(Bitmap bitmap, int left, int top) : this(bitmap)
{
Left = left;
Top = top;
}
///
/// Returns a GDI bitmap representing the frame.
///
///
public override Bitmap ToBitmap()
{
return _bitmap;
}
///
/// Scales the frame by the specified coefficient.
///
/// The coefficient.
///
public RasterFrame Scale(float coefficient)
{
var scaled = new Bitmap(_bitmap, new Size((int)(Width * coefficient), (int)(Height * coefficient)));
_bitmap.Dispose();
_bitmap = scaled;
return this;
}
///
/// Applies this frame onto an existing bitmap.
///
/// The bitmap.
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));
}
}
}
///
/// Draws the specified image on to this frame.
///
/// The bitmap.
public virtual void DrawImage(Bitmap bitmap, Point position)
{
using (Graphics g = Graphics.FromImage(_bitmap))
{
g.DrawImage(bitmap, new Rectangle(position, bitmap.Size));
}
}
///
/// Optimizes the bounds of this frame by removing unnecessary margins.
///
///
public RasterFrame OptimizeBounds()
{
var result = BitmapCliper.ClipBitmap(_bitmap);
_bitmap.Dispose();
Left = result.Bounds.Left;
Top = result.Bounds.Top;
_bitmap = result.Bitmap;
return this;
}
///
/// Releases unmanaged and - optionally - managed resources.
///
public override void Dispose()
{
_bitmap.Dispose();
}
}
}