프로젝트

일반

사용자정보

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

markus / KCOM / Controls / SignManager.xaml.cs @ 58dd9e89

이력 | 보기 | 이력해설 | 다운로드 (8.2 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
            if(strokeData != null)
38
            {
39
                StringToStroke(strokeData);
40
            }
41
        }
42

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

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

    
51
        private async void Save_Click(object sender, RoutedEventArgs e)
52
        {
53
            if (SignCanvas.Strokes.Count > 0)
54
            {
55
                var signImage = Convert.ToBase64String(SignatureToBitmapBytes());
56

    
57
                var pointes = SignCanvas.Strokes.Select(x => x.GetBounds());
58

    
59
                var _startX = pointes.Min(x => x.X);
60
                var _startY = pointes.Min(x => x.Y);
61

    
62
                var _actualWidth = pointes.Max(x => x.Right);
63
                _actualWidth = Math.Abs(_startX - _actualWidth);
64
                var _actualHeight = pointes.Max(x => x.Bottom);
65
                _actualHeight = Math.Abs(_startY - _actualHeight);
66

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

    
70
                if (result > 0)
71
                {
72
                    var strokesData = Convert.ToBase64String(StrokesToString());
73

    
74
                    var result2 = await ViewerDataModel.Instance.SystemMain.dzMainMenu.BaseTaskClient.SetSignStrokesAsync(
75
                                 App.ViewInfo.UserID,strokesData);
76

    
77
                    if (result2 > 0)
78
                    {
79
                        RadWindow.Alert(new DialogParameters()
80
                        {
81
                            Content = "Save Signature Completed."
82
                        });
83
                    }
84
                    //var signData = await ViewerDataModel.Instance.SystemMain.dzMainMenu.BaseTaskClient.GetSignDataAsync(App.ViewInfo.ProjectNO, App.ViewInfo.UserID);
85

    
86
                    //SetPreview(signData);
87
                }
88
            };
89
        }
90

    
91
        private void SetPreview(string imgStr)
92
        {
93
            byte[] imageBytes = System.Convert.FromBase64String(imgStr);
94

    
95
            BitmapImage returnImage = new BitmapImage();
96

    
97
            using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
98
            {
99
                stream.WriteAsync(imageBytes, 0, imageBytes.Length);
100

    
101
                stream.Position = 0;
102
                System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
103
                returnImage.BeginInit();
104
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
105
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
106
                ms.Seek(0, System.IO.SeekOrigin.Begin);
107
                returnImage.StreamSource = ms;
108
                returnImage.EndInit();
109
                stream.Close();
110
            }
111
        }
112

    
113
        /// <summary>
114
        /// Sign을 비트맵으로 변환하여 리턴한다.
115
        /// </summary>
116
        /// <returns></returns>
117
        private byte[] SignatureToBitmapBytes()
118
        {
119
            RenderTargetBitmap rtb = new RenderTargetBitmap((int)SignCanvas.ActualWidth, (int)SignCanvas.ActualHeight, 96d, 96d, PixelFormats.Default);
120
            rtb.Render(SignCanvas);
121

    
122
            byte[] bytes = null;
123
            using (MemoryStream ms = new MemoryStream())
124
            {
125
                System.Windows.Media.Imaging.PngBitmapEncoder encoder = new PngBitmapEncoder();
126
                BitmapFrame frame = BitmapFrame.Create(rtb);
127
                encoder.Frames.Add(frame);
128
                encoder.Save(ms);
129

    
130
                ms.Position = 0;
131

    
132
                bytes = new byte[ms.Length];
133
                ms.Read(bytes, 0, bytes.Length);
134
            }
135

    
136
            return bytes;
137
        }
138

    
139
        private byte[] StrokesToString()
140
        {
141
            byte[] bytes = null;
142

    
143
            CustomStrokes customStrokes = new CustomStrokes();
144

    
145
            customStrokes.StrokeCollection = new Point[SignCanvas.Strokes.Count][];
146

    
147
            for (int i = 0; i < SignCanvas.Strokes.Count; i++)
148
            {
149
                customStrokes.StrokeCollection[i] = new Point[SignCanvas.Strokes[i].StylusPoints.Count];
150

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

    
159
            //Serialize
160
            using (MemoryStream ms = new MemoryStream())
161
            {
162
                BinaryFormatter bf = new BinaryFormatter();
163
                bf.Serialize(ms, customStrokes);
164

    
165
                ms.Position = 0;
166

    
167
                bytes = new byte[ms.Length];
168
                ms.Read(bytes, 0, bytes.Length);
169
            }
170

    
171
            return bytes;
172
        }
173

    
174
        private void StringToStroke(string strokesData)
175
        {
176
            try
177
            {
178
                //deserialize it
179
                BinaryFormatter bf = new BinaryFormatter();
180
                MemoryStream ms = new MemoryStream(Convert.FromBase64String(strokesData));
181

    
182
                CustomStrokes customStrokes = (CustomStrokes)bf.Deserialize(ms);
183

    
184
                //rebuilt it
185
                for (int i = 0; i < customStrokes.StrokeCollection.Length; i++)
186
                {
187
                    if (customStrokes.StrokeCollection[i] != null)
188
                    {
189
                        StylusPointCollection stylusCollection = new StylusPointCollection(customStrokes.StrokeCollection[i]);
190

    
191
                        Stroke stroke = new Stroke(stylusCollection);
192
                        StrokeCollection strokes = new StrokeCollection();
193
                        strokes.Add(stroke);
194

    
195
                        SignCanvas.Strokes.Add(strokes);
196
                    }
197
                }
198
            }
199
            catch (Exception ex)
200
            {
201
            }
202
        }
203

    
204
        private string GetSignData()
205
        {
206
            string result = "";
207

    
208
            try
209
            {
210

    
211
            }
212
            catch (Exception)
213
            {
214

    
215
                throw;
216
            }
217

    
218
            return result;
219
        }
220

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

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

    
246
    [Serializable]
247
    public sealed class CustomStrokes
248
    {
249
        public CustomStrokes() { }
250

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