-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFastBitmap.cs
91 lines (78 loc) · 2.16 KB
/
FastBitmap.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
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
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace SupSubtitleParser
{
public class FastBitmap
{
private Bitmap _bmp = null;
private BitmapData _bmpData = null;
private IntPtr _ptr = new IntPtr(0);
public FastBitmap(Bitmap bmp)
{
_bmp = bmp;
}
public FastBitmap(Size size)
{
_bmp = new Bitmap(size.Width, size.Height);
}
public FastBitmap(int width, int height)
{
_bmp = new Bitmap(width, height);
}
private void LockBits()
{
_bmpData = _bmp.LockBits(
new Rectangle(0, 0, _bmp.Width, _bmp.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format32bppArgb);
_ptr = _bmpData.Scan0;
}
public void AddPixel(Color color)
{
if (_bmpData == null)
{
LockBits();
}
byte[] byt = new byte[] { color.R, color.G, color.B, color.A };
Marshal.Copy(byt, 0, _ptr, 4);
_ptr += 4;
}
public void Unlock()
{
if (_bmpData != null)
{
_bmp.UnlockBits(_bmpData);
_bmpData = null;
}
}
public Bitmap GetBitmap()
{
Unlock();
return _bmp;
}
public void SaveAs(string fileName)
{
Unlock();
ImageFormat fmt = ImageFormat.Bmp;
if (fileName.ToLower().EndsWith(".jpg"))
{
fmt = ImageFormat.Jpeg;
}
else if (fileName.ToLower().EndsWith(".png"))
{
fmt = ImageFormat.Png;
}
else if (fileName.ToLower().EndsWith(".gif"))
{
fmt = ImageFormat.Gif;
}
else if (fileName.ToLower().EndsWith(".tiff") || fileName.ToLower().EndsWith(".tif"))
{
fmt = ImageFormat.Tiff;
}
_bmp.Save(fileName, fmt);
}
}
}