WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
UnsafeBitmap.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Drawing;
5 using System.Drawing.Imaging;
6 
7 namespace DirectOutput.General.BitmapHandling
8 {
9 
14  public unsafe class UnsafeBitmap
15  {
16  Bitmap bitmap;
17 
18  // three elements used for MakeGreyUnsafe
19  int width;
20  BitmapData bitmapData = null;
21  Byte* pBase = null;
22 
23  public UnsafeBitmap(Bitmap bitmap)
24  {
25  this.bitmap = new Bitmap(bitmap);
26  }
27 
28  public UnsafeBitmap(int width, int height)
29  {
30  this.bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
31  }
32 
33  public void Dispose()
34  {
35  bitmap.Dispose();
36  }
37 
38  public Bitmap Bitmap
39  {
40  get
41  {
42  return (bitmap);
43  }
44  }
45 
46  private Point PixelSize
47  {
48  get
49  {
50  GraphicsUnit unit = GraphicsUnit.Pixel;
51  RectangleF bounds = bitmap.GetBounds(ref unit);
52 
53  return new Point((int)bounds.Width, (int)bounds.Height);
54  }
55  }
56 
57  public void LockBitmap()
58  {
59  GraphicsUnit unit = GraphicsUnit.Pixel;
60  RectangleF boundsF = bitmap.GetBounds(ref unit);
61  Rectangle bounds = new Rectangle((int)boundsF.X,
62  (int)boundsF.Y,
63  (int)boundsF.Width,
64  (int)boundsF.Height);
65 
66  // Figure out the number of bytes in a row
67  // This is rounded up to be a multiple of 4
68  // bytes, since a scan line in an image must always be a multiple of 4 bytes
69  // in length.
70  width = (int)boundsF.Width * sizeof(PixelData);
71  if (width % 4 != 0)
72  {
73  width = 4 * (width / 4 + 1);
74  }
75  bitmapData =
76  bitmap.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
77 
78  pBase = (Byte*)bitmapData.Scan0.ToPointer();
79  }
80 
81  public PixelData GetPixel(int x, int y)
82  {
83  PixelData returnValue = *PixelAt(x, y);
84  return returnValue;
85  }
86 
87  public void UnlockBitmap()
88  {
89  bitmap.UnlockBits(bitmapData);
90  bitmapData = null;
91  pBase = null;
92  }
93  public PixelData* PixelAt(int x, int y)
94  {
95 
96  return (PixelData*)(pBase + y * width + x * sizeof(PixelData));
97 
98  }
99  }
100 
101 }
102 
Struct holding the data for a single pixel in a bitmap.
Definition: PixelData.cs:12
This class allows fast access to the pixels of a bitmap. The code was inspired/stolen from this threa...
Definition: UnsafeBitmap.cs:14