/
BitmapData.cs
126 lines (114 loc) · 4.19 KB
/
BitmapData.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
#if NET7_0_OR_GREATER
using System.Runtime.InteropServices.Marshalling;
#endif
namespace System.Drawing.Imaging
{
/// <summary>
/// Specifies the attributes of a bitmap image.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public sealed class BitmapData
{
private int _width;
private int _height;
private int _stride;
private PixelFormat _pixelFormat;
private IntPtr _scan0;
private int _reserved;
/// <summary>
/// Specifies the pixel width of the <see cref='Bitmap'/>.
/// </summary>
public int Width
{
get { return _width; }
set { _width = value; }
}
/// <summary>
/// Specifies the pixel height of the <see cref='Bitmap'/>.
/// </summary>
public int Height
{
get { return _height; }
set { _height = value; }
}
/// <summary>
/// Specifies the stride width of the <see cref='Bitmap'/>.
/// </summary>
public int Stride
{
get { return _stride; }
set { _stride = value; }
}
/// <summary>
/// Specifies the format of the pixel information in this <see cref='Bitmap'/>.
/// </summary>
public PixelFormat PixelFormat
{
get { return _pixelFormat; }
set
{
switch (value)
{
case PixelFormat.DontCare:
// case PixelFormat.Undefined: same as DontCare
case PixelFormat.Max:
case PixelFormat.Indexed:
case PixelFormat.Gdi:
case PixelFormat.Format16bppRgb555:
case PixelFormat.Format16bppRgb565:
case PixelFormat.Format24bppRgb:
case PixelFormat.Format32bppRgb:
case PixelFormat.Format1bppIndexed:
case PixelFormat.Format4bppIndexed:
case PixelFormat.Format8bppIndexed:
case PixelFormat.Alpha:
case PixelFormat.Format16bppArgb1555:
case PixelFormat.PAlpha:
case PixelFormat.Format32bppPArgb:
case PixelFormat.Extended:
case PixelFormat.Format16bppGrayScale:
case PixelFormat.Format48bppRgb:
case PixelFormat.Format64bppPArgb:
case PixelFormat.Canonical:
case PixelFormat.Format32bppArgb:
case PixelFormat.Format64bppArgb:
break;
default:
throw new System.ComponentModel.InvalidEnumArgumentException(nameof(value), unchecked((int)value), typeof(PixelFormat));
}
_pixelFormat = value;
}
}
/// <summary>
/// Specifies the address of the pixel data.
/// </summary>
public IntPtr Scan0
{
get { return _scan0; }
set { _scan0 = value; }
}
/// <summary>
/// Reserved. Do not use.
/// </summary>
public int Reserved
{
get { return _reserved; }
set { _reserved = value; }
}
internal ref int GetPinnableReference() => ref _width;
#if NET7_0_OR_GREATER
[CustomMarshaller(typeof(BitmapData), MarshalMode.ManagedToUnmanagedIn, typeof(PinningMarshaller))]
internal static unsafe class PinningMarshaller
{
public static ref int GetPinnableReference(BitmapData managed) => ref (managed is null ? ref Unsafe.NullRef<int>() : ref managed.GetPinnableReference());
// All usages in our currently supported scenarios will always go through GetPinnableReference
public static int* ConvertToUnmanaged(BitmapData managed) => throw new UnreachableException();
}
#endif
}
}