1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

.NETCoreで画像をピクセル単位で扱う(SixLabors.ImageSharp)

Last updated at Posted at 2020-12-06

.NETCoreで画像編集するときの定番ImageSharp、たまにしか使わないせいか、使い方を忘れていることがしばしばあるので備忘録として。
公式リポジトリ:GitHub: SixLabors/ImageSharp
動作確認:Version 1.0.2

1. ImageSharpの導入

nugetを使って導入します。WindowsでVisual Studio使っているならGUIで特に困らず導入できますが、
Linuxで.NETCoreで開発するときに困るのでコマンドラインも書いておきます。

dotnet add package SixLabors.ImageSharp

2. サンプルコード

2.1 画像の読み込み

C#
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;

string path = "filepath.jpg"
Image<Rgba32> img = Image.Load<Rgba32>(path);

2.2 画像のサイズを変更

C#
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors.Transforms;

img.Mutate(x =>
  x.Resize(targetSize.Width, targetSize.Height, new BicubicResampler(), false)
);

2.3 (複数処理)画像のサイズを変更してから画像を水平反転する

C#
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors.Transforms;

img.Mutate(x =>
  x.Resize(targetSize.Width, targetSize.Height, new BicubicResampler(), false).Flip(FlipMode.Horizontal)
);

2.4 ピクセル単位でアクセス

C#
using SixLabors.ImageSharp.PixelFormats;

Rgba32 px = img[x, y]
byte r = img[x, y].R
byte g = img[x, y].G
byte b = img[x, y].B
byte a = img[x, y].A

get / set 両方可能

3. 最後に

昔はこのライブラリを使ってピクセル単位のデータアクセスとか苦労しましたが、今はかなり楽になりました。
ライセンスもApache2.0ですし、色々な意味で使いやすいライブラリだと思います。
長らくalpha版として公開されていたこのライブラリ、リリースのたびに破壊的変更が入っている印象があります。
今後も破壊的変更がないという保証は無いので、使うときはバージョンに注意した方が良いかも知れないですね。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?