using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Tango.RemoteDesktop.Utils
{
///
/// Represents a bitmap object with fast Get/Set Pixel methods.
///
///
public class DirectBitmap : IDisposable
{
public Bitmap Bitmap { get; private set; }
public Int32[] Bits { get; private set; }
public bool Disposed { get; private set; }
public int Height { get; private set; }
public int Width { get; private set; }
protected GCHandle BitsHandle { get; private set; }
///
/// Initializes a new instance of the class.
///
/// The width.
/// The height.
public DirectBitmap(int width, int height)
{
Width = width;
Height = height;
Bits = new Int32[width * height];
BitsHandle = GCHandle.Alloc(Bits, GCHandleType.Pinned);
Bitmap = new Bitmap(width, height, width * 4, PixelFormat.Format32bppPArgb, BitsHandle.AddrOfPinnedObject());
}
///
/// Sets the pixel color.
///
/// The x position.
/// The y position.
/// The color.
public void SetPixel(int x, int y, Color color)
{
int index = x + (y * Width);
int col = color.ToArgb();
Bits[index] = col;
}
///
/// Gets the pixel color.
///
/// The x position.
/// The y position.
///
public Color GetPixel(int x, int y)
{
int index = x + (y * Width);
int col = Bits[index];
Color result = Color.FromArgb(col);
return result;
}
///
/// Releases unmanaged and - optionally - managed resources.
///
public void Dispose()
{
if (Disposed) return;
Disposed = true;
BitsHandle.Free();
}
}
}