
PNG format is popular as they can include transparent graphics. Whereas, lithophane is an etched or molded artwork in very thin material which you can see by putting the light source behind such model. This article covers how to convert a PNG image to lithophane in C#.
PNG Image to Lithophane Converter – C# API Installation
Aspose.3D for .NET API can be used to work with different 3D models and scenes. You can easily configure the API by downloading its reference DLL files from the New Releases page or using the following NuGet installation command:
PM> Install-Package Aspose.3D
Convert PNG Image to Lithophane in C#
You can convert a PNG Image file to Lithophane format by following the steps below:
- Load the input PNG image.
- Perform computational operations on Mesh objects.
- Generate 3d scene and save objects with the Save method.
The code snippet below explains how to convert a PNG Image to Lithophane programmatically in C#:
string file = "template.png"; | |
string output = "file.fbx"; | |
// Create some new parameters | |
Aspose.ThreeD.Render.TextureData td = Aspose.ThreeD.Render.TextureData.FromFile(file); | |
const float nozzleSize = 0.9f; | |
const float layerHeight = 0.2f; | |
var grayscale = ToGrayscale(td); | |
const float width = 120.0f; | |
float height = width / td.Width * td.Height; | |
float thickness = 10.0f; | |
float layers = thickness / layerHeight; | |
int widthSegs = (int)Math.Floor(width / nozzleSize); | |
int heightSegs = (int)Math.Floor(height / nozzleSize); | |
// Perform computational operations on Mesh objects | |
Aspose.ThreeD.Entities.Mesh mesh = new Aspose.ThreeD.Entities.Mesh(); | |
for (int y = 0; y < heightSegs; y++) | |
{ | |
float dy = (float)y / heightSegs; | |
for (int x = 0; x < widthSegs; x++) | |
{ | |
float dx = (float)x / widthSegs; | |
float gray = Sample(grayscale, td.Width, td.Height, dx, dy); | |
float v = (1 - gray) * thickness; | |
mesh.ControlPoints.Add(new Aspose.ThreeD.Utilities.Vector4(dx * width, dy * height, v)); | |
} | |
} | |
for (int y = 0; y < heightSegs - 1; y++) | |
{ | |
int row = (y * heightSegs); | |
int ptr = row; | |
for (int x = 0; x < widthSegs - 1; x++) | |
{ | |
mesh.CreatePolygon(ptr, ptr + widthSegs, ptr + 1); | |
mesh.CreatePolygon(ptr + 1, ptr + widthSegs, ptr + widthSegs + 1); | |
ptr++; | |
} | |
} | |
// Generate 3d scene and save objects | |
Aspose.ThreeD.Scene scene = new Aspose.ThreeD.Scene(mesh); | |
scene.Save(output, Aspose.ThreeD.FileFormat.FBX7400ASCII); | |
// The sample method to call | |
static float Sample(float[,] data, int w, int h, float x, float y) | |
{ | |
return data[(int)(x * w), (int)(y * h)]; | |
} | |
// ToGrayscale method to call | |
static float[,] ToGrayscale(Aspose.ThreeD.Render.TextureData td) | |
{ | |
var ret = new float[td.Width, td.Height]; | |
var stride = td.Stride; | |
var data = td.Data; | |
var bytesPerPixel = td.BytesPerPixel; | |
for (int y = 0; y < td.Height; y++) | |
{ | |
int ptr = y * stride; | |
for (int x = 0; x < td.Width; x++) | |
{ | |
var v = (data[ptr] * 0.21f + data[ptr + 1] * 0.72f + data[ptr + 2] * 0.07f) / 255.0f; | |
ret[x, y] = v; | |
ptr += bytesPerPixel; | |
} | |
} | |
return ret; | |
} |
Get Free Temporary License
You may request a free temporary license to evaluate the API without any limitations.
Online Demo
Please try the PNG Image to Lithophane Converter web app developed using this API.
Conclusion
In this article, you have explored the conversion of PNG Image to lithophane. Moreover, you may take a look at the documentation space to learn other features of the API. In case you have any questions or queries, please write to us at the forum.