VB 通过COM接口解析PSD文件

最近有PS测评的需求,故而想到了解析psd文件,目的就是为了获取文档信息和图层信息;获取PS的图像信息有很多方式,有过程性的,比如监听PS的各种操作事件;有结果性的,比如本文写的解析PSD文件。

0.添加Photoshop 引用

在解决方案邮件点击添加>>引用,如下图:
在这里插入图片描述
在引用管理器中,点击COM,找到Adobe Photoshop XXX ,勾上,点击确定。
在这里插入图片描述

1.在vb文件添加导入Photoshop

在这里插入图片描述

2.解析psd

话不多说,直接上代码:


Imports Photoshop
Imports System.IO

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim filePath As String = "D:\PhotoshopCS6操作题1\1\PS1.psd"
        parsePsd(filePath)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Console.WriteLine("ssss")
        Dim filePath As String = "D:\PhotoshopCS6操作题1\1\PS1.psd"
        parsePsd(filePath)
    End Sub

    Private Sub parsePsd(filePath As String)
        Dim fileInfo As New FileInfo(filePath)
        Dim fileSizeInBytes As Long = fileInfo.Length
        Dim fileSizeInMB As Double = fileSizeInBytes / (1024 * 1024)

        If fileSizeInMB > 5 Then
            Console.WriteLine("文档大小[{0}MB]超过5MB", fileSizeInMB)
        Else
            Console.WriteLine("文档大小[{0}MB]符合要求", fileSizeInMB, fileSizeInBytes)
        End If
        ' 创建Photoshop应用程序对象
        Dim app As New Photoshop.Application()

        ' 打开PSD文件
        Dim doc As Photoshop.Document = app.Open(filePath)
        Try
            Console.WriteLine("名称: " & doc.Name)
            Console.WriteLine("文档尺寸宽: " & doc.Width)
            Console.WriteLine("文档尺寸高: " & doc.Height)
            Console.WriteLine("颜色模式: " & doc.Mode.ToString)

            ' 获取图像分辨率
            Dim resolution As Double = doc.Resolution
            Console.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.Layers
                Console.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 Then
                    Console.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.Red
                    Dim green As Integer = textColor.RGB.Green
                    Dim blue As Integer = textColor.RGB.Blue
                    Console.WriteLine("HexValue: " & textColor.RGB.HexValue)
                    Console.WriteLine("文字颜色:R={0}, G={1}, B={2}", red, green, blue)
                End If
                If layer.IsBackgroundLayer = True Then
                    Console.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 If
                End If
            Next

            Console.WriteLine("========================================")

            For Each layer As ArtLayer In doc.ArtLayers
                Console.WriteLine("图层名称: " & layer.Name)
                Console.WriteLine("图层LayerType: " & layer.Kind.ToString)
                Console.WriteLine("图层Opacity: " & layer.Opacity)
                Console.WriteLine("图层IsBackgroundLayer: " & layer.IsBackgroundLayer)

                If layer.Kind = PsLayerKind.psTextLayer Then


                    Console.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.Red
                    Dim green As Integer = textColor.RGB.Green
                    Dim blue As Integer = textColor.RGB.Blue
                    Console.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 = 16
                    Console.WriteLine("变形样式: {0}" & layer.TextItem.WarpStyle.ToString)
                    '抗锯齿效果: 
                    'psNoAntialias = 1 无
                    'psSharp = 2 锐化
                    'psCrisp = 3 清晰
                    'psStrong = 4 浑厚
                    'psSmooth = 5 平滑
                    Console.WriteLine("文本的抗锯齿方法:" & layer.TextItem.AntiAliasMethod.ToString)


                End If
                Console.WriteLine("BlendMode:" & layer.BlendMode.ToString)

                If layer.IsBackgroundLayer = True Then
                    Console.WriteLine("背景:PixelsLocked={0}, IsBackgroundLayer={1}, Visible={2}", layer.PositionLocked, layer.IsBackgroundLayer, layer.Visible)

                End If
                    Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
            Next

            For Each channel As Channel In doc.Channels
                Console.WriteLine(" channel.Name=" & channel.Name)
                Console.WriteLine(" channel.Kind=" & channel.Kind.ToString)
            Next
            For Each historyState As HistoryState In doc.HistoryStates
                Console.WriteLine(" historyState.Name=" & historyState.Name)
                Console.WriteLine(" historyState.Snapshot=" & historyState.Snapshot)
            Next

            ' 获取背景图层
            Dim backgroundLayer As ArtLayer = doc.BackgroundLayer

          
        Catch ex As Exception
            Console.WriteLine("发生异常: " & ex.Message)
        End Try
        ' 关闭文档
        doc.Close()
        ' 退出Photoshop应用程序
        app.Quit()
    End Sub

End Class

3.解析结果

此方法可以解析到文档基本信息,文档的宽高、颜色模式、分辨率等,图层的基本信息,文字图层信息,文字变化等,图层混合选项及滤镜信息暂时没获取到。

在这里插入图片描述
在这里插入图片描述

相关推荐

  1. 汽车VBF格式文件详解---使用python解析VBF文件

    2024-04-05 09:06:01       17 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-05 09:06:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-05 09:06:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-05 09:06:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-05 09:06:01       20 阅读

热门阅读

  1. Spring 自定义接口参数解析器

    2024-04-05 09:06:01       16 阅读
  2. Linux下I2C设备驱动:I2C设备和驱动匹配过程

    2024-04-05 09:06:01       14 阅读
  3. DPDK系列之四十二DPDK应用网络编程UDP编程

    2024-04-05 09:06:01       14 阅读
  4. K8s svc失效

    2024-04-05 09:06:01       10 阅读
  5. MongoDB聚合运算符:$maxN(用于数组)

    2024-04-05 09:06:01       12 阅读
  6. LeetCode-热题100:55. 跳跃游戏

    2024-04-05 09:06:01       13 阅读
  7. redis分布式锁

    2024-04-05 09:06:01       13 阅读
  8. 什么是CSS编程语言?怎么使用?

    2024-04-05 09:06:01       12 阅读
  9. 【leetcode面试经典150题】9.跳跃游戏(C++)

    2024-04-05 09:06:01       12 阅读
  10. tomcat 常用的一些配置

    2024-04-05 09:06:01       11 阅读