프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / KCOM / Controls / SignManager.xaml.cs @ b1c2c6fe

이력 | 보기 | 이력해설 | 다운로드 (8.18 KB)

1
using KCOM.Common;
2
using System;
3
using System.Collections.Generic;
4
using System.IO;
5
using System.Linq;
6
using System.Runtime.Serialization.Formatters.Binary;
7
using System.Text;
8
using System.Threading.Tasks;
9
using System.Windows;
10
using System.Windows.Controls;
11
using System.Windows.Data;
12
using System.Windows.Documents;
13
using System.Windows.Ink;
14
using System.Windows.Input;
15
using System.Windows.Media;
16
using System.Windows.Media.Imaging;
17
using System.Windows.Shapes;
18
using Telerik.Windows.Controls;
19

    
20
namespace KCOM.Controls
21
{
22
    /// <summary>
23
    /// SignManager.xaml에 대한 상호 작용 논리
24
    /// </summary>
25
    public partial class SignManager : UserControl
26
    {
27
        public SignManager()
28
        {
29
            InitializeComponent();
30

    
31
            this.Loaded += SignManager_Loaded;
32
        }
33

    
34
        private async void SignManager_Loaded(object sender, RoutedEventArgs e)
35
        {
36
            var strokeData = await ViewerDataModel.Instance.SystemMain.dzMainMenu.BaseTaskClient.GetSignStrokesAsync(App.ViewInfo.ProjectNO, App.ViewInfo.UserID);
37

    
38
            if(strokeData != null)
39
            {
40
                StringToStroke(strokeData);
41
            }
42
        }
43

    
44
        private void Reset_Click(object sender, RoutedEventArgs e)
45
        {
46
            txtInput.Text = "";
47
            txtInput.IsEnabled = true;
48

    
49
            SignCanvas.Strokes.Clear();
50
        }
51

    
52
        private async void Save_Click(object sender, RoutedEventArgs e)
53
        {
54
            //if (KCOM.VistaSecurity.IsAdmin())
55
            //{
56

    
57
            if (SignCanvas.Strokes.Count > 0)
58
            {
59

    
60
                var signImage = Convert.ToBase64String(SignatureToBitmapBytes());
61

    
62
                var pointes = SignCanvas.Strokes.Select(x => x.GetBounds());
63

    
64
                var _startX = pointes.Min(x => x.X);
65
                var _startY = pointes.Min(x => x.Y);
66

    
67
                var _actualWidth = pointes.Max(x => x.Right);
68
                _actualWidth = Math.Abs(_startX - _actualWidth);
69
                var _actualHeight = pointes.Max(x => x.Bottom);
70
                _actualHeight = Math.Abs(_startY - _actualHeight);
71

    
72
                var result = await ViewerDataModel.Instance.SystemMain.dzMainMenu.BaseTaskClient.SetSignDataAsync(
73
                             App.ViewInfo.UserID, signImage, (int)_startX, (int)_startY, (int)_actualWidth, (int)_actualHeight);
74

    
75
                if (result > 0)
76
                {
77
                    var strokesData = Convert.ToBase64String(StrokesToString());
78

    
79
                    var result2 = await ViewerDataModel.Instance.SystemMain.dzMainMenu.BaseTaskClient.SetSignStrokesAsync(
80
                                 App.ViewInfo.UserID,strokesData);
81

    
82
                    if (result2 > 0)
83
                    {
84
                        RadWindow.Alert(new DialogParameters()
85
                        {
86
                            Content = "Save Signature Completed."
87
                        });
88
                    }
89
                    //var signData = await ViewerDataModel.Instance.SystemMain.dzMainMenu.BaseTaskClient.GetSignDataAsync(App.ViewInfo.ProjectNO, App.ViewInfo.UserID);
90

    
91
                    //SetPreview(signData);
92
                }
93
            };
94
            //}
95
            //else
96
            //{
97
            //    MessageBox.Show("어드민 권한 필요");
98
            //}
99
        }
100

    
101
        private void SetPreview(string imgStr)
102
        {
103
            byte[] imageBytes = System.Convert.FromBase64String(imgStr);
104

    
105
            BitmapImage returnImage = new BitmapImage();
106

    
107
            using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
108
            {
109
                stream.WriteAsync(imageBytes, 0, imageBytes.Length);
110

    
111
                stream.Position = 0;
112
                System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
113
                returnImage.BeginInit();
114
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
115
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
116
                ms.Seek(0, System.IO.SeekOrigin.Begin);
117
                returnImage.StreamSource = ms;
118
                returnImage.EndInit();
119
                stream.Close();
120
            }
121
        }
122

    
123
        private byte[] SignatureToBitmapBytes()
124
        {
125
            RenderTargetBitmap rtb = new RenderTargetBitmap((int)SignCanvas.ActualWidth, (int)SignCanvas.ActualHeight, 96d, 96d, PixelFormats.Default);
126
            rtb.Render(SignCanvas);
127

    
128
            MemoryStream ms = new MemoryStream();
129
            System.Windows.Media.Imaging.PngBitmapEncoder encoder = new PngBitmapEncoder();
130
            BitmapFrame frame = BitmapFrame.Create(rtb);
131
            encoder.Frames.Add(frame);
132
            encoder.Save(ms);
133

    
134
            ms.Position = 0;
135

    
136
            byte[] bytes = new byte[ms.Length];
137
            ms.Read(bytes, 0, bytes.Length);
138

    
139
            return bytes;
140
        }
141

    
142
        private byte[] StrokesToString()
143
        {
144
            byte[] bytes = null;
145

    
146
            CustomStrokes customStrokes = new CustomStrokes();
147

    
148
            customStrokes.StrokeCollection = new Point[SignCanvas.Strokes.Count][];
149

    
150
            for (int i = 0; i < SignCanvas.Strokes.Count; i++)
151
            {
152
                customStrokes.StrokeCollection[i] = new Point[SignCanvas.Strokes[i].StylusPoints.Count];
153

    
154
                for (int j = 0; j < SignCanvas.Strokes[i].StylusPoints.Count; j++)
155
                {
156
                    customStrokes.StrokeCollection[i][j] = new Point();
157
                    customStrokes.StrokeCollection[i][j].X = SignCanvas.Strokes[i].StylusPoints[j].X;
158
                    customStrokes.StrokeCollection[i][j].Y = SignCanvas.Strokes[i].StylusPoints[j].Y;
159
                }
160
            }
161

    
162
            //Serialize
163
            using (MemoryStream ms = new MemoryStream())
164
            {
165
                BinaryFormatter bf = new BinaryFormatter();
166
                bf.Serialize(ms, customStrokes);
167

    
168
                ms.Position = 0;
169

    
170
                bytes = new byte[ms.Length];
171
                ms.Read(bytes, 0, bytes.Length);
172
            }
173

    
174
            return bytes;
175
        }
176

    
177
        private void StringToStroke(string strokesData)
178
        {
179
            try
180
            {
181
                
182
                //deserialize it
183
                BinaryFormatter bf = new BinaryFormatter();
184
                MemoryStream ms = new MemoryStream(Convert.FromBase64String(strokesData));
185

    
186
                CustomStrokes customStrokes = (CustomStrokes)bf.Deserialize(ms);
187

    
188
                //rebuilt it
189
                for (int i = 0; i < customStrokes.StrokeCollection.Length; i++)
190
                {
191
                    if (customStrokes.StrokeCollection[i] != null)
192
                    {
193
                        StylusPointCollection stylusCollection = new StylusPointCollection(customStrokes.StrokeCollection[i]);
194

    
195
                        Stroke stroke = new Stroke(stylusCollection);
196
                        StrokeCollection strokes = new StrokeCollection();
197
                        strokes.Add(stroke);
198

    
199
                        SignCanvas.Strokes.Add(strokes);
200
                    }
201
                }
202
            }
203
            catch (Exception ex)
204
            {
205
            }
206
        }
207

    
208
        private string GetSignData()
209
        {
210
            string result = "";
211

    
212
            try
213
            {
214

    
215
            }
216
            catch (Exception)
217
            {
218

    
219
                throw;
220
            }
221

    
222
            return result;
223
        }
224

    
225
        private void SignCanvas_SourceUpdated(object sender, DataTransferEventArgs e)
226
        {
227
            if(SignCanvas.Strokes.Count() > 0)
228
            {
229
                txtInput.IsEnabled = false;
230
            }
231
            else
232
            {
233
                txtInput.IsEnabled = true;
234
            }
235
        }
236

    
237
        private void SignCanvas_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
238
        {
239
            if (SignCanvas.Strokes.Count() > 0)
240
            {
241
                txtInput.IsEnabled = false;
242
            }
243
            else
244
            {
245
                txtInput.IsEnabled = true;
246
            }
247
        }
248
    }
249

    
250
    [Serializable]
251
    public sealed class CustomStrokes
252
    {
253
        public CustomStrokes() { }
254

    
255
        /// <summary>
256
        /// The first index is the stroke no.
257
        /// The second index is for the keep the 2D point.
258
        /// </summary>
259
        public Point[][] StrokeCollection;
260
    }
261
}
클립보드 이미지 추가 (최대 크기: 500 MB)