-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindowViewModel.cs
185 lines (162 loc) · 5.23 KB
/
MainWindowViewModel.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace ImgSampleApplication
{
class MainWindowViewModel : INotifyPropertyChanged
{
BitmapSource m_bitmapSource;
byte[] aBuf;
public BitmapSource p_bitmapSource
{
get => m_bitmapSource;
set
{
m_bitmapSource = value;
OnPropertyChanged();
}
}
public MainWindowViewModel()
{
}
public RelayCommand ImageLoadCommand
{
get => new RelayCommand(ImageLoad);
}
MemoryMappedFile m_MMF;
MemoryMappedViewStream m_MMVS;
long m_Adress;
/// <summary>
/// 이미지 샘플링을 위한 비트맵소스 읽어오는 함수
/// </summary>
/// <list type="table">
/// <listheader>
/// <term>22-09-16</term>
/// <term>이하운</term>
/// <term>다이얼로그에서 읽어옵니다</term>
/// <term>비고</term>
/// </listheader>
/// <item>
/// <term>2022-09-16</term>
/// <term>이하운</term>
/// <term>생성</term>
/// <term>-</term>
/// </item>
/// </list>
/// <param name="args">없음</param>
/// <returns> 없음 </returns>
private void ImageLoad()
{
//MMF와 MMVS 만들기
m_MMF = MemoryMappedFile.CreateOrOpen("Memory", 1024 * 1024 * 1024);
m_MMVS = m_MMF.CreateViewStream();
//MMVS에 입력할 버퍼 만들기
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Image Files|*.bmp";
dlg.InitialDirectory = @"D:\Images";
if (dlg.ShowDialog() == DialogResult.OK)
{
Bitmap bitmap = new Bitmap(dlg.FileName);
aBuf = new byte[bitmap.Width * bitmap.Height];
for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
aBuf[bitmap.Width * y + x] = bitmap.GetPixel(x, y).R;
}
};
}
//MMVS에 버퍼 입력하기
m_MMVS.Write(aBuf, 0, aBuf.Length);
unsafe
{
byte* p = null;
m_MMF.CreateViewAccessor().SafeMemoryMappedViewHandle.AcquirePointer(ref p);
m_Adress = (long)(IntPtr)p;
}
}
public byte[] ReadAllBytes(string fileName)
{
byte[] buffer = null;
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
{
buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
}
return buffer;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public class RelayCommand : ICommand
{
#region Declarations
readonly Func<Boolean> _canExecute;
readonly Action _execute;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="RelayCommand<T>"/> class and the command can always be executed.
/// </summary>
/// <param name="execute">The execution logic.</param>
public RelayCommand(Action execute)
: this(execute, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RelayCommand<T>"/> class.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action execute, Func<Boolean> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion
#region ICommand Members
public event EventHandler CanExecuteChanged
{
add
{
if (_canExecute != null)
CommandManager.RequerySuggested += value;
}
remove
{
if (_canExecute != null)
CommandManager.RequerySuggested -= value;
}
}
[DebuggerStepThrough]
public Boolean CanExecute(Object parameter)
{
return _canExecute == null ? true : _canExecute();
}
public void Execute(Object parameter)
{
try
{
_execute();
}
catch (Exception)
{
}
}
#endregion
}
}