修改图像

位图图像的抖动

抖动是一种通过改变实际创建图像的点的模式来创造新颜色和阴影 illusio的技术。它是减少图像的色彩范围到 256 种(或更少)色彩的最常见方式。Aspose.PSD 通过引入接受两个参数的 Dither 方法为 RasterImage 类提供了抖动支持。第一个参数是要应用的 DitheringMethod 类型,有两种可能的选择:FloydSteinbergDithering 和 ThresholdDithering。Dither 方法的第二个参数是整数类型的 BitCount 。 BitCount 定义了抖动结果的采样大小。默认值为 1,代表黑白色彩,而允许的值为 1, 4, 8,分别生成具有 2、4 和 256 种色彩的调色板。

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFile = dataDir + @"sample.psd";
string destName = dataDir + @"SampleImage_out.bmp";
// Load an existing image into an instance of RasterImage class
using (var image = (PsdImage)Image.Load(sourceFile))
{
// Peform Floyd Steinberg dithering on the current image and Save the resultant image
image.Dither(DitheringMethod.ThresholdDithering, 4);
image.Save(destName, new BmpOptions());
}

调整亮度、对比度和伽玛

数字图像中的颜色调整是大多数图像处理库提供的核心功能之一。颜色调整可以分为以下几种。

  1. 亮度是指颜色的明亮程度或暗淡程度。增加图像的亮度会使所有颜色变亮,而减少亮度会使所有颜色变暗。
  2. 对比度是指使图像中的对象或细节更加明显。增加图像的对比度会增加光线和暗区之间的差异,使得光亮区域变得更亮,暗区域变得更暗。降低对比度将使亮和暗区域保持大致相同,但整体图像变得更加均匀。
  3. 伽玛优化了照明图像的对比度和亮度。

调整亮度

Aspose.PSD for .NET API 为 RasterImage 类提供了 AdjustBrightness 方法,可通过传递整数值来调整图像亮度。参数值越高,图像越亮。以下是用于比较的原始图像和处理后的图像。

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFile = dataDir + @"sample.psd";
string destName = dataDir + @"AdjustBrightness_out.tiff";
using (var image = (PsdImage)Image.Load(sourceFile))
{
RasterCachedImage rasterImage = image;
// Set the brightness value. The accepted values of brightness are in the range [-255, 255].
rasterImage.AdjustBrightness(-50);
TiffOptions tiffOptions = new TiffOptions(TiffExpectedFormat.Default);
rasterImage.Save(destName, tiffOptions);
}

调整对比度

RasterImage 类公开的 AdjustContrast 方法可通过传递浮点值来调整图像的对比度。

参数值越高,给定图像的对比度越高。以下是用于比较的原始图像和处理后图像。

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFile = dataDir + @"sample.psd";
string destName = dataDir + @"AdjustContrast_out.tiff";
// Load an existing image into an instance of RasterImage class
using (var image = Image.Load(sourceFile))
{
// Cast object of Image to RasterImage
RasterImage rasterImage = (RasterImage)image;
// Check if RasterImage is cached and Cache RasterImage for better performance
if (!rasterImage.IsCached)
{
rasterImage.CacheData();
}
// Adjust the contrast
rasterImage.AdjustContrast(50);
// Create an instance of TiffOptions for the resultant image, Set various properties for the object of TiffOptions and Save the resultant image to TIFF format
TiffOptions tiffOptions = new TiffOptions(TiffExpectedFormat.Default);
tiffOptions.BitsPerSample = new ushort[] { 8, 8, 8 };
tiffOptions.Photometric = TiffPhotometrics.Rgb;
rasterImage.Save(destName, tiffOptions);
}

调整伽玛

RasterImage 类公开的 AdjustGamma 方法有两个版本。其中一个重载接受一个浮点值,并对红色、蓝色和绿色通道系数执行伽玛校正。而另一个重载接受三个浮点参数,分别表示每个颜色系数。以下代码示例演示了如何在图像上应用 AdjustGamma。

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFile = dataDir + @"sample.psd";
string destName = dataDir + @"AdjustGamma_out.tiff";
// Load an existing image into an instance of RasterImage class
using (var image = Image.Load(sourceFile))
{
// Cast object of Image to RasterImage
RasterImage rasterImage = (RasterImage)image;
// Check if RasterImage is cached and Cache RasterImage for better performance
if (!rasterImage.IsCached)
{
rasterImage.CacheData();
}
// Adjust the gamma
rasterImage.AdjustGamma(2.2f, 2.2f, 2.2f);
// Create an instance of TiffOptions for the resultant image, Set various properties for the object of TiffOptions and Save the resultant image to TIFF format
TiffOptions tiffOptions = new TiffOptions(TiffExpectedFormat.Default);
tiffOptions.BitsPerSample = new ushort[] { 8, 8, 8 };
tiffOptions.Photometric = TiffPhotometrics.Rgb;
rasterImage.Save(destName, tiffOptions);
}

模糊图像

本文演示了如何使用 Aspose.PSD for .NET 在图像上执行模糊效果。Aspose.PSD 的 API 提供了高效且易于使用的方法来实现此目标。Aspose.PSD for .NET 公开了 GaussianBlurFilterOptions 类,用于动态创建模糊效果。GaussianBlurFilterOptions 类需要半径(radius)和 sigma 值来在图像上创建模糊效果。执行重塑的步骤如下:

  1. 使用 Image 类公开的工厂方法 Load 加载图像。
  2. 将图像转换为 RasterImage
  3. 使用默认构造函数创建 GaussianBlurFilterOptions 类的实例,或在构造函数中提供 radius 和 sigma 值。
  4. 在指定矩形作为图像边界的情况下,调用 RasterImage.Filter 方法,并提供 GaussianBlurFilterOptions 类的实例。
  5. 保存结果。

以下代码示例演示了如何在图像上创建模糊效果。

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFile = dataDir + @"sample.psd";
string destName = dataDir + @"BlurAnImage_out.gif";
// Load an existing image into an instance of RasterImage class
using (var image = Image.Load(sourceFile))
{
// Convert the image into RasterImage,
//Pass Bounds[rectangle] of image and GaussianBlurFilterOptions instance to Filter method and Save the results
RasterImage rasterImage = (RasterImage)image;
rasterImage.Filter(rasterImage.Bounds, new GaussianBlurFilterOptions(15, 15));
rasterImage.Save(destName, new GifOptions());
}

验证图像透明度

本文演示了如何使用 Aspose.PSD for .NET 检查图像透明度。检查图像透明度的步骤如下:

  1. 使用 Image 类公开的工厂方法 Load 加载图像。
  2. 检查图像的不透明度,如果不透明度为零,则图像是透明的。 以下代码示例演示了如何检查图像是否透明。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFile = dataDir + @"sample.psd";
string destName = dataDir + @"AdjustBrightness_out.tiff";
// Load an existing image into an instance of RasterImage class
using (PsdImage image = (PsdImage)Image.Load(sourceFile))
{
float opacity = image.ImageOpacity;
Console.WriteLine(opacity);
if (opacity == 0)
{
// The image is fully transparent.
}
}

实现有损 GIF 压缩器

使用 Aspose.PSD for .NET,开发人员可以设置像素差异。GIF 的压缩基于所见像素字符串的“字典”。普通编码器在字典中搜索与图像中像素完全匹配的最长像素字符串。有损编码器选择与图像中的像素“足够相似”的最长像素字符串。以下是所述功能的代码演示。

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFile = dataDir + @"sample.psd";
string destName = dataDir + @"anim_lossy-200.gif";
GifOptions gifExport = new GifOptions();
// Load an existing image into an instance of RasterImage class
using (var image = Image.Load(sourceFile))
{
gifExport.MaxDiff = 80;
image.Save("anim_lossy-80.gif", gifExport);
gifExport.MaxDiff = 200;
image.Save(destName, gifExport);
}

实现双三次重采样

重采样意味着您正在更改图像的像素尺寸。当您降采样时,您正在消除像素,因此从图像中删除信息和细节。当您上采样时,您正在添加像素。Photoshop 使用插值来添加这些像素。本文演示了如何使用 Aspose.PSD for .NET 执行双三次重采样。

以下是所述功能的代码演示。

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string destNameCubicConvolution = dataDir + "ResamplerCubicConvolutionStripes_after.psd";
// Load an existing image into an instance of PsdImage class
using (PsdImage image = (PsdImage)Image.Load(sourceFile))
{
image.Resize(300, 300, ResizeType.CubicConvolution);
image.Save(destNameCubicConvolution, new PsdOptions(image));
}
string destNameCatmullRom = dataDir + "ResamplerCatmullRomStripes_after.psd";
// Load an existing image into an instance of PsdImage class
using (PsdImage image = (PsdImage)Image.Load(sourceFile))
{
image.Resize(300, 300, ResizeType.CatmullRom);
image.Save(destNameCatmullRom, new PsdOptions(image));
}
string destNameMitchell = "ResamplerMitchellStripes_after.psd";
// Load an existing image into an instance of PsdImage class
using (PsdImage image = (PsdImage)Image.Load(sourceFile))
{
image.Resize(300, 300, ResizeType.Mitchell);
image.Save(destNameMitchell, new PsdOptions(image));
}
string destNameCubicBSpline = "ResamplerCubicBSplineStripes_after.psd";
// Load an existing image into an instance of PsdImage class
using (PsdImage image = (PsdImage)Image.Load(sourceFile))
{
image.Resize(300, 300, ResizeType.CubicBSpline);
image.Save(destNameCubicBSpline, new PsdOptions(image));
}
string destNameSinC = "ResamplerSinCStripes_after.psd";
// Load an existing image into an instance of PsdImage class
using (PsdImage image = (PsdImage)Image.Load(sourceFile))
{
image.Resize(300, 300, ResizeType.SinC);
image.Save(destNameSinC, new PsdOptions(image));
}
string destNameBell = "ResamplerBellStripes_after.psd";
// Load an existing image into an instance of PsdImage class
using (PsdImage image = (PsdImage)Image.Load(sourceFile))
{
image.Resize(300, 300, ResizeType.Bell);
image.Save(destNameBell, new PsdOptions(image));
}

色彩平衡调整图层

本文演示了如何使用 Aspose.PSD for .NET 在图像上执行色彩平衡调整图层。色彩平衡调整图层使您能够调整图像的着色。它呈现三个颜色通道及其互补颜色,您可以调整这些对来改变照片的外观。

以下是所述功能的代码演示。

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
var outputPath = dataDir + "ColorBalance_out.psd";
using (var im = (FileFormats.Psd.PsdImage)Image.Load(filePath))
{
foreach (var layer in im.Layers)
{
var cbLayer = layer as ColorBalanceAdjustmentLayer;
if (cbLayer != null)
{
cbLayer.ShadowsCyanRedBalance = 30;
cbLayer.ShadowsMagentaGreenBalance = -15;
cbLayer.ShadowsYellowBlueBalance = 40;
cbLayer.MidtonesCyanRedBalance = -90;
cbLayer.MidtonesMagentaGreenBalance = -25;
cbLayer.MidtonesYellowBlueBalance = 20;
cbLayer.HighlightsCyanRedBalance = -30;
cbLayer.HighlightsMagentaGreenBalance = 67;
cbLayer.HighlightsYellowBlueBalance = -95;
cbLayer.PreserveLuminosity = true;
}
}
im.Save(outputPath);
}

反相调整图层

本文演示了如何使用 Aspose.PSD for .NET 执行反相调整图层。调整图层是一种用于颜色校正的特殊图层。它们不是具有自己内容的图层,而是调整其下图层的信息。反相调整图层通过反转图像的颜色产生照片底片效果。

以下是所述功能的代码演示。

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
var outputPath = dataDir + "InvertStripes_after.psd";
using (var im = (PsdImage)Image.Load(filePath))
{
im.AddInvertAdjustmentLayer();
im.Save(outputPath);
}