132 lines
4.7 KiB
C#
132 lines
4.7 KiB
C#
namespace Zenith
|
||
{
|
||
using System;
|
||
using System.IO;
|
||
using SixLabors.ImageSharp;
|
||
using SixLabors.ImageSharp.Metadata;
|
||
using UnityEngine;
|
||
|
||
public static class ImageDpiProcessor
|
||
{
|
||
/// <summary>
|
||
/// 修改现有图片文件的DPI
|
||
/// </summary>
|
||
/// <param name="inputPath">原始图片路径</param>
|
||
/// <param name="outputPath">输出图片路径(可与输入路径相同,覆盖原文件)</param>
|
||
/// <param name="dpi">目标DPI值(水平和垂直方向将使用相同值)</param>
|
||
/// <returns>是否成功</returns>
|
||
public static bool ModifyImageDpi(string inputPath, string outputPath, int dpi)
|
||
{
|
||
try
|
||
{
|
||
// 验证输入文件
|
||
if (!File.Exists(inputPath))
|
||
{
|
||
Debug.LogError($"文件不存在: {inputPath}");
|
||
return false;
|
||
}
|
||
|
||
// 确保DPI值有效
|
||
if (dpi <= 0)
|
||
{
|
||
Debug.LogError("DPI值必须大于0");
|
||
return false;
|
||
}
|
||
|
||
// 加载图片并修改DPI
|
||
using (var image = Image.Load(inputPath))
|
||
{
|
||
// 设置分辨率单位为英寸(DPI的标准单位)
|
||
image.Metadata.ResolutionUnits = PixelResolutionUnit.PixelsPerInch;
|
||
|
||
// 设置水平和垂直DPI
|
||
image.Metadata.HorizontalResolution = dpi;
|
||
image.Metadata.VerticalResolution = dpi;
|
||
|
||
// 确保输出目录存在
|
||
string outputDirectory = Path.GetDirectoryName(outputPath);
|
||
if (!Directory.Exists(outputDirectory))
|
||
{
|
||
Directory.CreateDirectory(outputDirectory);
|
||
}
|
||
|
||
// 保存修改后的图片(自动保留原格式)
|
||
image.Save(outputPath);
|
||
}
|
||
|
||
Debug.Log($"成功将图片DPI修改为 {dpi}: {outputPath}");
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.LogError($"修改DPI失败: {ex.Message}");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将Unity的Texture2D保存为指定DPI的图片
|
||
/// </summary>
|
||
/// <param name="texture">Unity纹理</param>
|
||
/// <param name="savePath">保存路径(需包含文件名和扩展名,如.png)</param>
|
||
/// <param name="dpi">目标DPI值</param>
|
||
/// <returns>是否成功</returns>
|
||
public static bool SaveTextureWithDpi(Texture2D texture, string savePath, int dpi)
|
||
{
|
||
try
|
||
{
|
||
if (texture == null)
|
||
{
|
||
Debug.LogError("Texture2D不能为空");
|
||
return false;
|
||
}
|
||
|
||
// 从Texture2D获取图片字节数据(根据保存格式选择编码方式)
|
||
byte[] imageData;
|
||
string extension = Path.GetExtension(savePath).ToLower();
|
||
|
||
if (extension == ".png")
|
||
{
|
||
imageData = texture.EncodeToPNG();
|
||
}
|
||
else if (extension == ".jpg" || extension == ".jpeg")
|
||
{
|
||
imageData = texture.EncodeToJPG(90); // 90%质量
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("不支持的图片格式,仅支持PNG和JPG");
|
||
return false;
|
||
}
|
||
|
||
// 使用内存流处理图片数据
|
||
using (var memoryStream = new MemoryStream(imageData))
|
||
using (var image = Image.Load(memoryStream))
|
||
{
|
||
// 设置DPI元数据
|
||
image.Metadata.ResolutionUnits = PixelResolutionUnit.PixelsPerInch;
|
||
image.Metadata.HorizontalResolution = dpi;
|
||
image.Metadata.VerticalResolution = dpi;
|
||
|
||
// 确保保存目录存在
|
||
string directory = Path.GetDirectoryName(savePath);
|
||
if (!Directory.Exists(directory))
|
||
{
|
||
Directory.CreateDirectory(directory);
|
||
}
|
||
|
||
// 保存图片
|
||
image.Save(savePath);
|
||
}
|
||
|
||
Debug.Log($"Texture2D已保存为 {dpi} DPI: {savePath}");
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.LogError($"保存带DPI的图片失败: {ex.Message}");
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
} |