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:\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");
} } }
c# WPF convert photo to Sketch effects的更多相关文章
- wpf convert png to xaml
原文:wpf convert png to xaml 把png图片转化成xaml资源 <ResourceDictionary xmlns="http://schemas.microso ...
- WPF Convert使用
在存在基本数据缓存时,可以传入一个数据库中的数据唯一标识码,然后利用自己编写的Convert类,这个Convert类必须实现IValueConverter接口,进行转换,在进行转换的过程中,可以从基本 ...
- sketch 相关论文
sketch 相关论文 Sketch Simplification We present a novel technique to simplify sketch drawings based on ...
- 【WPF/C#】联网异步获取二进制文件(如图片)的流程
步骤: 联网异步获取Json数据. 使用Json.NET工具,反序列化Json为对应的实体类,获得该实体类的对象. 从对象身上获取图片路径(实体类中定义了头像图片是string类型的文件路径). 根据 ...
- 【C#/WPF】Bitmap、BitmapImage、ImageSource 、byte[]转换问题
C#/WPF项目中,用到图像相关的功能时,涉及到多种图像数据类型的相互转换问题,这里做了个整理.包含的内容如下: Bitmap和BitmapImage相互转换. RenderTargetBitmap ...
- ImageMagick命令执行学习笔记(常见于图片预览处)
实验版本: ImageMagick版本:6.9.2 push graphic-context viewbox 0 0 640 480 fill 'url(https://"|whoami&q ...
- 浅谈WPF中对控件的位图特效(WPF Bitmap Effects)
原文:浅谈WPF中对控件的位图特效(WPF Bitmap Effects) -------------------------------------------------------------- ...
- wpf控件提示Value ‘’ can not convert
我们在对控件的ErrorTemplate设置后,有时会出现Value '' can not convert. 为什么会出现呢? 原因:如果控件的输入值和null不能转换(比如控件要求的是int或flo ...
- WPF MultiBinding后台绑定动态属性 属性改变不调用Convert的问题
一开始的写法: MultiBinding mb = new MultiBinding(); Binding b1 = new Binding(); b1.ElementName = "tex ...
- 准备.Net转前端开发-WPF界面框架那些事,值得珍藏的8个问题
题外话 不出意外,本片内容应该是最后一篇关于.Net技术的博客,做.Net的伙伴们忽喷忽喷..Net挺好的,微软最近在跨平台方面搞的水深火热,更新也比较频繁,而且博客园的很多大牛也写的有跨平台相关技术 ...
随机推荐
- SSIS错误汇总
问题一 [Excel 目标 [2]] 错误: SSIS 错误代码 DTS_E_CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER.对连接管理器"未匹配用 ...
- Quartz集群增强版_00.How to use?(如何使用)
Quartz集群增强版_00.How to use?(如何使用) 转载请著名出处 https://www.cnblogs.com/funnyzpc/p/18540378 开源地址 https://gi ...
- php字符串练习题
把以前发给别人的练习题邮件备份记录一下 1.用php编写统计二维数组内某个字符出现的次数的一个函数.给定二维数组和字符串,返回这个字符串在二维数组中出现的次数. 例: 数组如下: $array=arr ...
- SpringFlex框架搭建
或者参考http://limingnihao.iteye.com/blog/830409.使用Eclipse的Maven构建SpringMVC项目. 1.1 简单介绍 Spring是一个轻量级的控制反 ...
- 关于ClassLoader中getResource与getResourceAsStream的疑问
背景: 某日临近下班,一个同事欲任何类中获取项目绝对路径,不通过Request方式获取,可是始终获取不到预想的路径.于是晚上回家google了一下,误以为是System.getProperty(&qu ...
- golang之媒体处理
[视频] 获取视频封面图: 1) 如果是使用oss的话, 可以添加指定的后缀生成指定图片 视频截帧: https://help.aliyun.com/zh/oss/user-guide/video-s ...
- go get 和 go install 对比
(一)命令定义和区别 go install 和 go get 都是 Go 语言的工具命令,但它们之间有一些区别. go get:用于从远程代码存储库(如 GitHub)中下载或更新 Go 代码包.它会 ...
- Prometheus之系统安装,启动
Prometheus简介Prometheus是最初在SoundCloud上构建的开源系统监视和警报工具包. 自2012年成立以来,许多公司和组织都采用了Prometheus,该项目拥有非常活跃的开发人 ...
- 集成Nacos
版本统一:Spring Boot 2.6.11,Spring Cloud 2021.0.4,Spring Cloud Alibaba 2021.0.4.0: Maven 仓库记得配置中央仓库,私服上有 ...
- 优雅地打印 HEX 数据
转载至知乎Murphy https://zhuanlan.zhihu.com/p/320391096 前言 在调试的时候经常要打印内存里的数据,来看看数据及格式是否在预期范围内:以及在调试二进制协议的 ...