1. 程式人生 > >影象處理控制元件Aspose.Imaging v19.6新版亮點示例詳解(1)

影象處理控制元件Aspose.Imaging v19.6新版亮點示例詳解(1)

Aspose.Imaging for .NET一種高階影象處理控制元件,允許開發人員建立,編輯,繪製或轉換影象。影象匯出和轉換是API核心功能之一,它允許在不安裝Photoshop應用程式或任何其他影象編輯器的情況下儲存為AdobePhotoshop®本機格式。

近期釋出了Aspose.Imaging for .NET v19.6,JPEG輸出中不再保留IMAGINGNET-3351 DPI屬性,下面我們一起來探索新版中的新增功能及其工作原理。>>歡迎下載Aspose.Imaging for .NET v19.6體驗

▲IMAGINGNET-3376 - Backpose Aspose.PSD程式碼更新到Aspose.Imaging April / 2019

 //支援SoCoResource
    string sourceFileName = "ColorFillLayer.psd";
    string exportPath = "SoCoResource_Edited.psd";
 
    var im = (PsdImage)Image.Load(sourceFileName);
 
    using (im)
    {
        foreach (var layer in im.Layers)
        {
            if (layer is FillLayer)
            {
                var fillLayer = (FillLayer)layer;
                foreach (var resource in fillLayer.Resources)
                {
                    if (resource is SoCoResource)
                    {
                        var socoResource = (SoCoResource)resource;
                        Assert.AreEqual(Color.FromArgb(63, 83, 141), socoResource.Color);
                        socoResource.Color = Color.Red;
                        break;
                    }
                 }
                 break;
             }
            im.Save(exportPath);
        }
    }

 

 //支援GdFlResource
    string sourceFileName = "ComplexGradientFillLayer.psd";
    string exportPath = "ComplexGradientFillLayer_after.psd";
 
    var im = (PsdImage)Image.Load(sourceFileName);
 
    using (im)
    {
        foreach (var layer in im.Layers)
        {
                    if (layer is FillLayer)
                    {
                        var fillLayer = (FillLayer)layer;
 
                        var resources = fillLayer.Resources;
 
                        foreach (var res in resources)
                        {
                            if (res is GdFlResource)
                            {
                               //閱讀
                                var resource = (GdFlResource)res;
 
                                if (resource.AlignWithLayer != false ||
                                (Math.Abs(resource.Angle - 45.0) > 0.001) ||
                                resource.Dither != true ||
                                resource.Reverse != false ||
                                resource.Color != Color.Empty ||
                                Math.Abs(resource.HorizontalOffset - (-39)) > 0.001 ||
                                Math.Abs(resource.VerticalOffset - (-5)) > 0.001 ||
                                resource.TransparencyPoints.Length != 3 ||
                                resource.ColorPoints.Length != 2)
                                {
                                    throw new Exception("Resource Parameters were read wrong");
                                }
 
                                var transparencyPoints = resource.TransparencyPoints;
 
                                if (Math.Abs(100.0 - transparencyPoints[0].Opacity) > 0.25 ||
                                transparencyPoints[0].Location != 0 ||
                                transparencyPoints[0].MedianPointLocation != 50 ||
                                Math.Abs(50.0 - transparencyPoints[1].Opacity) > 0.25 ||
                                transparencyPoints[1].Location != 2048 ||
                                transparencyPoints[1].MedianPointLocation != 50 ||
                                Math.Abs(100.0 - transparencyPoints[2].Opacity) > 0.25 ||
                                transparencyPoints[2].Location != 4096 ||
                                transparencyPoints[2].MedianPointLocation != 50)
                                {
                                    throw new Exception("Gradient Transparency Points were read Wrong");
                                }
 
                                var colorPoints = resource.ColorPoints;
 
                                if (colorPoints[0].Color != Color.FromArgb(203, 64, 140) ||
                                colorPoints[0].Location != 0 ||
                                colorPoints[0].MedianPointLocation != 50 ||
                                colorPoints[1].Color != Color.FromArgb(203, 0, 0) ||
                                colorPoints[1].Location != 4096 ||
                                colorPoints[1].MedianPointLocation != 50)
                                {
                                    throw new Exception("Gradient Color Points were read Wrong");
                                }
 
                                //編輯
                                resource.Angle = 30.0;
                                resource.Dither = false;
                                resource.AlignWithLayer = true;
                                resource.Reverse = true;
                                resource.HorizontalOffset = 25;
                                resource.VerticalOffset = -15;
 
                                var newColorPoints = new List  (resource.ColorPoints);
                                var newTransparencyPoints = new List  (resource.TransparencyPoints);
 
                                newColorPoints.Add(new GradientColorPoint()
                                {
                                    Color = Color.Violet,
                                    Location = 4096,
                                    MedianPointLocation = 75
                                });
 
                                colorPoints[1].Location = 3000;
 
                                newTransparencyPoints.Add(new GradientTransparencyPoint()
                                {
                                    Opacity = 80.0,
                                    Location = 4096,
                                    MedianPointLocation = 25
                                });
 
                                transparencyPoints[2].Location = 3000;
 
                                resource.ColorPoints = newColorPoints.ToArray();
                                resource.TransparencyPoints = newTransparencyPoints.ToArray();
 
                                im.Save(exportPath);
                            }
 
                            break;
                        }
 
                        break;
                    }
                }   
    }

 

 //支援VmskResource
        static void ExampleOfVmskResourceSupport()
        {
            string sourceFileName = "Rectangle.psd";
            string exportPath = "Rectangle_changed.psd";
 
            var im = (PsdImage)Image.Load(sourceFileName);
 
            using (im)
            {
                var resource = GetVmskResource(im);
 
                //閱讀
                if (resource.IsDisabled != false ||
                    resource.IsInverted != false ||
                    resource.IsNotLinked != false ||
                    resource.Paths.Length != 7 ||
                    resource.Paths[0].Type != VectorPathType.PathFillRuleRecord ||
                    resource.Paths[1].Type != VectorPathType.InitialFillRuleRecord ||
                    resource.Paths[2].Type != VectorPathType.ClosedSubpathLengthRecord ||
                    resource.Paths[3].Type != VectorPathType.ClosedSubpathBezierKnotUnlinked ||
                    resource.Paths[4].Type != VectorPathType.ClosedSubpathBezierKnotUnlinked ||
                    resource.Paths[5].Type != VectorPathType.ClosedSubpathBezierKnotUnlinked||
                    resource.Paths[6].Type != VectorPathType.ClosedSubpathBezierKnotUnlinked)
                {
                        throw new Exception("VmskResource was read wrong");
                }
 
                var pathFillRule = (PathFillRuleRecord)resource.Paths[0];
                var initialFillRule = (InitialFillRuleRecord)resource.Paths[1];
                var subpathLength = (LengthRecord)resource.Paths[2];
 
                //路徑填充規則不包含任何附加資訊
                if (pathFillRule.Type != VectorPathType.PathFillRuleRecord || 
                initialFillRule.Type != VectorPathType.InitialFillRuleRecord ||
                initialFillRule.IsFillStartsWithAllPixels != false ||
                subpathLength.Type != VectorPathType.ClosedSubpathLengthRecord ||
                subpathLength.IsClosed != true ||
                subpathLength.IsOpen != false) 
                {
                    throw new Exception("VmskResource paths were read wrong");
                }
 
                // 編輯
                resource.IsDisabled = true;
                resource.IsInverted = true;
                resource.IsNotLinked = true;
 
                var bezierKnot = (BezierKnotRecord)resource.Paths[3];
                bezierKnot.Points[0] = new Point(0, 0);
 
                bezierKnot = (BezierKnotRecord)resource.Paths[4];
                bezierKnot.Points[0] = new Point(8039797, 10905190);
 
                initialFillRule.IsFillStartsWithAllPixels = true;
                subpathLength.IsClosed = false;
                im.Save(exportPath);
            }         
        }
            static VmskResource GetVmskResource(PsdImage image)
        {
            var layer = image.Layers[1];
 
            VmskResource resource = null;
            var resources = layer.Resources;
            for (int i = 0; i < resources.Length; i++)
            {
                if (resources[i] is VmskResource)
                {
                    resource = (VmskResource) resources[i];
                    break;
                }
            }
 
            if (resource == null)
            {
                throw new Exception("VmskResource not found");
            }
 
            return resource;
        }

 

//新增填充層支援:顏色填充
    string sourceFileName = "ColorFillLayer.psd";
    string exportPath = "ColorFillLayer_output.psd";
    string exportPathPng = "ColorFillLayer_output.png";
 
    var im = (PsdImage)Image.Load(sourceFileName);
 
    using (im)
    {
        foreach (var layer in im.Layers)
        {
            if (layer is FillLayer)
            {
                var fillLayer = (FillLayer)layer;
 
                if (fillLayer.FillSettings.FillType != FillType.Color) 
                {
                    throw new Exception("Wrong Fill Layer");
                }
 
                var settings = (IColorFillSettings)fillLayer.FillSettings;
 
                settings.Color = Color.Red;    
                fillLayer.Update(); 
                im.Save(exportPath);
                break;
            }
        }
    }

 

//支援漸變填充層
    string sourceFileName = "ComplexGradientFillLayer.psd";
    string outputFile = "ComplexGradientFillLayer_output.psd";
 
    var im = (PsdImage)Image.Load(sourceFileName);
 
    using (im)
            {
                foreach (var layer in im.Layers)
                {
                    if (layer is FillLayer)
                    {
                        var fillLayer = (FillLayer)layer;
 
                        if (fillLayer.FillSettings.FillType != FillType.Gradient)
                        {
                            throw new Exception("Wrong Fill Layer");
                        }
 
                        var settings = (IGradientFillSettings)fillLayer.FillSettings;
 
                        if (
                            Math.Abs(settings.Angle - 45) > 0.25 ||
                            settings.Dither != true ||
                            settings.AlignWithLayer != false ||
                            settings.Reverse != false ||
                            Math.Abs(settings.HorizontalOffset - (-39)) > 0.25 ||
                            Math.Abs(settings.VerticalOffset - (-5)) > 0.25 ||
                            settings.TransparencyPoints.Length != 3 ||
                            settings.ColorPoints.Length != 2 ||
                            Math.Abs(100.0 - settings.TransparencyPoints[0].Opacity) > 0.25 ||
                            settings.TransparencyPoints[0].Location != 0 ||
                            settings.TransparencyPoints[0].MedianPointLocation != 50 ||
                            settings.ColorPoints[0].Color != Color.FromArgb(203, 64, 140) ||
                            settings.ColorPoints[0].Location != 0 ||
                            settings.ColorPoints[0].MedianPointLocation != 50)
                        {
                            throw new Exception("Gradient Fill was not read correctly");
                        }
 
                        settings.Angle = 0.0;
                        settings.Dither = false;
                        settings.AlignWithLayer = true;
                        settings.Reverse = true;
                        settings.HorizontalOffset = 25;
                        settings.VerticalOffset = -15;
 
                        var colorPoints = new List(settings.ColorPoints);
                        var transparencyPoints = new List(settings.TransparencyPoints);
 
                        colorPoints.Add(new GradientColorPoint()
                        {
                            Color = Color.Violet,
                            Location = 4096,
                            MedianPointLocation = 75
                        });
 
                        colorPoints[1].Location = 3000;
 
                        transparencyPoints.Add(new GradientTransparencyPoint()
                        {
                            Opacity = 80.0,
                            Location = 4096,
                            MedianPointLocation = 25
                        });
 
                        transparencyPoints[2].Location = 3000;
 
                        settings.ColorPoints = colorPoints.ToArray();
                        settings.TransparencyPoints = transparencyPoints.ToArray();
                        fillLayer.Update();
 
                        im.Save(outputFile, new PsdOptions(im));
 
                        break;
                    }
                }
            }

 

//轉換矩陣文字層旋轉的渲染
      string sourceFileName = "TransformedText.psd";
      string exportPath = "TransformedTextExport.psd";
      string exportPathPng = "TransformedTextExport.png";
 
      var im = (PsdImage)Image.Load(sourceFileName);
 
      using (im)
      {
         im.Save(exportPath);
         im.Save(exportPathPng, new PngOptions() { ColorType = PngColorType.TruecolorWithAlpha });