最近有PS测评的需求,故而想到了解析psd文件,目的就是为了获取文档信息和图层信息;获取PS的图像信息有很多方式,有过程性的,比如监听PS的各种操作事件;有结果性的,比如本文写的解析PSD文件。
0.添加Photoshop 引用
在解决方案邮件点击添加>>引用,如下图:
在引用管理器中,点击COM,找到Adobe Photoshop XXX ,勾上,点击确定。
1.在vb文件添加导入Photoshop
2.解析psd
话不多说,直接上代码:
Imports Photoshop
Imports System.IOPublic Class Form1Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.LoadDim filePath As String = "D:\PhotoshopCS6操作题1\1\PS1.psd"parsePsd(filePath)End SubPrivate Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickConsole.WriteLine("ssss")Dim filePath As String = "D:\PhotoshopCS6操作题1\1\PS1.psd"parsePsd(filePath)End SubPrivate Sub parsePsd(filePath As String)Dim fileInfo As New FileInfo(filePath)Dim fileSizeInBytes As Long = fileInfo.LengthDim fileSizeInMB As Double = fileSizeInBytes / (1024 * 1024)If fileSizeInMB > 5 ThenConsole.WriteLine("文档大小[{0}MB]超过5MB", fileSizeInMB)ElseConsole.WriteLine("文档大小[{0}MB]符合要求", fileSizeInMB, fileSizeInBytes)End If' 创建Photoshop应用程序对象Dim app As New Photoshop.Application()' 打开PSD文件Dim doc As Photoshop.Document = app.Open(filePath)TryConsole.WriteLine("名称: " & doc.Name)Console.WriteLine("文档尺寸宽: " & doc.Width)Console.WriteLine("文档尺寸高: " & doc.Height)Console.WriteLine("颜色模式: " & doc.Mode.ToString)' 获取图像分辨率Dim resolution As Double = doc.ResolutionConsole.WriteLine("图像分辨率:{0} dpi", resolution)' 将宽度和高度转换为像素Dim widthInPixels As Integer = CInt((doc.Width * resolution) / 2.54)Dim heightInPixels As Integer = CInt((doc.Height * resolution) / 2.54)Console.WriteLine("图像宽度:{0} 像素", widthInPixels)Console.WriteLine("图像高度:{0} 像素", heightInPixels)' 遍历图层For Each layer As Object In doc.LayersConsole.WriteLine("0图层名称: " & layer.Name)Console.WriteLine("0图层LayerType: " & layer.Kind.ToString)Console.WriteLine("0图层Opacity: " & layer.Opacity)Console.WriteLine("0图层IsBackgroundLayer: " & layer.IsBackgroundLayer)If layer.Kind = PsLayerKind.psTextLayer ThenConsole.WriteLine("0图层文字内容: " & layer.TextItem.Contents)Console.WriteLine("0图层文字字体size: " & layer.TextItem.Size)Console.WriteLine("0图层文字字体: " & layer.TextItem.Font)' 获取文字颜色Dim textColor As SolidColor = layer.TextItem.Color' 获取颜色的分量值Dim red As Integer = textColor.RGB.RedDim green As Integer = textColor.RGB.GreenDim blue As Integer = textColor.RGB.BlueConsole.WriteLine("HexValue: " & textColor.RGB.HexValue)Console.WriteLine("文字颜色:R={0}, G={1}, B={2}", red, green, blue)End IfIf layer.IsBackgroundLayer = True ThenConsole.WriteLine("背景:PixelsLocked={0}, IsBackgroundLayer={1}, Visible={2}", layer.PixelsLocked, layer.IsBackgroundLayer, layer.Visible)' 检查图层是否有蒙版'If layer.HasLayerMask Then' 获取蒙版'Dim layerMask As Object = layer.LayerMask'Console.WriteLine("layerMask:" & layerMask.ToString)'End IfEnd IfNextConsole.WriteLine("========================================")For Each layer As ArtLayer In doc.ArtLayersConsole.WriteLine("图层名称: " & layer.Name)Console.WriteLine("图层LayerType: " & layer.Kind.ToString)Console.WriteLine("图层Opacity: " & layer.Opacity)Console.WriteLine("图层IsBackgroundLayer: " & layer.IsBackgroundLayer)If layer.Kind = PsLayerKind.psTextLayer ThenConsole.WriteLine("图层文字内容: " & layer.TextItem.Contents)Console.WriteLine("图层文字字体: " & layer.TextItem.Font)Console.WriteLine("图层文字字体size: " & layer.TextItem.Size)' 获取文字颜色Dim textColor As SolidColor = layer.TextItem.Color' 获取颜色的分量值Dim red As Integer = textColor.RGB.RedDim green As Integer = textColor.RGB.GreenDim blue As Integer = textColor.RGB.BlueConsole.WriteLine("HexValue: " & textColor.RGB.HexValue)Console.WriteLine("文字颜色:R={0}, G={1}, B={2}", red, green, blue)Console.WriteLine("图层文字BlendMode: " & layer.BlendMode.ToString)Console.WriteLine("变形: {0}" & layer.TextItem.WarpBend)Console.WriteLine("变形弯曲方向: {0}" & layer.TextItem.WarpDirection.ToString)'psNoWarp = 1'psArc = 2'psArcLower = 3'psArcUpper = 4'psArch = 5'psBulge = 6'psShellLower = 7'psShellUpper = 8'psFlag = 9'psWave = 10'psFish = 11'psRise = 12'psFishEye = 13'psInflate = 14'psSqueeze = 15'psTwist = 16Console.WriteLine("变形样式: {0}" & layer.TextItem.WarpStyle.ToString)'抗锯齿效果: 'psNoAntialias = 1 无'psSharp = 2 锐化'psCrisp = 3 清晰'psStrong = 4 浑厚'psSmooth = 5 平滑Console.WriteLine("文本的抗锯齿方法:" & layer.TextItem.AntiAliasMethod.ToString)End IfConsole.WriteLine("BlendMode:" & layer.BlendMode.ToString)If layer.IsBackgroundLayer = True ThenConsole.WriteLine("背景:PixelsLocked={0}, IsBackgroundLayer={1}, Visible={2}", layer.PositionLocked, layer.IsBackgroundLayer, layer.Visible)End IfConsole.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")NextFor Each channel As Channel In doc.ChannelsConsole.WriteLine(" channel.Name=" & channel.Name)Console.WriteLine(" channel.Kind=" & channel.Kind.ToString)NextFor Each historyState As HistoryState In doc.HistoryStatesConsole.WriteLine(" historyState.Name=" & historyState.Name)Console.WriteLine(" historyState.Snapshot=" & historyState.Snapshot)Next' 获取背景图层Dim backgroundLayer As ArtLayer = doc.BackgroundLayerCatch ex As ExceptionConsole.WriteLine("发生异常: " & ex.Message)End Try' 关闭文档doc.Close()' 退出Photoshop应用程序app.Quit()End SubEnd Class
3.解析结果
此方法可以解析到文档基本信息,文档的宽高、颜色模式、分辨率等,图层的基本信息,文字图层信息,文字变化等,图层混合选项及滤镜信息暂时没获取到。