รูปภาพ PNG เป็น Lithophane C #

รูปแบบ PNG เป็นที่นิยมเนื่องจากมีกราฟิกโปร่งใส ในขณะที่ lithophane เป็นงานศิลป์ที่สลักหรือขึ้นรูปด้วยวัสดุที่บางมาก ซึ่งคุณสามารถมองเห็นได้โดยวางแหล่งกำเนิดแสงไว้ด้านหลังโมเดลดังกล่าว บทความนี้ครอบคลุมถึงวิธีการแปลงภาพ PNG เป็น lithophane ใน C#

รูปภาพ PNG เป็น Lithophane Converter - การติดตั้ง C # API

Aspose.3D for .NET สามารถใช้ API เพื่อทำงานกับโมเดลและฉาก 3 มิติต่างๆ ได้ คุณสามารถกำหนดค่า API ได้ง่ายๆ โดยดาวน์โหลดไฟล์ DLL อ้างอิงจากหน้า New Releases หรือใช้คำสั่งติดตั้ง NuGet ต่อไปนี้:

PM> Install-Package Aspose.3D

แปลงภาพ PNG เป็น Lithophane ใน C

คุณสามารถแปลงไฟล์รูปภาพ PNG เป็นรูปแบบ Lithophane โดยทำตามขั้นตอนด้านล่าง:

  • โหลดภาพ PNG อินพุต
  • ดำเนินการคำนวณบนวัตถุตาข่าย
  • สร้างฉาก 3 มิติและบันทึกวัตถุด้วยวิธี บันทึก

ข้อมูลโค้ดด้านล่างอธิบายวิธีการแปลงภาพ PNG เป็น Lithophane โดยทางโปรแกรมใน C #:

string file = "template.png";
string output = "file.fbx";

// สร้างพารามิเตอร์ใหม่
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);

// ดำเนินการคำนวณบนวัตถุตาข่าย
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++;
    }
}

// สร้างฉาก 3 มิติและบันทึกวัตถุ
Aspose.ThreeD.Scene scene = new Aspose.ThreeD.Scene(mesh);
scene.Save(output, Aspose.ThreeD.FileFormat.FBX7400ASCII);

// ตัวอย่างวิธีโทร
static float Sample(float[,] data, int w, int h, float x, float y)
{
    return data[(int)(x * w), (int)(y * h)];
}

// เมธอด ToGrayscale เพื่อเรียก
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;
}

รับใบอนุญาตชั่วคราวฟรี

คุณสามารถขอ ใบอนุญาตชั่วคราวฟรี เพื่อประเมิน API ได้โดยไม่มีข้อจำกัดใดๆ

การสาธิตออนไลน์

โปรดลองใช้เว็บแอป PNG Image to Lithophane Converter ที่พัฒนาโดยใช้ API นี้

บทสรุป

ในบทความนี้ คุณได้สำรวจการแปลงรูปภาพ PNG เป็น lithophane นอกจากนี้ คุณสามารถดูพื้นที่ เอกสาร เพื่อเรียนรู้คุณสมบัติอื่นๆ ของ API ในกรณีที่คุณมีคำถามหรือข้อสงสัยใดๆ โปรดเขียนถึงเราที่ ฟอรัม

ดูสิ่งนี้ด้วย

แปลง USDZ เป็นไฟล์ GLB หรือ GLTF ใน C#