VB 通过COM接口解析PSD文件

2024-04-04 21:44
文章标签 接口 解析 com vb psd

本文主要是介绍VB 通过COM接口解析PSD文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近有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.解析结果

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

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

这篇关于VB 通过COM接口解析PSD文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/876883

相关文章

python常见环境管理工具超全解析

《python常见环境管理工具超全解析》在Python开发中,管理多个项目及其依赖项通常是一个挑战,下面:本文主要介绍python常见环境管理工具的相关资料,文中通过代码介绍的非常详细,需要的朋友... 目录1. conda2. pip3. uvuv 工具自动创建和管理环境的特点4. setup.py5.

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

Python包管理工具核心指令uvx举例详细解析

《Python包管理工具核心指令uvx举例详细解析》:本文主要介绍Python包管理工具核心指令uvx的相关资料,uvx是uv工具链中用于临时运行Python命令行工具的高效执行器,依托Rust实... 目录一、uvx 的定位与核心功能二、uvx 的典型应用场景三、uvx 与传统工具对比四、uvx 的技术实

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

MybatisPlus service接口功能介绍

《MybatisPlusservice接口功能介绍》:本文主要介绍MybatisPlusservice接口功能介绍,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录Service接口基本用法进阶用法总结:Lambda方法Service接口基本用法MyBATisP

Redis过期删除机制与内存淘汰策略的解析指南

《Redis过期删除机制与内存淘汰策略的解析指南》在使用Redis构建缓存系统时,很多开发者只设置了EXPIRE但却忽略了背后Redis的过期删除机制与内存淘汰策略,下面小编就来和大家详细介绍一下... 目录1、简述2、Redis http://www.chinasem.cn的过期删除策略(Key Expir

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析

《Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析》InstantiationAwareBeanPostProcessor是Spring... 目录一、什么是InstantiationAwareBeanPostProcessor?二、核心方法解

深入解析 Java Future 类及代码示例

《深入解析JavaFuture类及代码示例》JavaFuture是java.util.concurrent包中用于表示异步计算结果的核心接口,下面给大家介绍JavaFuture类及实例代码,感兴... 目录一、Future 类概述二、核心工作机制代码示例执行流程2. 状态机模型3. 核心方法解析行为总结:三

springboot项目中使用JOSN解析库的方法

《springboot项目中使用JOSN解析库的方法》JSON,全程是JavaScriptObjectNotation,是一种轻量级的数据交换格式,本文给大家介绍springboot项目中使用JOSN... 目录一、jsON解析简介二、Spring Boot项目中使用JSON解析1、pom.XML文件引入依