-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitmapWrapper.cs
49 lines (44 loc) · 1.63 KB
/
BitmapWrapper.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
namespace Pixelate_Core
{
public class BitmapWrapper : IDisposable
{
Bitmap bitmap;
BitmapData data;
byte[] RGBs;
public Bitmap Bitmap { get { return bitmap; } }
public byte[] Array { get { return RGBs; } }
public BitmapWrapper(Bitmap bitmap)
{
this.bitmap = bitmap;
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
data = bitmap.LockBits(rect, ImageLockMode.ReadWrite, bitmap.PixelFormat);
IntPtr ptr = data.Scan0;
int bytes = Math.Abs(data.Stride) * bitmap.Height;
RGBs = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, RGBs, 0, bytes);
}
public void SetPixel(int x, int y, Color color)
{
RGBs[(y * data.Width + x) * 4] = color.B;
RGBs[(y * data.Width + x) * 4 + 1] = color.G;
RGBs[(y * data.Width + x) * 4 + 2] = color.R;
RGBs[(y * data.Width + x) * 4 + 3] = color.A;
}
public Color GetPixel(int x, int y)
{
return Color.FromArgb(RGBs[(y * data.Width + x) * 4 + 3], RGBs[(y * data.Width + x) * 4 + 2], RGBs[(y * data.Width + x) * 4 + 1], RGBs[(y * data.Width + x) * 4]);
}
public void Dispose()
{
System.Runtime.InteropServices.Marshal.Copy(RGBs, 0, data.Scan0, Math.Abs(data.Stride) * bitmap.Height);
bitmap.UnlockBits(data);
}
}
}