-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadOnlyBitmapData.cs
More file actions
executable file
·83 lines (73 loc) · 2.41 KB
/
ReadOnlyBitmapData.cs
File metadata and controls
executable file
·83 lines (73 loc) · 2.41 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
namespace ImageVerifier.ImageManagement
{
public unsafe class ReadOnlyBitmapData
{
private Int32[] imageData;
private int width;
private int height;
public ReadOnlyBitmapData(Bitmap b)
{
Bitmap toDispose = null;
try
{
if (b.PixelFormat != PixelFormat.Format32bppArgb)
{
Bitmap input = b;
b = new Bitmap(b.Width, b.Height, PixelFormat.Format32bppArgb);
toDispose = b;
using (Graphics g = Graphics.FromImage(b))
{
g.DrawImage(input, 0, 0);
}
}
width = b.Width;
height = b.Height;
BitmapData lockData = b.LockBits(
new Rectangle(0, 0, width, height),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
PixelFormat.Format32bppArgb);
// Create an array to store image data
imageData = new Int32[width * height];
// Use the Marshal class to copy image data
System.Runtime.InteropServices.Marshal.Copy(
lockData.Scan0, imageData, 0, imageData.Length);
b.UnlockBits(lockData);
}
finally
{
if (toDispose != null)
{
toDispose.Dispose();
}
}
}
public Color GetPixel(int x, int y)
{
int pixelValue = imageData[y * width + x];
return Color.FromArgb(pixelValue);
}
public Color GetPixel(int x, int y, Color defColor)
{
if (x >= width || y >= height)
{
return defColor;
}
int pixelValue = imageData[y * width + x];
return Color.FromArgb(pixelValue);
}
public int GetRawARGBValue(int x, int y, int defValue)
{
if (x >= width || y >= height)
{
return defValue;
}
return imageData[y * width + x];
}
}
}