프로젝트

일반

사용자정보

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

markus / KCOM / Controls / SignManager.xaml.cs @ 74abcf6f

이력 | 보기 | 이력해설 | 다운로드 (8.05 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
                var _actualHeight = pointes.Max(x => x.Bottom);
69

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

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

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

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

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

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

    
103
            BitmapImage returnImage = new BitmapImage();
104

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

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

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

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

    
132
            ms.Position = 0;
133

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

    
137
            return bytes;
138
        }
139

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

    
144
            CustomStrokes customStrokes = new CustomStrokes();
145

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

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

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

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

    
166
                ms.Position = 0;
167

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

    
172
            return bytes;
173
        }
174

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

    
184
                CustomStrokes customStrokes = (CustomStrokes)bf.Deserialize(ms);
185

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

    
193
                        Stroke stroke = new Stroke(stylusCollection);
194
                        StrokeCollection strokes = new StrokeCollection();
195
                        strokes.Add(stroke);
196

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

    
206
        private string GetSignData()
207
        {
208
            string result = "";
209

    
210
            try
211
            {
212

    
213
            }
214
            catch (Exception)
215
            {
216

    
217
                throw;
218
            }
219

    
220
            return result;
221
        }
222

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

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

    
248
    [Serializable]
249
    public sealed class CustomStrokes
250
    {
251
        public CustomStrokes() { }
252

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