zoukankan      html  css  js  c++  java
  • c# WPF convert photo to Sketch effects

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

    <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:Usersgwangsource
    eposWpfMosaicinDebug12.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 = "
    ";
    
                    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");
            }
    
    
        }
    
    }
    

      

    fffffffffffffffff
    test red font.
  • 相关阅读:
    JQ插件
    jQuery radio的取值与赋值
    Js获取当前日期时间
    jquery 整理之一
    2014-9月收集整理之二(原生)
    Codeforces Round #279 (Div. 2) E-Restoring Increasing Sequence
    HDU-4431 麻将
    hdu-2222 AC自动机模板题
    Codeforces Round #460 (Div. 2) E. Congruence Equation
    Codeforces Round #459 (Div. 2) D. MADMAX
  • 原文地址:https://www.cnblogs.com/wgscd/p/15090209.html
Copyright © 2011-2022 走看看