/// <summary> /// Saves to file. /// </summary> /// <param name="__docRef">Document</param> /// <param name="__args">Arguments</param> /// <param name="__selectionOnly">if set to <c>true</c> Export Selection Only</param> private void saveToFile(Photoshop.Document __docRef, string[] __args, bool __selectionOnly) { bool __exportLayerComps = false; int __compsCount; int __compsIndex; Photoshop.LayerComp __compRef; Photoshop.Document __duppedDocument; string __fileNameBody = null; bool __hasSelection; bool __exportFonts = false; Format __imageFormat; __exportLayerComps = __form.ExportLayerComps.Checked; //MessageBox.Show(__exportLayerComps.ToString()); bool __isNamedLayerComp = false; Photoshop.JPEGSaveOptions __jpgSaveOptions = null; try { __jpgSaveOptions = new Photoshop.JPEGSaveOptions(); __jpgSaveOptions.EmbedColorProfile = false; __jpgSaveOptions.FormatOptions = Photoshop.PsFormatOptionsType.psStandardBaseline; // 1 psStandardBaseline __jpgSaveOptions.Matte = Photoshop.PsMatteType.psNoMatte; // 1 psNoMatte } catch //(Exception __e) { DialogResult __dr = MessageBox.Show("Photoshop is busy with open dialog or something." + "\r\n" + "\r\n" + "Please switch to Photoshop then close open dialogs or leave editing state", "Photoshop not ready", MessageBoxButtons.OK); if (__dr == DialogResult.OK) { Application.Exit(); } } switch (__args[2 + __form.idx]) { case "jpg": __jpgSaveOptions.Quality = int.Parse(__args[4 + __form.idx]); if (__jpgSaveOptions.Quality == 12) { __exportFonts = true; } __imageFormat = Format.JPG; //__exportLayerComps = true; //force using this mode break; case "png": __imageFormat = Format.PNG; //__exportLayerComps = true; break; case "gif": __imageFormat = Format.GIF; break; case "pdf": __imageFormat = Format.PDF; __exportLayerComps = true; break; case "sc": __jpgSaveOptions.Quality = 12; __imageFormat = Format.JPG; __exportLayerComps = true; //force using this mode break; default: // Export each layer by its name __jpgSaveOptions.Quality = int.Parse(__args[4 + __form.idx]); __imageFormat = Format.JPG; break; } switch (__args[3 + __form.idx]) { case "name": __isNamedLayerComp = true; break; case "index": __isNamedLayerComp = false; break; default: __isNamedLayerComp = false; break; } Photoshop.ExportOptionsSaveForWeb __gifExportOptionsSaveForWeb = new Photoshop.ExportOptionsSaveForWeb(); //gifExportOptionsSaveForWeb.MatteColor = 255 __gifExportOptionsSaveForWeb.Format = Photoshop.PsSaveDocumentType.psCompuServeGIFSave; // 3; __gifExportOptionsSaveForWeb.ColorReduction = Photoshop.PsColorReductionType.psAdaptive; //1; __gifExportOptionsSaveForWeb.Colors = 256; __gifExportOptionsSaveForWeb.Dither = Photoshop.PsDitherType.psNoise; //3; __gifExportOptionsSaveForWeb.DitherAmount = 100; __gifExportOptionsSaveForWeb.Quality = 100; __gifExportOptionsSaveForWeb.Transparency = true; __gifExportOptionsSaveForWeb.TransparencyAmount = 100; __gifExportOptionsSaveForWeb.TransparencyDither = Photoshop.PsDitherType.psNoDither; //2; __gifExportOptionsSaveForWeb.IncludeProfile = false; __gifExportOptionsSaveForWeb.Lossy = 0; __gifExportOptionsSaveForWeb.WebSnap = 0; Photoshop.ExportOptionsSaveForWeb __pngExportOptionsSaveForWeb = new Photoshop.ExportOptionsSaveForWeb(); __pngExportOptionsSaveForWeb.Format = Photoshop.PsSaveDocumentType.psPNGSave; // 13; __pngExportOptionsSaveForWeb.PNG8 = false; __pngExportOptionsSaveForWeb.Transparency = true; Photoshop.PDFSaveOptions __pdfSaveOptions = new Photoshop.PDFSaveOptions(); __pdfSaveOptions.AlphaChannels = false; __pdfSaveOptions.Annotations = false; __pdfSaveOptions.DowngradeColorProfile = true; __pdfSaveOptions.EmbedColorProfile = true; __pdfSaveOptions.Encoding = Photoshop.PsPDFEncodingType.psPDFJPEG; __pdfSaveOptions.Interpolation = false; __pdfSaveOptions.JPEGQuality = 9; __pdfSaveOptions.Layers = false; __pdfSaveOptions.SpotColors = false; __pdfSaveOptions.Transparency = false; __pdfSaveOptions.UseOutlines = false; __pdfSaveOptions.VectorData = false; //MessageBox.Show(__args(3)) //MessageBox.Show(__imageType) __compsCount = __docRef.LayerComps.Count; //ActionSaveScreenSelection __asss = new ActionSaveScreenSelection(__docRef, __jpgSaveOptions); ActionSaveScreenSelection __asss = new ActionSaveScreenSelection(__docRef); __hasSelection = __asss.hasSelection; if (__asss.hasSelection) __asss.wipeOldScreens(); // Exporting layercomps by index or name if (__exportLayerComps) { if (__compsCount <= 1) { //Set textItemRef = appRef.ActiveDocument.Layers(1) //textItemRef.TextItem.Contents = Args.Item(1) //outFileName = Args.Item(1) if (__selectionOnly) // IF screen selection then save crop { if (__asss.count >= 1) { __asss.saveAll(); } } else { //MessageBox.Show(__args[1]); if (__imageFormat == Format.JPG) { __docRef.SaveAs(__args[1 + __form.idx], __jpgSaveOptions, true, null); } else if (__imageFormat == Format.PNG) { __fileNameBody = __docRef.Name.Substring(0, __docRef.Name.LastIndexOf(".")) + ".png"; //MessageBox.Show(__docRef.Path + __fileNameBody); __docRef.Export(__docRef.Path + __fileNameBody, 2, __pngExportOptionsSaveForWeb); if (__fileNameBody.Replace(" ", "-") != __fileNameBody) { if (File.Exists(__docRef.Path + __fileNameBody)) File.Delete(__docRef.Path + __fileNameBody); try { //MessageBox.Show(__fileNameBody.Replace(" ", "-")); File.Move(__docRef.Path + __fileNameBody.Replace(" ", "-"), __docRef.Path + __fileNameBody); } catch { } } } else if (__imageFormat == Format.PDF) { __fileNameBody = __docRef.Name.Substring(0, __docRef.Name.LastIndexOf(".")) + ".pdf"; __docRef.SaveAs(__docRef.Path + __fileNameBody, __pdfSaveOptions, false, Photoshop.PsExtensionType.psLowercase); } else { __fileNameBody = __docRef.Name.Substring(0, __docRef.Name.LastIndexOf(".")) + ".gif"; __docRef.Export(__docRef.Path + __fileNameBody, 2, __gifExportOptionsSaveForWeb); } } } else { //msgbox("comps!") for (__compsIndex = 1; __compsIndex <= __compsCount; __compsIndex++) { //MsgBox(docRef.LayerComps.Count) //End __compRef = __docRef.LayerComps[__compsIndex]; //if (exportInfo.selectionOnly && !compRef.selected) continue; // selected only __compRef.Apply(); //msgbox(compRef.Name) __duppedDocument = null; /*if (__hasSelection) { try { __duppedDocument = __docRef.Duplicate(null, null); } catch (Exception) { MessageBox.Show("Essayez de redémarrer UTC FMCore"); } if (__screenSelectionList.Count > 0) { if (__screenSelectionList.Contains(__compsIndex.ToString())) saveScreenSelection(__docRef, __duppedDocument, __compsIndex, __jpgSaveOptions); else __duppedDocument.Close(2); } else { saveScreenSelection(__docRef, __duppedDocument, __compsIndex, __jpgSaveOptions); } }*/ if (__selectionOnly) { if (__asss.count >= 1) { __asss.saveAll(__compsCount, __compsIndex); } } else { try { __duppedDocument = __docRef.Duplicate(null, null); } catch (Exception) { MessageBox.Show("Essayez de redémarrer UTC FMCore"); } if (!__isNamedLayerComp) { // cleaning single JPEG file if layer comps > 1 if (File.Exists(__docRef.Name.Substring(0, __docRef.Name.LastIndexOf(".")) + ".jpg")) { File.Delete(__docRef.Name.Substring(0, __docRef.Name.LastIndexOf(".")) + ".jpg"); } __fileNameBody = __docRef.Name.Substring(0, __docRef.Name.LastIndexOf(".")) + "." + __compsIndex; } else { __fileNameBody = __compRef.Name; } if (__imageFormat == Format.JPG) { __fileNameBody += ".jpg"; __duppedDocument.SaveAs(__docRef.Path + __fileNameBody, __jpgSaveOptions, true, null); } else if (__imageFormat == Format.PNG) { __fileNameBody += ".png"; //MessageBox.Show(__docRef.Path + __fileNameBody); __duppedDocument.Export(__docRef.Path + __fileNameBody, 2, __pngExportOptionsSaveForWeb); if (__fileNameBody.Replace(" ", "-") != __fileNameBody) { if (File.Exists(__docRef.Path + __fileNameBody)) File.Delete(__docRef.Path + __fileNameBody); try { File.Move(__docRef.Path + __fileNameBody.Replace(" ", "-"), __docRef.Path + __fileNameBody); } catch { } } } else if (__imageFormat == Format.PDF) { __fileNameBody += ".pdf"; //MessageBox.Show(__docRef.Path + __fileNameBody); //__duppedDocument.Export(__docRef.Path + __fileNameBody, 2, __pdfExportOptionsSaveForWeb); __duppedDocument.SaveAs(__docRef.Path + __fileNameBody, __pdfSaveOptions, false, Photoshop.PsExtensionType.psLowercase); } else { __fileNameBody += ".gif"; __duppedDocument.Export(__docRef.Path + __fileNameBody, 2, __gifExportOptionsSaveForWeb); } __duppedDocument.Close(2); } } __compRef = __docRef.LayerComps[1]; __compRef.Apply(); } //MsgBox(Me.AutoArchive.Checked) archiveFiles(__docRef); FileInfo __selectedfile = new FileInfo(__docRef.Name); string __selectedfileext = __selectedfile.Extension; //MessageBox.Show(__selectedfile.Extension); List<string> __allowedext = new List<string>(new string[] { ".psd", ".psb" }); if (__allowedext.Contains(__selectedfile.Extension.ToLower()) && __exportFonts) listFonts(__docRef, __docRef.Name, true); } else //Exporting each layers by name { Photoshop.ArtLayer __layer; for (__compsIndex = 1; __compsIndex <= __docRef.ArtLayers.Count; __compsIndex++) { __layer = __docRef.ArtLayers[__compsIndex]; //isVisible = oLayer.visible __appRef.ActiveDocument.ActiveLayer = __layer; //oLayer.Apply() //duppedDocument = docRef.Duplicate() //msgbox(compRef.Name) __fileNameBody = (string)(__layer.Name + ".jpg"); //msgbox(fileNameBody) __docRef.SaveAs(__docRef.Path + __fileNameBody, __jpgSaveOptions, true, null); __layer.Visible = false; __appRef.ActiveDocument.ActiveLayer = __layer; //duppedDocument.Close(2) } } //ExportImagesRights() if (__compsCount > 0) { __compRef = __docRef.LayerComps[1]; __compRef.Apply(); } }
/// <summary> /// Saves the asset. /// </summary> /// <param name="__docRef">Document reference</param> /// <param name="__name">Name</param> /// <param name="__layers">Layers</param> private void saveAsset(Photoshop.Document __docRef, string __name, object __layers) { Photoshop.Document __duppedDocument; object __bounds = null; Photoshop.ExportOptionsSaveForWeb __pngExportOptionsSaveForWeb = new Photoshop.ExportOptionsSaveForWeb(); __pngExportOptionsSaveForWeb.Format = Photoshop.PsSaveDocumentType.psPNGSave; // 13; __pngExportOptionsSaveForWeb.PNG8 = false; __pngExportOptionsSaveForWeb.Transparency = true; if (!Directory.Exists(__docRef.Path + "+ Assets\\")) { Directory.CreateDirectory(__docRef.Path + "+ Assets\\"); } if (__layers != null) __bounds = checkBounds(__layers); Photoshop.ActionDescriptor __desc = __appRef.ExecuteAction(__appRef.StringIDToTypeID("newPlacedLayer"), null, Photoshop.PsDialogModes.psDisplayNoDialogs); __duppedDocument = __docRef.Duplicate(__name, null); __appRef.ActiveDocument = __duppedDocument; moveLayer(__name); selectAllLayers(); deselectLayer(__name); hideAllLayers(); deselectLayers(); //System.Threading.Thread.Sleep(2000); if (__bounds == null) { __duppedDocument.Trim(Photoshop.PsTrimType.psTransparentPixels, true, true, true, true); } else { __duppedDocument.Crop(__bounds, null, null, null); } string __tempName = (__name.IndexOf("@1x") > -1) ? Regex.Replace(__name, "@1x", "") : __name; __tempName = Regex.Replace(__tempName, "(\\+)+\\s", ""); __duppedDocument.Export(__docRef.Path + "+ Assets\\" + __tempName, 2, __pngExportOptionsSaveForWeb); __name = Regex.Replace(__name, "(\\+)+\\s", ""); if (__name.IndexOf("@2x") > -1) { //__duppedDocument.ResizeImage(__duppedDocument.Width / 2, __duppedDocument.Width / 2, null, Photoshop.PsResampleMethod.psBicubicSmoother); resizeImage(__duppedDocument.Width / 2); try { __duppedDocument.Export(__docRef.Path + "+ Assets\\" + Regex.Replace(__name, "@2x", ""), 2, __pngExportOptionsSaveForWeb); } catch { MessageBox.Show("Please allow to save all slices in Save For Web options"); } } else if (__name.IndexOf("@1x") > -1) { //__duppedDocument.ResizeImage(__duppedDocument.Width * 2, __duppedDocument.Width * 2, null, Photoshop.PsResampleMethod.psBicubicSmoother); resizeImage(__duppedDocument.Width * 2); try { __duppedDocument.Export(__docRef.Path + "+ Assets\\" + Regex.Replace(__name, "@1x", "@2x"), 2, __pngExportOptionsSaveForWeb); } catch { MessageBox.Show("Please allow to save all slices in Save For Web options"); } } __duppedDocument.Close(2); }