-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathDirectBitmap.cs
More file actions
69 lines (61 loc) · 2.04 KB
/
Copy pathDirectBitmap.cs
File metadata and controls
69 lines (61 loc) · 2.04 KB
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
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
namespace ShowMATLABImage
{
/// <summary>
/// Draw directly to a bitmap's surface
/// by setting and getting pixel colors
/// to and from a byte array (RGBA)
/// <see cref="https://stackoverflow.com/questions/24701703/c-sharp-faster-alternatives-to-setpixel-and-getpixel-for-bitmaps-for-windows-f#34801225"/>
/// </summary>
public class DirectBitmap : IDisposable
{
public Bitmap Bitmap { get; private set; }
public byte[] 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; }
public DirectBitmap(int width, int height)
{
Width = width;
Height = height;
Bits = new byte[width * height * 4];
BitsHandle = GCHandle.Alloc(Bits, GCHandleType.Pinned);
Bitmap = new Bitmap(width, height, width * 4, PixelFormat.Format32bppRgb, BitsHandle.AddrOfPinnedObject());
}
public void SetBytes(byte[] data)
{
data.CopyTo(Bits, 0);
}
public void SetPixel(int x, int y, Color colour)
{
int index = (x + (y * Width)) * 4;
byte[] argb = BitConverter.GetBytes(colour.ToArgb());
for (int i = 0; i < 4; i++)
{
Bits[index + i] = argb[i];
}
}
public Color GetPixel(int x, int y)
{
int index = (x + (y * Width)) * 4;
Color result = Color.FromArgb(
Bits[index + 0],
Bits[index + 1],
Bits[index + 2],
Bits[index + 3]
);
return result;
}
public void Dispose()
{
if (Disposed) return;
Disposed = true;
Bitmap.Dispose();
BitsHandle.Free();
}
}
}