千家信息网

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

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章给大家介绍跨平台的NodeJS 组件如何解决.NetCore不支持System.Drawing图形功能的若干问题,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。问题生成缩略
千家信息网最后更新 2026年11月03日跨平台的NodeJS 组件如何解决.NetCore不支持System.Drawing图形功能的若干问题

这篇文章给大家介绍跨平台的NodeJS 组件如何解决.NetCore不支持System.Drawing图形功能的若干问题,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

问题

  • 生成缩略图

  • 生成验证码

  • 生成二维码

  • 给图片加水印

外部引用

  • 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 = 1000;

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 Index(string imagePath, int maxWidth, int maxHeight)

{

imagePath = imagePath;

// Validate incoming params

if (maxWidth < 0 || maxHeight < 0 || maxWidth > MaxDimension || maxHeight > MaxDimension

|| (maxWidth + maxHeight) == 0)

{

return BadRequest("Invalid dimensions");

}

var mimeType = GetContentType(imagePath);

if (Array.IndexOf(AllowedMimeTypes, mimeType) < 0)

{

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(

"./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;

}

}

效果

生成验证码代码
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)

}

效果


生成二维码代码

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

效果


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

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

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

关于跨平台的NodeJS 组件如何解决.NetCore不支持System.Drawing图形功能的若干问题就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

生成 组件 问题 支持 代码 效果 更多 二维 二维码 验证 功能 图形 内容 水印 帮助 不错 抛砖引玉 作者 兴趣 图像 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 致骋互联网科技 善班互联网科技有限公司 易通互联网络科技有限公司 长亮科技互联网金融 互联网金融和金融科技 互联网 科技服务平台 哈尔滨新华互联网科技 济南和联互联网科技有限公司 以后互联网后是啥科技 赶超互联网科技 微信多开 米聚互联网科技南京有限公司排面 华夏恒生科技与互联网科技 佛山市拉法互联网科技有限公司 为客找房互联网科技公司怎么样 互联网硬科技公司 互联网的科技体现在哪里 青岛易易互联网科技 保山浩联互联网科技有限公司 河南商务圈互联网科技有限公司 互联网的黑科技实验室 花魁咖啡 互联网新科技 深圳互联网科技怎么样 天津腾达互联网科技有限公司 佛山市翊博互联网络科技有限公司 四川慕妮娅互联网科技有限公司 怎样加入科技互联网 广东鹅鹅鹅互联网科技有限公司 互联网硬科技公司 上海同邻互联网科技有限公司规模
0