프로젝트

일반

사용자정보

개정판 74abcf6f

ID74abcf6f6a706ead3c5666b598790fadac032fbf
상위 d21e9c15
하위 77cdac33

김태성이(가) 3년 이상 전에 추가함

sign manager 추가

Change-Id: Ia511ce9bf05e3a238353a549118d3b29ab673aa0

차이점 보기:

KCOM/Controls/SignManager.xaml.cs
1 1
using KCOM.Common;
2 2
using System;
3 3
using System.Collections.Generic;
4
using System.IO;
4 5
using System.Linq;
6
using System.Runtime.Serialization.Formatters.Binary;
5 7
using System.Text;
6 8
using System.Threading.Tasks;
7 9
using System.Windows;
8 10
using System.Windows.Controls;
9 11
using System.Windows.Data;
10 12
using System.Windows.Documents;
13
using System.Windows.Ink;
11 14
using System.Windows.Input;
12 15
using System.Windows.Media;
13 16
using System.Windows.Media.Imaging;
......
24 27
        public SignManager()
25 28
        {
26 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
            }
27 42
        }
28 43

  
29 44
        private void Reset_Click(object sender, RoutedEventArgs e)
......
36 51

  
37 52
        private async void Save_Click(object sender, RoutedEventArgs e)
38 53
        {
39
            var items = await ViewerDataModel.Instance.SystemMain.dzMainMenu.BaseTaskClient.SetSignDataAsync(
40
                         App.UserID,"");
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
            }
41 204
        }
42 205

  
43 206
        private string GetSignData()
......
81 244
            }
82 245
        }
83 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
    }
84 259
}

내보내기 Unified diff

클립보드 이미지 추가 (최대 크기: 500 MB)