C#-使用StbSharp库读写图片
一.StbSharp
StbSharp是基于C++/Stb图形处理库封装的C#接口,支持多种格式PNG/JPG等图片的处理.
GitHub链接:
GitHub - StbSharp/StbTrueTypeSharp: C# port of stb_truetype.hhttps://github.com/StbSharp/StbTrueTypeSharp二.使用StbSharp创建高度图
创建一张500*500的高度图PNG图片
public static void GenerateHeightMapPNG()
{
Image image = new Image();
image.Width = 500;
image.Height = 500;
image.Comp = 4;
image.Data = new byte[image.Width * image.Height * image.Comp];
for (int y = 0; y < image.Height; y++)
{
for (int x = 0; x < image.Width; x++)
{
int index = (y * image.Width + x) * image.Comp;
if (x > 250 && y > 250)
{
image.Data[index] = 255;
image.Data[index + 1] = 0;
image.Data[index + 2] = 0;
image.Data[index + 3] = 255;
}
else
{
image.Data[index] = 255;
image.Data[index + 1] = 255;
image.Data[index + 2] = 255;
image.Data[index + 3] = 255;
}
}
}
FileInfo fileInfo = new FileInfo("test.png");
if (!fileInfo.Exists)
{
fileInfo.Create();
}
ImageWriter imageWriter = new ImageWriter();
imageWriter.WritePng(image, fileInfo.OpenWrite());
}