夏普手机官网(夏普手机官网在哪里)

↓推荐关注↓

前言

ImageSharp是对.NET Core平台扩展的一个图像处理方案,以往网上的案例多以生成文字及画出简单图形、验证码等方式进行探讨和实践。

分享一下所在公司项目的实际应用案例,导出微信二维码图片圆形头像等等。

一、源码获取

Git项目地址:https://github.com/SixLabors/ImageSharp

安装这两个包即可:

Install-Package SixLabors.ImageSharp -Version 1.0.0-beta0001

Install-Package SixLabors.ImageSharp.Drawing -Version 1.0.0-beta0001

二、应用

1、在图片中画出文字

首先要注意字体问题,Windows自带的字体一般存储于 C:\Windows\Fonts 文件夹内,如果是部署在Linux系统的应用程序,则存储于 usr/share/fonts 文件夹内。

以黑体为例,我们找到对应的字体文件 SIMHEI.TTF ,将其放入项目的根目录内方便调用。

varpath = “Image/Mud.png”//图片路径

FontCollection fonts = newFontCollection;

FontFamily fontfamily = fonts.Install( “Source/SIMHEI.TTF”); //字体的路径 var font = new Font(fontfamily,50);

using(ImageRgba32 image = Image.Load(path))

{

image.Mutate(x = x.DrawText (

“陆家嘴旗舰店”, //文字内容

font,

Rgba32.Black, //文字颜色

newPointF( 100, 100)) //坐标位置(浮点)

);

image.Save(path);

}

关于Image.Load获取图片方法的使用,可以直接读取Stream类型的流,也可以根据图片的本地路径获取。

//线上地址的图片,通过获取流的方式读取

WebRequest imgRequest = WebRequest.Create(url);

varres = (HttpWebResponse)imgRequest.GetResponse;

varimage = Image.Load(res.GetResponseStream);

夏普手机官网(夏普手机官网在哪里)

获取文字的像素宽度,可以使用:

varstr = “我是什么长度”;

varsize = TextMeasurer.Measure(str, newRendererOptions( newFont(fontfamily, 50)));

varwidth = size.Width;

2、在图片中画出圆形的头像

在ImageSharp的源码中,发现有画圆形的工具类可以使用,在这里直接copy出来。

usingSixLabors.ImageSharp;

usingSixLabors.ImageSharp.PixelFormats;

usingSixLabors.ImageSharp.Processing;

usingSixLabors.Primitives;

usingSixLabors.Shapes;

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

namespaceCodePicDownload

{

publicstaticclassCupCircularHelper

{

publicstaticIImageProcessingContextRgba32 ConvertToAvatar( thisIImageProcessingContextRgba32 processingContext, Size size, floatcornerRadius )

{

returnprocessingContext.Resize( newResizeOptions

{

Size = size,

Mode = ResizeMode.Crop

}).Apply(i = ApplyRoundedCorners(i, cornerRadius));

}

// This method can be seen as an inline implementation of an `IImageProcessor`:

// (The combination of `IImageOperations.Apply` + this could be replaced with an `IImageProcessor`)

privatestaticvoidApplyRoundedCorners( ImageRgba32 img, floatcornerRadius )

{

IPathCollection corners = BuildCorners(img.Width, img.Height, cornerRadius);

vargraphicOptions = newGraphicsOptions( true)

夏普手机官网(夏普手机官网在哪里)

{

AlphaCompositionMode = PixelAlphaCompositionMode.DestOut // enforces that any part of this shape that has color is punched out of the background

};

// mutating in here as we already have a cloned original

// use any color (not Transparent), so the corners will be clipped

img.Mutate(x = x.Fill(graphicOptions, Rgba32.LimeGreen, corners));

}

privatestaticIPathCollection BuildCorners( intimageWidth, intimageHeight, floatcornerRadius )

{

// first create a square

varrect = newRectangularPolygon( -0.5f, -0.5f, cornerRadius, cornerRadius);

// then cut out of the square a circle so we are left with a corner

IPath cornerTopLeft = rect.Clip( newEllipsePolygon(cornerRadius – 0.5f, cornerRadius – 0.5f, cornerRadius));

// corner is now a corner shape positions top left

//lets make 3 more positioned correctly, we can do that by translating the orgional artound the center of the image

floatrightPos = imageWidth – cornerTopLeft.Bounds.Width + 1;

floatbottomPos = imageHeight – cornerTopLeft.Bounds.Height + 1;

// move it across the width of the image – the width of the shape

IPath cornerTopRight = cornerTopLeft.RotateDegree( 90).Translate(rightPos, 0);

IPath cornerBottomLeft = cornerTopLeft.RotateDegree( -90).Translate( 0, bottomPos);

IPath cornerBottomRight = cornerTopLeft.RotateDegree( 180).Translate(rightPos, bottomPos);

returnnewPathCollection(cornerTopLeft, cornerBottomLeft, cornerTopRight, cornerBottomRight);

}

}

}

有了画圆形的方法,我们只需要调用ConvertToAvatar 方法把方形的图片转为圆形,画在图片上即可。

using(ImageRgba32 image = Image.Load( “Image/Mud.png”))

{

varlogoWidth = 300;

varlogo = Image.Load( “Image/Logo.png”) 5logo.Mutate(x = x.ConvertToAvatar( newSize(logoWidth, logoWidth), logoWidth / 2));

image.Mutate(x = x.DrawImage(logo, newPoint( 100, 100), 1));

Image.Save( “..”);

}

3、处理二维码的BitMatrix类型

以微信获取的二维码类型为例,因为我的项目中二维码是从微信公众号平台API获取,在这次获取图片中,将BitMatrix类型转换为流的格式从而可以通过Image.Load方法获取图片信息成为了关键。

在这里我还是引用到了System.Drawing,可以单独提取公用方法。

publicvoidWriteToStream( BitMatrix QrMatrix, ImageFormat imageFormat, Stream stream)

{

if(imageFormat != ImageFormat.ExifimageFormat != ImageFormat.IconimageFormat != ImageFormat.MemoryBmp)

{

DrawingSize size = m_iSize.GetSize(QrMatrix?.Width ?? 21);

using(Bitmap bitmap = newBitmap(size.CodeWidth, size.CodeWidth))

{

using(Graphics graphics = Graphics.FromImage(bitmap))

{

Draw(graphics, QrMatrix);

bitmap.Save(stream, imageFormat);

}

}

}

}

这样数据就存入了stream中,但直接用ImageSharp去Load处理过的流可能会有些问题,为了保险,我将数据流中的byte取出,实例化了一个新的MemoryStream类型。

这样,就可以获取到二维码的图片了。

//Matrix为BitMatrix类型数据,ImageFormat我选择了png类型

MemoryStream ms = newMemoryStream;

WriteToStream(Matrix,System.Drawing.Imaging.ImageFormat.Png, ms);

byte[] data = newbyte[ms.Length];

ms.Seek( 0, SeekOrigin.Begin);

ms.Read(data, 0, Convert.ToInt32(ms.Length));

varimage = Image.Load( newMemoryStream(data));

最后附上保存后图片的效果:

转自:走泥丸

– EOF –

点击标题可跳转

.NET 6 接入Skywalking链路追踪完整流程

五分钟 WPF 开发-心电图曲线绘制

.NET 中使用 Flurl 高效处理Http请求

看完本文有收获?请转发分享给更多人

推荐关注「DotNet」,提升.Net技能

点赞和在看就是最大的支持❤️

郑重声明:本文内容及图片均整理自互联网,不代表本站立场,版权归原作者所有,如有侵权请联系管理员(admin#loooy.com)删除。
(0)
上一篇 2022年10月15日 18:23
下一篇 2022年10月15日 18:25

相关推荐

  • 怎么让图片变小(4个方法调整图片大小)

    大家有没有发现,手机里的图片占用了许多内存,不仅在分享的时候受限制,下载图片的朋友也需要加载很久。   所以今天想和大家分享4个方法,让你轻松调整图片的大小! 图片格式更改   你…

    2023年4月26日
  • 手机绘画笔能用什么代替(手机绘画)

    适合手机上画画的软件1画世界支持用户进行绘画交流,多人同时互画,一起比较谁画得更好各种仿生画笔更加贴近手绘的感觉,玩家可以将画笔调整为适合自己的宽度和透明度,拿出手机就可以进行画画…

    2023年4月9日
  • 眼睛手机(眼睛手机壁纸)

    都知道高亮度对人类眼睛的刺激是很大的,所以在使用手机的时候可以将手机亮度调到适中,让眼睛能更舒适的适应手机亮度,这样既可以看清图片文字也可以适当的保护眼睛,避免光伤害长时间用手机,…

    2022年11月14日
  • 魅蓝note6配置参数(分解魅蓝Note6上手体验)

    上一篇通过外观和简单的使用告诉大家我为什么买魅蓝note6,通过这一段时间的深入使用,这一篇我和大家聊聊魅蓝note6是值得拥有的。 一、外在篇 简单说说魅蓝note6的参数,让大…

    2023年5月24日
  • 手机怎么删除(手机怎么删除多余的页面)

    1在手机桌面,打开系统设置2选择设置页面的应用管理3点击所有应用4选择需要卸载的应用5点击卸载6卸载完成后,在手机桌面上即可彻底删除该程序图标;1长按要卸载的应用程序,点击卸载2点…

    2022年11月7日
  • 手机棋牌游戏平台

    1、手机棋牌游戏的发展趋势一地方棋牌竞争激烈 全国性棋牌平台将加入更多地方棋牌,目前腾讯游戏APP已经推出了多款。 2、小强热线有一位杨先生打进我们的热线,说他最近在手机的一款棋牌…

    2022年12月19日
  • 最好的手机(最好的手机处理器排名)

    1、推荐一款华为 Mate 40很不错的,手机参数如下1屏幕屏幕尺寸为65英寸,屏幕色彩1670万色,DCIP3广色域,分辨率FHD+ 2376 × 1080 像素,采用68#18…

    2022年11月14日
  • 液晶电视和显示器的区别(液晶电视和显示器区别是什么)

    液晶电视与液晶显示器虽然同为输出显示设备,核心部件同样均为液晶面板,接口配置、外观设计上也颇有相通之处,但在市场上这两类产品却绝对是径渭分明,互不相干。 首先,在渠道上,液晶电视大…

    2023年4月25日
  • 苹果手机怎么消除浏览过页面(苹果删除浏览过的页面记录方法)

    苹果如何删除浏览过的页面,具体步骤如下所示: 品牌型号:iPhone13 系统版本:iOS 15.3 软件版本:设置 12.0 方法/步骤 1/3 分步阅读 点开浏览器设置 进入苹…

    2023年4月25日
  • 小米11使用后的缺点(小米11的使用体验)

    创作立场声明:各位值友好,作为一个新人,这是我在值得买平台发布的第二篇文章,这还不是因为之前发的那篇竟然获得了不少网友的评论,新人备受鼓舞,极大的激发了我的创作热情,今天来谈谈小米…

    2023年6月19日