-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathBitmapBuffer.cs
More file actions
96 lines (78 loc) · 2.69 KB
/
BitmapBuffer.cs
File metadata and controls
96 lines (78 loc) · 2.69 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
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright © 2010-2017 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using CefSharp.Structs;
namespace CefSharp.OffScreen
{
public class BitmapBuffer
{
private const int BytesPerPixel = 4;
private const PixelFormat Format = PixelFormat.Format32bppPArgb;
private byte[] buffer;
/// <summary>
/// Number of bytes
/// </summary>
public int NumberOfBytes { get; private set; }
/// <summary>
/// Width
/// </summary>
public int Width { get; private set; }
/// <summary>
/// Height
/// </summary>
public int Height { get; private set; }
/// <summary>
/// Dirty Rect - unified region containing th
/// </summary>
public Rect DirtyRect { get; private set; }
public object BitmapLock { get; private set; }
public BitmapBuffer(object bitmapLock)
{
BitmapLock = bitmapLock;
}
public byte[] Buffer
{
get { return buffer; }
}
//TODO: May need to Pin the buffer in memory using GCHandle.Alloc(this.buffer, GCHandleType.Pinned);
private void ResizeBuffer(int width, int height)
{
if (buffer == null || width != Width || height != Height)
{
//No of Pixels (width * height) * BytesPerPixel
NumberOfBytes = width * height * BytesPerPixel;
buffer = new byte[NumberOfBytes];
Width = width;
Height = height;
}
}
public void UpdateBuffer(int width, int height, IntPtr buffer, Rect dirtyRect)
{
lock (BitmapLock)
{
DirtyRect = dirtyRect;
ResizeBuffer(width, height);
Marshal.Copy(buffer, this.buffer, 0, NumberOfBytes);
}
}
public Bitmap CreateBitmap()
{
lock (BitmapLock)
{
if (Width == 0 || Height == 0 || buffer.Length == 0)
{
return null;
}
var bitmap = new Bitmap(Width, Height, Format);
var bitmapData = bitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.WriteOnly, Format);
Marshal.Copy(Buffer, 0, bitmapData.Scan0, NumberOfBytes);
bitmap.UnlockBits(bitmapData);
return bitmap;
}
}
}
}