Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How do cross-platform NodeJS components solve some problems that .NetCore does not support System.Drawing graphics functions?

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces you how to solve the cross-platform NodeJS components. NetCore does not support System.Drawing graphics functions, the content is very detailed, interested friends can refer to, hope to be helpful to you.

problem

Generate thumbnails

Generate CAPTCHA

Generate a QR code

Watermark a picture

External reference

Node does not interpret https://nodejs.org/en/download/

Sharp High performance Thumbnail https://github.com/lovell/sharp

Qr-image QR code https://github.com/alexeyten/qr-image

Captchagen CAPTCHA https://github.com/contra/captchagen

Node-images lightweight cross-platform image coding and decoding library https://github.com/zhangyuanwei/node-images

Generate thumbnail code

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

}

}

Effect.

Generate CAPTCHA code

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)

}

Effect.

Generate QR code

1 var qr = require ('qr-image')

2 module.exports = function (result, size, url) {

3 qr.image (url, {type: 'png', size: size, margin: 1})

Pipe (result.stream)

5}

Effect.

It took a lot of time to install the Node reference components mainly because the author did not take a closer look at the installation instructions in various environments.

Watermarking has not been done yet, mainly to modify the source code to support stream type output.

Throw a brick to attract jade, hope more friends to share the application of various components of Node.

On the cross-platform NodeJS components how to solve. NetCore does not support System.Drawing graphics functions to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report