MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
DirectBitmap.cs
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Drawing.Imaging;
5using System.Linq;
6using System.Runtime.InteropServices;
7using System.Text;
8using System.Threading.Tasks;
9
10namespace MyCaffe.basecode
11{
18 public class DirectBitmap : IDisposable
19 {
23 public Bitmap Bitmap { get; private set; }
27 public Int32[] Bits { get; private set; }
31 public bool Disposed { get; private set; }
35 public int Height { get; private set; }
39 public int Width { get; private set; }
43 protected GCHandle BitsHandle { get; private set; }
44
50 public DirectBitmap(int width, int height)
51 {
52 Width = width;
53 Height = height;
54 Bits = new Int32[width * height];
55 BitsHandle = GCHandle.Alloc(Bits, GCHandleType.Pinned);
56 Bitmap = new Bitmap(width, height, width * 4, PixelFormat.Format32bppPArgb, BitsHandle.AddrOfPinnedObject());
57 }
58
65 public void SetPixel(int x, int y, Color colour)
66 {
67 int index = x + (y * Width);
68 int col = colour.ToArgb();
69
70 Bits[index] = col;
71 }
72
78 public void SetRow(int y, Color colour)
79 {
80 int col = colour.ToArgb();
81 int index = y * Width;
82
83 for (int x = 0; x < Width; x++)
84 {
85 Bits[index + x] = col;
86 }
87 }
88
95 public Color GetPixel(int x, int y)
96 {
97 int index = x + (y * Width);
98 int col = Bits[index];
99 Color result = Color.FromArgb(col);
100
101 return result;
102 }
103
107 public void Dispose()
108 {
109 if (Disposed) return;
110 Disposed = true;
111
112 if (Bitmap != null)
113 Bitmap.Dispose();
114
115 BitsHandle.Free();
116 }
117 }
118}
The DirectBitmap class provides an efficient bitmap creating class.
Definition: DirectBitmap.cs:19
Int32[] Bits
Returns an array containing the raw bitmap data.
Definition: DirectBitmap.cs:27
int Height
Returns the bitmap height.
Definition: DirectBitmap.cs:35
void Dispose()
Release all resources used.
DirectBitmap(int width, int height)
The constructro.
Definition: DirectBitmap.cs:50
GCHandle BitsHandle
Returns the bitmap memory handle.
Definition: DirectBitmap.cs:43
bool Disposed
Returns true when disposed.
Definition: DirectBitmap.cs:31
void SetRow(int y, Color colour)
Set an entire row to the same color.
Definition: DirectBitmap.cs:78
Color GetPixel(int x, int y)
Returns the color of a pixel in the bitmap.
Definition: DirectBitmap.cs:95
void SetPixel(int x, int y, Color colour)
Sets a pixel within the bitmap with the specified color.
Definition: DirectBitmap.cs:65
int Width
Returns the bitmap width.
Definition: DirectBitmap.cs:39
Bitmap Bitmap
Returns the Bitmap itself.
Definition: DirectBitmap.cs:23
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12