专业的编程技术博客社区

网站首页 > 博客文章 正文

图像处理控件Aspose.Imaging v19.6新版亮点示例详解(1)

baijin 2024-09-11 00:52:09 博客文章 6 ℃ 0 评论

Aspose.Imaging for .NET一种高级图像处理控件,允许开发人员创建,编辑,绘制或转换图像。图像导出和转换是API核心功能之一,它允许在不安装Photoshop应用程序或任何其他图像编辑器的情况下保存为AdobePhotoshop?本机格式。

近期发布了Aspose.Imaging for .NET v19.6,JPEG输出中不再保留IMAGINGNET-3351 DPI属性,下面我们一起来探索新版中的新增功能及其工作原理。

▲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 });
 }

>>下载Aspose.Imaging for .NET v19.6体验点击下方“了解更多”

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表