HTML JPG PDF XML JPG
Aspose.3D  for .NET
JPG

Create your Lithophane from JPG via C#

Build your own .NET apps to create your lithophane from JPG files using server-side APIs.

How to Create your Lithophane from JPG File Using C#

In order to create your lithophane from JPG file, we’ll use

Aspose.3D for .NET

API which is a feature-rich, powerful and easy to use API for C# platform to be used with create your lithophane. Open

NuGet

package manager, search for Aspose.3D and install. You may also use the following command from the Package Manager Console.

Package Manager Console Command


PM> Install-Package Aspose.3D

Steps to Create your Lithophane from JPG via C#

Aspose.3D makes it easy for the developers to create your lithophane from the JPG file with just few lines of code.

  • Create some new parameters and create a Mesh object
  • Perform computational operations on Mesh objects
  • JPG file loads 3d scene through Mesh class
  • Call the Scene.Save method with object

System Requirements

Aspose.3D for .NET is supported on all major operating systems. Just make sure that you have the following prerequisites.

  • Microsoft Windows or a compatible OS with .NET Framework, .NET Core, Mono
  • Development environment like Microsoft Visual Studio
  • Aspose.3D for .NET referenced in your project
 

C# code to Create your Lithophane from JPG


//The original image that needs to be uploaded and the 3d file output after saving
    string file = "template.jpg";
    string output =System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".fbx";

//Create some new parameters
    var td= TextureData.FromFile(file);
    const float nozzleSize = 0.9f;//0.2mm
    const float layerHeight = 0.2f;
    var grayscale = ToGrayscale(td);
    const float width = 120.0f;//canvas width is 200.0mm
    float height = width / td.Width * td.Height;
    float thickness = 10.0f;//10mm thickness
    float layers = thickness / layerHeight;
    int widthSegs = (int)Math.Floor(width / nozzleSize);
    int heightSegs = (int)Math.Floor(height / nozzleSize);

//Perform computational operations on Mesh objects
    var mesh = new 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 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
    var scene = new Scene(mesh);
    scene.Save(output, 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(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;
    }
 
  • About Aspose.3D for .NET API

    Aspose.3D is a CAD and Gameware API to load, modify and convert 3D files. API is a standalone and does not require any any 3D modeling or rendering software. One can easily use API for Discreet3DS, WavefrontOBJ, STL (ASCII, Binary), Universal3D, FBX (ASCII, Binary), Collada, glTF, PLY, GLB, DirectX and more formats.

    Free App to Create your Lithophane from JPG

    Check our live demos to Lithophane JPG with following benefits.

      No need to download or setup anything
      No need to write or compile code
      Just upload JPG file and hit the "lithophane" button
      Download JPG file from the link, if required

    Other Supported App to Create your Lithophane from Formats

    Using C#, One can also create your lithophane from many other file formats including.

    JPEG (Joint Photographic Experts Group)
    PNG (Portable Network Graphics)
    TGA (Truevision Advanced Raster Adapter)
    BMP (Bitmap)
    GIF (Graphical Interchange Format)
    TIFF (Tagged Image File Format)