HcfNS.Drawing
7.0.2.1
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package HcfNS.Drawing --version 7.0.2.1
NuGet\Install-Package HcfNS.Drawing -Version 7.0.2.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HcfNS.Drawing" Version="7.0.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HcfNS.Drawing --version 7.0.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: HcfNS.Drawing, 7.0.2.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install HcfNS.Drawing as a Cake Addin #addin nuget:?package=HcfNS.Drawing&version=7.0.2.1 // Install HcfNS.Drawing as a Cake Tool #tool nuget:?package=HcfNS.Drawing&version=7.0.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Hcf Drawing 图像处理组件,封装Windows下基于System.Drawing.Common,Linux下基于SkiaSharp的图像封装实现
用法
通过nuget引用 Hcf.Drawing
程序集
Install-Package HcfNS.Drawing
使用DrawingPack包
services.AddPack<DrawingPack>();
示例
获取IImageHandle实例
var imageHandle = provider.GetService<IImageHandle>();
创建缩略图
/// <summary>
/// 创建缩略图
/// </summary>
/// <param name="oriFileName">原始文件名称</param>
/// <param name="targetFileName">目标文件名称</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
void CreateThumbnail(string oriFileName, string targetFileName, int width, int height);
/// <summary>
/// 创建缩略图
/// </summary>
/// <param name="stream">原始文件流</param>
/// <param name="targetFileName">目标文件名称</param>
/// <param name="height">高度</param>
/// <param name="width">宽度</param>
void CreateThumbnail(FileStream stream, string targetFileName, int width, int height);
获取图片尺寸
/// <summary>
/// 获取文件尺寸
/// </summary>
/// <param name="fileName">文件名称</param>
/// <returns></returns>
Tuple<int, int> GetImageSize(string fileName);
/// <summary>
/// 获取文件尺寸
/// </summary>
/// <param name="stream">文件流</param>
/// <returns></returns>
Tuple<int, int> GetImageSize(FileStream stream);
保存图片
/// <summary>
/// 保存图片
/// </summary>
/// <param name="bytes">图片字节</param>
/// <param name="fileName">保存地址</param>
/// <param name="type">文件类型</param>
/// <param name="quality">图片质量,0~100</param>
void Save(byte[] bytes, string fileName, ImageTypeFormat type, int quality = 100);
/// <summary>
/// 保存图片
/// </summary>
/// <param name="stream">图片流</param>
/// <param name="fileName">保存地址</param>
/// <param name="type">文件类型</param>
/// <param name="quality">图片质量,0~100</param>
void Save(Stream stream, string fileName, ImageTypeFormat type, int quality = 100);
扩展
Bitmap扩展, 只支持Windows平台
/// <summary>
/// 将Bitmap转换为Byte[]
/// </summary>
/// <param name="bmp">待处理的图像</param>
/// <returns></returns>
public static byte[] ToBytes(this Bitmap bmp)
/// <summary>
/// 将Bitmap转换为Base64字符串
/// </summary>
/// <param name="bmp">待处理的图像</param>
/// <returns></returns>
public static string ToBase64(this Bitmap bmp)
/// <summary>
/// 按指定百分比缩放图像
/// </summary>
/// <param name="bmp">待处理的图像</param>
/// <param name="percent">缩放百分比(小数)</param>
/// <param name="model">图像质量模式</param>
/// <returns> 缩放后的图像 </returns>
public static Bitmap Zoom(this Bitmap bmp, double percent, InterpolationMode model = InterpolationMode.Default)
验证码图片
Window平台
public IActionResult GetValidateCode()
{
ValidateCoder validateCoder = new ValidateCoder();
validateCoder.RandomColor = true;
validateCoder.RandomItalic = true;
validateCoder.RandomLineCount = 4;
validateCoder.RandomPosition = true;
string code = validateCoder.GetCode(4);
Bitmap bitmap = validateCoder.CreateImage(code, ValidateCodeType.NumberAndLetter);
HttpContext.Session.SetString("ValidateCode", code);
return File(bitmap.ToBytes(), "image/jpg");
}
Windows、Linux平台
public IActionResult GetValidateCode()
{
var validateCodeProvider = new SkiaSharpValidateCoder();
validateCodeProvider.SetWith = 126;
validateCodeProvider.SetHeight = 38;
validateCodeProvider.SetFontSize = 20;
var bytes = validateCodeProvider.GetVerifyCodeImage();
var code = validateCodeProvider.SetVerifyCodeText;
HttpContext.Session.SetString("ValidateCode", code);
return File(bytes, "image/jpg");
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- HcfNS.Core (>= 7.0.2)
- SkiaSharp (>= 3.116.1)
- SkiaSharp.NativeAssets.Linux.NoDependencies (>= 3.116.1)
- System.Drawing.Common (>= 6.0.0)
-
net7.0
- HcfNS.Core (>= 7.0.2)
- SkiaSharp (>= 3.116.1)
- SkiaSharp.NativeAssets.Linux.NoDependencies (>= 3.116.1)
- System.Drawing.Common (>= 7.0.0)
-
net8.0
- HcfNS.Core (>= 7.0.2)
- SkiaSharp (>= 3.116.1)
- SkiaSharp.NativeAssets.Linux.NoDependencies (>= 3.116.1)
- System.Drawing.Common (>= 8.0.11)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.