HcfNet.Drawing 1.0.1

dotnet add package HcfNet.Drawing --version 1.0.1
                    
NuGet\Install-Package HcfNet.Drawing -Version 1.0.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="HcfNet.Drawing" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HcfNet.Drawing" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="HcfNet.Drawing" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add HcfNet.Drawing --version 1.0.1
                    
#r "nuget: HcfNet.Drawing, 1.0.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.
#:package HcfNet.Drawing@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=HcfNet.Drawing&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=HcfNet.Drawing&version=1.0.1
                    
Install as a Cake Tool

Hcf Drawing 图像处理组件,封装Windows下基于System.Drawing.Common,Linux下基于SkiaSharp的图像封装实现

用法

通过nuget引用 Hcf.Drawing 程序集

Install-Package HcfNet.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 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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on HcfNet.Drawing:

Package Downloads
SiFang.Edge

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 382 1/12/2025