跨平台的 NodeJS 组件解决 .NetCore 不支持 System.Drawing图形功能的若干问题

时间:2022-10-26 12:06:30

问题

  • 生成缩略图
  • 生成验证码
  • 生成二维码
  • 给图片加水印

外部引用

  • Node  不解释  https://nodejs.org/en/download/
  • sharp 高性能缩略图  https://github.com/lovell/sharp
  • qr-image  二维码  https://github.com/alexeyten/qr-image
  • captchagen  验证码  https://github.com/contra/captchagen
  • node-images  轻量级跨平台图像编解码库 https://github.com/zhangyuanwei/node-images

生成缩略图代码
resizeImage.js

 var sharp = require('sharp');

 module.exports = function (result, physicalPath, mimeType, maxWidth, maxHeight) {
// Invoke the 'sharp' NPM module, and have it pipe the resulting image data back to .NET
sharp(physicalPath)
.resize(maxWidth || null, maxHeight || null)
.pipe(result.stream);
}

ResizeController.cs

 public class ResizeController : Controller
{
private const int MaxDimension = ;
private static string[] AllowedMimeTypes = new[] { "image/jpeg", "image/png", "image/gif" }; private IHostingEnvironment _environment;
private INodeServices _nodeServices; public ResizeController(IHostingEnvironment environment, INodeServices nodeServices)
{
_environment = environment;
_nodeServices = nodeServices;
} [Route("resize_{maxWidth}_{maxHeight}/{*imagePath}")]
public async Task<IActionResult> Index(string imagePath, int maxWidth, int maxHeight)
{
imagePath = imagePath;
// Validate incoming params
if (maxWidth < || maxHeight < || maxWidth > MaxDimension || maxHeight > MaxDimension
|| (maxWidth + maxHeight) == )
{
return BadRequest("Invalid dimensions");
} var mimeType = GetContentType(imagePath);
if (Array.IndexOf(AllowedMimeTypes, mimeType) < )
{
return BadRequest("Disallowed image format");
} // Locate source image on disk
var fileInfo = _environment.WebRootFileProvider.GetFileInfo(imagePath);
if (!fileInfo.Exists)
{
return NotFound();
} // Invoke Node and pipe the result to the response
var imageStream = await _nodeServices.InvokeAsync<Stream>(
"./Node/resizeImage",
fileInfo.PhysicalPath,
mimeType,
maxWidth,
maxHeight);
return File(imageStream, mimeType);
} private string GetContentType(string path)
{
string result;
return new FileExtensionContentTypeProvider().TryGetContentType(path, out result) ? result : null;
}
}

效果
跨平台的 NodeJS 组件解决 .NetCore 不支持 System.Drawing图形功能的若干问题
生成验证码代码
captchagen.js

 var captchagen = require('captchagen');
module.exports = function (result, width, height, text) {
// optional object arg with keys: height, width, text, font
var captcha = captchagen.create({ width: width, height: height, text: text||'8888'});
captcha.generate(); // Draws the image to the canvas
/* call `generate()` before running the below */
captcha.stream().pipe(result.stream); // outputs an image stream. type can be either png or jpeg (png is the default)
}

效果
跨平台的 NodeJS 组件解决 .NetCore 不支持 System.Drawing图形功能的若干问题

生成二维码代码

 var qr = require('qr-image');
module.exports = function (result, size, url) {
qr.image(url, { type: 'png', size: size, margin: 1 })
.pipe(result.stream);
}

效果
跨平台的 NodeJS 组件解决 .NetCore 不支持 System.Drawing图形功能的若干问题

总结

安装Node引用组件时费了不少时间主要是因为没细看作者给出的各种环境下的安装说明。

加水印目前还没做好,主要是要修改源码实现支持stream类型输出

抛砖引玉,希望更多朋友分享 Node各种组件的应用,

跨平台的 NodeJS 组件解决 .NetCore 不支持 System.Drawing图形功能的若干问题的更多相关文章

  1. &period;netcore中无法使用System&period;Drawing --解决方案

    问题重现: 无法正常使用  解决方法: 安装System.Drawing.Common的NuGet就能正常使用了 操作之后: 这个是.netcoe中的解决办法,.net framework解决方案中添 ...

  2. nodejs项目mysql使用sequelize支持存储emoji

    nodejs项目mysql使用sequelize支持存储emoji 本篇主要记录nodejs项目阿里云mysql如何支持存储emoji表情. 因由 最近项目遇到用户在文本输入emoji进行存储的时候导 ...

  3. jquery easyui-linkButton获取和设置按钮text并且解决火狐不支持innerText的方法

    <a href="javascript:test" id="btn" class="easyui-linkbutton" iconCl ...

  4. 解决ie6不支持position&colon; fixed&semi;导致无法滚动的办法

    <div id="im" style="top: 100px; position: fixed; left: 5px; border: 3px solid #006 ...

  5. 解决selenium不支持firefox低版本的问题

    解决selenium不支持firefox低版本的问题 在火狐浏览器升级后,突然发现webdriver运行脚本的时候不能调出火狐浏览器了,并报错WebDriverException:Message:'C ...

  6. 解决IE8不支持console

    解决IE8不支持console,代码中包含console时会报错. //解决 IE8 不支持console window.console = window.console || (function ( ...

  7. vue&plus;nodejs&plus;express解决跨域问题

    nodejs+express解决跨域问题,发现网上的大部分都是误导人,花了不少时间,终于弄懂了, 我在vue+nodejs+express+mongodb的项目里面,发现本地用vue代理正常调用远程的 ...

  8. 基于V2EX API的nodejs组件&period;

    今天又学习到了新的知(zi)识(shi),来给自己做个笔录,也算在这酷热的天气里给自己写了一篇降温的‘膏药’,话就讲这么多了 ,start off...... 首先 ,依赖选择: /**设置为严格模式 ...

  9. &lbrack;转&rsqb; NodeJS框架express的途径映射(路由)功能及控制

    NodeJS框架express的路径映射(路由)功能及控制 我们知道Express是一个基于NodeJS的非常优秀的服务端开发框架,本篇CSSer将提供express框架的route和route co ...

随机推荐

  1. bootstrap validate 实现页面动态验证(formvalidate)

    关于基本的bootstrap validate 验证方法外面有许多博客上都有讲解,我就不在过多叙述了.大家也可以去看官网api:http://bv.doc.javake.cn/api/ 今天要说的是动 ...

  2. 【零基础学习iOS开发】【转载】

    原文地址:http://www.cnblogs.com/mjios/archive/2013/04/24/3039357.html 本文目录 一.什么是iOS 二.主流手机操作系统 三.什么是iOS开 ...

  3. 谷歌和HTTPS

    谷歌和HTTPS HTTPS被觉得是加强互联网安全的次要部分,而且使用广泛.google近来做了一份关于数据加密近况的陈述. 正在陈述的最新部分中,提到了google以及第三方构造对于数据加密所做的贡 ...

  4. eclipse安装maven插件

  5. js实现是倒计时功能

    工作中经常用到倒计时的功能,最近在整理之前做的项目的时候,发现自己写过一个倒计时的功能的效果,这里和大家分享下!实现这个功能是用原生js写的,不需要加载额外的库文件!功能比较简单,但是可以在此基础上扩 ...

  6. Oracle和Mysql分别生成sequence序列

    有时候在往数据库中插入数据的时候,如果ID值是32位的UUID, 而自己随便写个字符又不合适,这时就要用到函数来产生一个序列值 Oracle: select sys_guid() from dual; ...

  7. Apache Tomcat部署java web项目

    本教程安装环境为windows服务器 在服务器中下载安装JDK JDK下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8 ...

  8. install&period;php文件建表时设置编码方式

    DROP TABLE IF EXISTS `".$db_prefix."adminlog`;CREATE TABLE `".$db_prefix."adminl ...

  9. web网络协议

    一.OSI七层模型   OSI参考模型是国际标准化组织ISO(International Standards Organization )制定的模型,把计算机与计算机之间的通信分成七个互相连接的协议层 ...

  10. bootstraptable 分页查询

    1.前端配置 2.后台输出格式化数据 1.前端配置 @{ Layout = null; } <!DOCTYPE html> <html> <head> <me ...