1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.Drawing; |
4 |
using System.Drawing.Imaging; |
5 |
using System.Runtime.InteropServices; |
6 |
|
7 |
namespace Oni.Imaging |
8 |
{ |
9 |
internal static class SysReader |
10 |
{ |
11 |
public static Surface Read(string filePath) |
12 |
{ |
13 |
using (Bitmap bmp = new Bitmap(filePath, false)) |
14 |
{ |
15 |
SurfaceFormat surfaceFormat; |
16 |
PixelFormat pixelFormat; |
17 |
|
18 |
if (bmp.RawFormat == ImageFormat.Jpeg || bmp.RawFormat == ImageFormat.Bmp) |
19 |
{ |
20 |
surfaceFormat = SurfaceFormat.BGRX; |
21 |
pixelFormat = PixelFormat.Format32bppRgb; |
22 |
} |
23 |
else |
24 |
{ |
25 |
surfaceFormat = SurfaceFormat.BGRA; |
26 |
pixelFormat = PixelFormat.Format32bppArgb; |
27 |
} |
28 |
|
29 |
var surface = new Surface(bmp.Width, bmp.Height, surfaceFormat); |
30 |
var rc = new Rectangle(0, 0, bmp.Width, bmp.Height); |
31 |
|
32 |
var data = bmp.LockBits(rc, ImageLockMode.ReadOnly, pixelFormat); |
33 |
Marshal.Copy(data.Scan0, surface.Data, 0, surface.Data.Length); |
34 |
bmp.UnlockBits(data); |
35 |
|
36 |
return surface; |
37 |
} |
38 |
} |
39 |
} |
40 |
} |