在Photoshop中打开图片来自C#应用程序

问题描述 投票:-1回答:2

我正在开发的C#应用​​程序,并要打开在安装Photoshop图像和编辑后的文件保存得在离那些打开的同一位置。

有没有什么办法可以做到这一点?

c# .net photoshop
2个回答
1
投票

这里是我的代码,我用它来申请使用C#的行动。 (第一部分将告诉你如何在Photoshop中打开一个文件)

它装载的Photoshop,加载图像,适用的操作,然后关闭。

    using Photoshop;  //Adobe Photoshop CC 2014 Object Library
    private void RunPhotoShopAction(string sFile,string ActionSet, string ActionName)
    {
        Photoshop.Application appRef = new Photoshop.Application();
        appRef.Open(sFile);
        appRef.DoAction(ActionSet, ActionName);   
        appRef.ActiveDocument.Close();
    }

更多信息可以在这个文件中找到:

https://www.adobe.com/content/dam/acom/en/devnet/photoshop/pdfs/photoshop-cc-scripting-guide-2019.pdf

你应该能够遵循VBScript中的例子,但只使用由Photoshop的对象库,而不是提供的方法和属性。这是令人惊讶的直截了当。我能够重新创建几乎所有的例子。

[更新]我补充修改后的代码:

using System;
using System.Windows.Forms;
using System.Collections;
using Photoshop;

namespace PhotoShopCSharp2019
{

public partial class Form1 : Form
{

    /// <summary>
    /// This file will contain methods to interface with Photoshop CC
    /// It uses the Adobe Photoshop CC 2014 Object Library
    /// Code was modified from the 
    /// 2019 Photoshop Scripting Guide and Photoshop CC VBScript reference
    /// https://www.adobe.com/devnet/photoshop/scripting.html
    /// The appRef object is global indicating a single instance of Photoshop.
    /// The docRef object will stay local to the methods 
    /// </summary>


    Photoshop.Application appRef = new Photoshop.Application();

    private void HelloWorld() // page 19
    {
        // Modified from Page 19 of Script
        PsUnits originalRulerUnits = appRef.Preferences.RulerUnits;
        appRef.Preferences.RulerUnits = PsUnits.psInches;
        Document docRef = appRef.Documents.Add(2, 4);
        ArtLayer artLayerRef = docRef.ArtLayers.Add();
        artLayerRef.Kind = PsLayerKind.psTextLayer; //Normal Layer
        TextItem textItemRef = artLayerRef.TextItem;
        textItemRef.Contents = "Hello World";
        appRef.Preferences.RulerUnits = originalRulerUnits;
    }

    private Document OpenDocument(string sFile) // Page 29
    {
        Document docRef = appRef.Open(sFile);
        return docRef;
    }


    private void SavePDFDocument(string pdfFilename) // Page 30
    {
        PsUnits originalRulerUnits = appRef.Preferences.RulerUnits;
        appRef.Preferences.RulerUnits = PsUnits.psPixels;
        // Create a PDF option object
        PDFOpenOptions pdfOpenOptionsRef = new PDFOpenOptions();
        pdfOpenOptionsRef.AntiAlias = true;
        pdfOpenOptionsRef.Mode = PsOpenDocumentMode.psOpenRGB;
        pdfOpenOptionsRef.Resolution = 72;
        pdfOpenOptionsRef.Page = 3;
        Document docref = appRef.Open(pdfFilename, pdfOpenOptionsRef);
        //Restore Units
        appRef.Preferences.RulerUnits = originalRulerUnits;

    }
    private void SaveJpegDocument(string jpgFilename) // Page 32
    {
        PsUnits originalRulerUnits = appRef.Preferences.RulerUnits;
        appRef.Preferences.RulerUnits = PsUnits.psPixels;
        JPEGSaveOptions myJPEGOptions = new JPEGSaveOptions();
        myJPEGOptions.EmbedColorProfile = true;
        myJPEGOptions.FormatOptions = PsFormatOptionsType.psStandardBaseline;
        myJPEGOptions.Matte = PsMatteType.psNoMatte;
        myJPEGOptions.Quality = 1;
        appRef.ActiveDocument.SaveAs(jpgFilename, myJPEGOptions, true, PsExtensionType.psLowercase);
    }


    private void SomePreferences() // Holding place for various settings
    {
        appRef.Preferences.RulerUnits = PsUnits.psInches; // page 32
        appRef.Preferences.TypeUnits = PsTypeUnits.psTypePixels; // pag32
        appRef.DisplayDialogs = PsDialogModes.psDisplayNoDialogs; // page 33
    }

    private void PhotoResizeExample(string sFile) // Document Object Example page 35
    {
        PsUnits startRulerUnits = appRef.Preferences.RulerUnits;
        appRef.Preferences.RulerUnits = PsUnits.psInches;
        Document docRef = appRef.Open(sFile); 
        docRef.ResizeImage(4, 4);
        docRef.ResizeCanvas(4, 4);
        docRef.Trim(PsTrimType.psTopLeftPixel, true, false, true, false);
        appRef.Preferences.RulerUnits = PsUnits.psPixels;
        int[] MyArray = { 100, 200, 400, 500 };
        docRef.Crop(MyArray, 45, 20, 20);
        docRef.FlipCanvas(PsDirection.psHorizontal);
        // Restore units
        appRef.Preferences.RulerUnits = startRulerUnits;
    }

    private void CreateArtLayer() // Page 37
    {
        // This is akin to creating a new layer and first determining the layer type

        Document docref = appRef.Documents.Add();
        ArtLayer layerObj = appRef.ActiveDocument.ArtLayers.Add();
        layerObj.Name = "MyBlendLayer";
        layerObj.BlendMode = PsBlendMode.psNormalBlend;
        appRef.ActiveDocument.Selection.SelectAll();
        SolidColor ColorObj = new SolidColor();  // Color to be used with the fill command
        ColorObj.RGB.Red = 255;
        ColorObj.RGB.Green = 0;
        ColorObj.RGB.Blue = 0;

        // fill selection
        appRef.ActiveDocument.Selection.Fill(ColorObj);

    }

    private void CreateLayerSet() // page 38
    {
        appRef.Documents.Add();
        appRef.ActiveDocument.ArtLayers.Add();
        ArtLayer layerRef = appRef.ActiveDocument.Layers[1];
        // Create a new layer set
        LayerSet newLayerSetRef = appRef.ActiveDocument.LayerSets.Add();
        // Move the new layer to after the first layer
        newLayerSetRef.Move(newLayerSetRef, PsElementPlacement.psPlaceAfter);
    }

    private void DuplicateAndPlaceLayer() // Page 39
    {
        Document docref = appRef.Documents.Add();
        appRef.ActiveDocument.ArtLayers.Add();
        LayerSet layerSetRef = docref.LayerSets.Add();
        ArtLayer layerRef = docref.ArtLayers[1].Duplicate(layerSetRef, 2);
    }

    private void LinkingLayes() // Page 40
    {
        Document docref = appRef.Documents.Add();
        ArtLayer layer1Ref = docref.ArtLayers.Add();
        ArtLayer layer2Ref = docref.ArtLayers.Add();
        layer1Ref.Link(layer2Ref);

    }

    private void ApplyLayerStyle(Document docRef,string LayerName, string CaseSensitiveStyleName) // page 40
    {
        // Example uses a layer name of "L1" and a style named "Puzzle (Image)"
        LayerName = "L1";
        CaseSensitiveStyleName = "Puzzle (Image)";
        docRef.ArtLayers[LayerName].ApplyStyle(CaseSensitiveStyleName);
    }

    private void CreateTextLayer(Document docRef,string texLayerName, string MyText) // Page 41
    {
        ArtLayer newLayerRef = docRef.ArtLayers.Add();
        newLayerRef.Kind = PsLayerKind.psTextLayer;
        newLayerRef.Name = texLayerName;
        TextItem textItemRef = docRef.ArtLayers[texLayerName].TextItem;
        textItemRef.Contents = MyText;
        textItemRef.Justification = PsJustification.psRight;
    }

    private void MakeSelection() // Page 43
    {
        // Top Left ==> Origin
        // Ruler set to Pixels
        Document docRef = appRef.Documents.Add();
        int[,] shapeRef = { { 0, 0 }, { 100, 0 },{ 0, 100 },{ 100, 100 } };
        docRef.Selection.Select(shapeRef);
    }

    private void StrokeSelection() // Page 44
    {
        SolidColor strokeColor = new SolidColor();
        strokeColor.CMYK.Cyan = 20;
        strokeColor.CMYK.Magenta = 50;
        strokeColor.CMYK.Yellow = 30;
        strokeColor.CMYK.Black = 0;
        appRef.ActiveDocument.Selection.Stroke(strokeColor, 5, 1, 15, 75, false);
    }

    private void InvertExpandContractFeatherSelection()
    {
        Selection MySelection = appRef.ActiveDocument.Selection;
        PsUnits startRulerUnits = appRef.Preferences.RulerUnits;
        appRef.Preferences.RulerUnits = PsUnits.psPixels;
        MySelection.Invert();
        MySelection.Expand(5); //  5 pixels
        MySelection.Feather(5); // 5 pixels
        appRef.Preferences.RulerUnits = startRulerUnits;
    }


    private void FillSelection()  // Page 45
    {
        SolidColor fillColor = new SolidColor();
        fillColor.RGB.Red = 255;
        fillColor.RGB.Green = 0;
        fillColor.RGB.Blue = 0;
        Selection MySelection = appRef.ActiveDocument.Selection;
        MySelection.Fill(fillColor, 15, 25, false);
    }

    private void FillSelectionWithHistory(Document docRef, int myHistoryState)  // Page 45
    {
        Selection MySelection = appRef.ActiveDocument.Selection;
        MySelection.Fill(docRef.HistoryStates[myHistoryState]);
    }

    private void LoadAndStoreSelection(Document docRef) // Page 46
    {
        Channel chanRef = docRef.Channels.Add();
        chanRef.Name = "My Channel";
        chanRef.Kind = PsChannelType.psSelectedAreaAlphaChannel;
        docRef.Selection.Store(docRef.Channels["My Channel"], PsSelectionType.psExtendSelection);
    }

    private void RestoreSelection(Document docRef) // Page 46
    {
        Selection selRef = appRef.ActiveDocument.Selection;
        selRef.Load(docRef.Channels["My Channel"], PsSelectionType.psExtendSelection);
    }

    private void ChangeChannelType(Channel chanRef)  // Page 47
    {
        // Change to SpotColorChannel for example
        chanRef.Kind= PsChannelType.psSpotColorChannel;
    }

    private void DocumentInfo(Document docRef) // Page 47
    {
        DocumentInfo docInfoRef = docRef.Info;
        docInfoRef.Copyrighted = PsCopyrightedType.psCopyrightedWork;
        docInfoRef.OwnerUrl = "http://www.adobe.com";
    }

    private void MakeActiveHistoryState(Document docRef, int myHistoryState) // Page 48
    {
        docRef.ActiveHistoryState = docRef.HistoryStates[myHistoryState];
    }

    private void SaveCurrentFilter_HistoryState(Document docRef)
    {
        HistoryState savedState = docRef.ActiveHistoryState;
        docRef.ArtLayers[1].ApplyMotionBlur(20, 20); // Angle,Radius
        docRef.ActiveHistoryState = savedState;
    }


    private void Notifier()  // Page 49
    {
        appRef.NotifiersEnabled = true;
        string eventFile = appRef.Path + @"Presets\Scripts\Event Scripts Only\ Welcome.jax";
        appRef.Notifiers.Add("OPN ", eventFile);
    }

    private void PathObject() // Page 50
    {

        ArrayList lineArray = new ArrayList();
        int[] Point1 = { 100, 100 };
        int[] Point2 = { 150, 200 };
        Document docRef = appRef.Documents.Add(5000, 7000, 72, "Simple Line");
        PathPointInfo LineArray0 = new PathPointInfo();
        LineArray0.Kind = PsPointKind.psCornerPoint;
        LineArray0.Anchor = Point1;
        LineArray0.LeftDirection = LineArray0.Anchor;
        LineArray0.RightDirection = LineArray0.Anchor;
        PathPointInfo LineArray1 = new PathPointInfo();
        LineArray1.Kind = PsPointKind.psCornerPoint;
        LineArray1.Anchor = Point2;
        LineArray1.LeftDirection = LineArray1.Anchor;
        LineArray1.RightDirection = LineArray1.Anchor;
        lineArray.Add(LineArray0);
        lineArray.Add(LineArray1);
        SubPathInfo LineSubPathArray0 = new SubPathInfo();
        LineSubPathArray0.Operation = PsShapeOperation.psShapeXOR;
        LineSubPathArray0.Closed = false;
        LineSubPathArray0.EntireSubPath =lineArray;
        PathItem myPathItem = docRef.PathItems.Add("A Line", LineSubPathArray0);
        myPathItem.StrokePath(PsToolType.psBrush);

    }


    private void SetColor()  // Page 51
    {
        SolidColor solidColorRef = new SolidColor();
        solidColorRef.CMYK.Cyan = 20;
        solidColorRef.CMYK.Magenta = 90;
        solidColorRef.CMYK.Yellow = 50;
        solidColorRef.CMYK.Black = 50;
        appRef.ForegroundColor = solidColorRef;

    }

    private void ApplyGaussianBlur(Document docRef, int radius)
    {
        ArtLayer myLayer = docRef.ActiveLayer;
        myLayer.ApplyGaussianBlur(radius);
    }


    private void CopyMerged(Document docRef)
    {
        docRef.Selection.Copy(true);  // true ==> Merged
    }

    private void RunPhotoShopAction(string sFile,string ActionSet, string ActionName)
    {

        appRef.DisplayDialogs = PsDialogModes.psDisplayNoDialogs;
        appRef.Open(sFile);
        appRef.DoAction(ActionSet, ActionName);   // ActionSet,Action
        SaveJpegDocument(@"C:\Images\SavedFile.jpg");
        appRef.ActiveDocument.Close();
        //appRef.Quit();
        //appRef = null;
    }

}

}


0
投票

您可以使用System.Diagnostics.Process.Start(@"ImagePath");使用默认浏览器打开一个图像。

如果你想打开一个自定义浏览器的图像,那么你可以使用以下命令:

Process photoViewer = new Process();
photoViewer.StartInfo.FileName = @"PhotoshopPath";
photoViewer.StartInfo.Arguments = @"ImagePath";
photoViewer.Start();
© www.soinside.com 2019 - 2024. All rights reserved.