弹来弹去跑马灯!

c# WPF convert photo to Sketch effects

using the online website https://imagetosketch.com/

 

 

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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<Window x:Class="WpfMosaic.PhotoSketchWind"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfMosaic"
        mc:Ignorable="d"
        Title="PhotoSketchWind" Height="450" Width="800">
    <Grid>
        <Border>
            <WrapPanel ScrollViewer.HorizontalScrollBarVisibility="Visible"
              ScrollViewer.VerticalScrollBarVisibility="Visible"
              x:Name="wrapPanel">
            </WrapPanel>
        </Border>
 
    </Grid>
</Window>
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
 
namespace WpfMosaic
{
    /// <summary>
    /// Interaction logic for PhotoSketchWind.xaml
    /// </summary>
    public partial class PhotoSketchWind : Window
    {
        public PhotoSketchWind()
        {
            InitializeComponent();
            Loaded += PhotoSketchWind_Loaded;
        }
 
        private void PhotoSketchWind_Loaded(object sender, RoutedEventArgs e)
        {
            string imgFile = @"C:\Users\gwang\source\repos\WpfMosaic\bin\Debug\12.jpg";
            ProcessPhoto(imgFile);
        }
 
 
 
        void ProcessPhoto(string imgFile)
        {
            var files = ImageSketchServer.ProcessPhoto(imgFile);
            if (files != null && files.Count > 0)
            {
                foreach (var f in files)
                {
                    var img = new Image() { MaxWidth = 222, Source = new BitmapImage(new Uri(f, UriKind.RelativeOrAbsolute)) };
                    img.Tag = f;
                    img.MouseLeftButtonDown += Img_MouseLeftButtonDown;
                    wrapPanel.Children.Add(img);
                }
 
            }
 
        }
 
        private void Img_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            System.Diagnostics.Process.Start("" + (sender as Image).Tag);
        }
    }
}
 
 
 
 
 
 
 
 
 
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
 
namespace WpfMosaic
{
    public class Httphelper
    {
 
 
        public static string Upload(string url, string filePath)
        {
            try
            {
                string modelId = "4f1e2e3d-6231-4b13-96a4-835e5af10394";
                string updateTime = "2016-11-03 14:17:25";
                string encrypt = "f933797503d6e2c36762428a280e0559";
                string fileName = Path.GetFileName(filePath);
 
                byte[] fileContentByte = File.ReadAllBytes(filePath);
 
                //#region 将文件转成二进制
 
                //FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                //fileContentByte = new byte[fs.Length]; // 二进制文件
                //fs.Read(fileContentByte, 0, Convert.ToInt32(fs.Length));
                //fs.Close();
 
                //#endregion
 
 
                #region 定义请求体中的内容 并转成二进制
 
                string boundary = "WebKitFormBoundarybgqrda0HwT2BjMam";
                string Enter = "\r\n";
 
                string modelIdStr = "--" + boundary + Enter
                        + "Content-Disposition: form-data; name=\"modelId\"" + Enter + Enter
                        + modelId + Enter;
 
                string fileContentStr = "--" + boundary + Enter
                        + "Content-Type:bimage/jpeg" + Enter
                        + "Content-Disposition: form-data; name=\"myfile\"; filename=\"" + fileName + "\"" + Enter + Enter;
 
                string updateTimeStr = Enter + "--" + boundary + Enter
                        + "Content-Disposition: form-data; name=\"updateTime\"" + Enter + Enter
                        + updateTime;
 
                string encryptStr = Enter + "--" + boundary + Enter
                        + "Content-Disposition: form-data; name=\"encrypt\"" + Enter + Enter
                        + encrypt + Enter + "--" + boundary + "--";
 
 
                var modelIdStrByte = Encoding.UTF8.GetBytes(modelIdStr);//modelId所有字符串二进制
 
                var fileContentStrByte = Encoding.UTF8.GetBytes(fileContentStr);//fileContent一些名称等信息的二进制(不包含文件本身)
 
                var updateTimeStrByte = Encoding.UTF8.GetBytes(updateTimeStr);//updateTime所有字符串二进制
 
                var encryptStrByte = Encoding.UTF8.GetBytes(encryptStr);//encrypt所有字符串二进制
 
                var endString = Encoding.UTF8.GetBytes(Enter + "--" + boundary + "--");
 
                #endregion
 
 
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "multipart/form-data;boundary=" + boundary;
 
                Stream myRequestStream = request.GetRequestStream();//定义请求流
 
                #region 将各个二进制 安顺序写入请求流 modelIdStr -> (fileContentStr + fileContent) -> uodateTimeStr -> encryptStr
 
                // myRequestStream.Write(modelIdStrByte, 0, modelIdStrByte.Length);
 
                myRequestStream.Write(fileContentStrByte, 0, fileContentStrByte.Length);
                myRequestStream.Write(fileContentByte, 0, fileContentByte.Length);
 
                //  myRequestStream.Write(updateTimeStrByte, 0, updateTimeStrByte.Length);
 
                //  myRequestStream.Write(encryptStrByte, 0, encryptStrByte.Length);
 
                myRequestStream.Write(endString, 0, endString.Length);
 
 
                #endregion
 
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
 
                string retString = myStreamReader.ReadToEnd();
 
                myStreamReader.Close();
                myResponseStream.Close();
 
                return retString;
            }
            catch
            {
 
                return "";
 
            }
 
        }
        public static string HttpGet(string url)
        {
            try
            {
                //https://access.sketcherai.com:8999/status/iDHMujs3
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "Get";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
 
                myStreamReader.Close();
                myResponseStream.Close();
 
                return retString;
            }
            catch
            {
 
                return "";
            }
 
        }
 
 
    }
 
 
    public class ImageSketchServer
    {
        public static List<string> ProcessPhoto(string filePath)
        {
            string url = "https://access.sketcherai.com:8999/upload";
            string token = Httphelper.Upload(url, filePath);
            if (string.IsNullOrEmpty(token))
            {
                return null;
            }
            url = "https://access.sketcherai.com:8999/status/" + token;
            string abcs = Httphelper.HttpGet(url);
 
            if (string.IsNullOrEmpty(abcs))
            {
                return null;
            }
            else
            {
 
                //thumb img: https://access.sketcherai.com:8889/results/iDHMujs3_a_tn.jpg
                //org img: https://access.sketcherai.com:8889/results/iDHMujs3_a.jpg
 
                List<string> list = new List<string>();
                foreach (var c in abcs)
                {
                    list.Add("https://access.sketcherai.com:8889/results/iDHMujs3_" + c + "_tn.jpg");
                }
                return list;
            }
 
 
        }
        public static string ToOriginal(string thPath)
        {
 
            return thPath.Replace("_tn.jpg", ".jpg");
        }
 
 
    }
 
}

  

posted @   wgscd  阅读(58)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示