■ MemoryMappedFile 클래스의 CreateFromFile 정적 메소드를 사용해 메모리 매핑 파일을 만드는 방법을 보여준다.
▶ CustomColor.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 |
using System; namespace TestProject { /// <summary> /// 커스텀 색상 /// </summary> public struct CustomColor { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 적색 /// </summary> public short Red; /// <summary> /// 녹색 /// </summary> public short Green; /// <summary> /// 청색 /// </summary> public short Blue; /// <summary> /// 알파 /// </summary> public short Alpha; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 밝게하기 - Brighten(value) /// <summary> /// 밝게하기 /// </summary> /// <param name="value">값</param> public void Brighten(short value) { Red = (short)Math.Min(short.MaxValue, (int)Red + value); Green = (short)Math.Min(short.MaxValue, (int)Green + value); Blue = (short)Math.Min(short.MaxValue, (int)Blue + value); Alpha = (short)Math.Min(short.MaxValue, (int)Alpha + value); } #endregion } } |
▶ Program.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 |
using System.IO; using System.IO.MemoryMappedFiles; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { string mapFilePath = "d:\\ExtremelyLargeImage.data"; string mapName = "testMap"; long dataOffset = 0x10000000; // 256MB long dataLength = 0x20000000; // 512MB long mapFileLength = 0x40000000; // 1GB if(!File.Exists(mapFilePath)) { using(MemoryMappedFile file = MemoryMappedFile.CreateFromFile(mapFilePath, FileMode.CreateNew, mapName, mapFileLength)) { } } using(MemoryMappedFile file = MemoryMappedFile.CreateFromFile(mapFilePath, FileMode.Open, mapName)) { using(MemoryMappedViewAccessor accessor = file.CreateViewAccessor(dataOffset, dataLength)) { int colorSize = Marshal.SizeOf(typeof(CustomColor)); for (long i = 0; i < dataLength; i += colorSize) { accessor.Read(i, out CustomColor color); color.Brighten(10); accessor.Write(i, ref color); } } } } #endregion } } |