Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active February 22, 2023 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/0dc6787183a1823ca0774be84c7b7aa2 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/0dc6787183a1823ca0774be84c7b7aa2 to your computer and use it in GitHub Desktop.

Aspose.Imaging .NET API allows to easy manipulate your png images or photos in your .NET application or Web service.

You can:

  • Convert png image to raster formats;
  • Create png image;
  • Convert png image to vector formats;
  • Compress png images;
  • Specify bit depth of png image;
  • Operate with transparent png images;
  • Perform crop,rotate,resize and other typical operations to png images.

Interested ?

You may go further at : https://products.aspose.com/imaging/net/

using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.ImageOptions;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
using (PngImage png = (PngImage)Image.Load(dataDir + "template.png"))
{
// Create an instance of PngOptions, Set the PNG filter method and Save changes to the disc
PngOptions options = new PngOptions();
options.FilterType = PngFilterType.Paeth;
png.Save(dataDir + "result.png", options);
}
File.Delete(dataDir + "result.png");
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.ImageOptions;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Create an instance of Image class and load a PNG image
using (Image img = Image.Load(dataDir + "template.png"))
{
// Create an instance of RasterImage and get the pixels array by calling method LoadArgb32Pixels.
RasterImage rasterImg = img as RasterImage;
if (rasterImg != null)
{
int[] pixels = rasterImg.LoadArgb32Pixels(img.Bounds);
if (pixels != null)
{
// Iterate through the pixel array and Check the pixel information that if it is a transparent color pixel and Change the pixel color to white
for (int i = 0; i < pixels.Length; i++)
{
if (pixels[i] == rasterImg.TransparentColor.ToArgb())
{
pixels[i] = Color.White.ToArgb();
}
}
// Replace the pixel array into the image.
rasterImg.SaveArgb32Pixels(img.Bounds, pixels);
}
}
// Save the updated image to disk.
if (rasterImg != null)
rasterImg.Save(dataDir + "result.png");
}
File.Delete(dataDir + "result.png");
using Aspose.Imaging;
using Aspose.Imaging.Brushes;
using Aspose.Imaging.FileFormats.Bmp;
using Aspose.Imaging.FileFormats.Core.VectorPaths;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Emf.Emf.Consts;
using Aspose.Imaging.FileFormats.Emf.Graphics;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.FileFormats.Tiff.PathResources;
using Aspose.Imaging.FileFormats.Webp;
using Aspose.Imaging.FileFormats.Wmf;
using Aspose.Imaging.FileFormats.Wmf.Graphics;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
string fileName = Path.Combine(dataDir, "template.tiff");
// Load an image from file (or stream)
using (Image image = Image.Load(dataDir + "template.png"))
{
// Loop over possible CompressionLevel range
for (int i = 0; i <= 9; i++)
{
// Create an instance of PngOptions for each resultant PNG, Set CompressionLevel and Save result on disk
PngOptions options = new PngOptions();
options.CompressionLevel = i;
image.Save(dataDir + i + "_out.png", options);
File.Delete(dataDir + i + "_out.png");
}
}
using Aspose.Imaging;
using Aspose.Imaging.Brushes;
using Aspose.Imaging.FileFormats.Bmp;
using Aspose.Imaging.FileFormats.Core.VectorPaths;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Emf.Emf.Consts;
using Aspose.Imaging.FileFormats.Emf.Graphics;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.FileFormats.Tiff.PathResources;
using Aspose.Imaging.FileFormats.Webp;
using Aspose.Imaging.FileFormats.Wmf;
using Aspose.Imaging.FileFormats.Wmf.Graphics;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
string fileName = Path.Combine(dataDir, "template.tiff");
using (RasterImage image = (RasterImage)Image.Load(dataDir + "template.png"))
{
PngOptions options = new PngOptions()
{
CompressionLevel = 9,
ColorType = PngColorType.IndexedColor,
Palette = ColorPaletteHelper.GetCloseTransparentImagePalette(image, 256),
FilterType = PngFilterType.Avg,
};
image.Save(dataDir + "result.png", options);
}
File.Delete(dataDir + "result.png");
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.ImageOptions;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Enabling the global memory usage optimization strategy.
// Setting a global memory limit of 50 megabytes
Aspose.Imaging.MemoryManagement.Configuration.BufferSizeHint = 50;
using (var image = Image.Load(dataDir + "template.png"))
{
image.Save(dataDir + "result.jpg", new Aspose.Imaging.ImageOptions.JpegOptions());
}
File.Delete(dataDir + "result.jpg");
using Aspose.Imaging;
using Aspose.Imaging.Brushes;
using Aspose.Imaging.FileFormats.Bmp;
using Aspose.Imaging.FileFormats.Core.VectorPaths;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Emf.Emf.Consts;
using Aspose.Imaging.FileFormats.Emf.Graphics;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.FileFormats.Tiff.PathResources;
using Aspose.Imaging.FileFormats.Webp;
using Aspose.Imaging.FileFormats.Wmf;
using Aspose.Imaging.FileFormats.Wmf.Graphics;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
string fileName = Path.Combine(dataDir, "template.tiff");
// Initialize variables to hold width & height values
int width = 0;
int height = 0;
// Initialize an array of type Color to hold the pixel data
Color[] pixels = null;
// Create an instance of RasterImage and load a BMP image
using (RasterImage raster = (RasterImage)Image.Load(dataDir + "template.png"))
{
// Store the width & height in variables for later use
width = raster.Width;
height = raster.Height;
// Load the pixels of RasterImage into the array of type Color
pixels = raster.LoadPixels(new Rectangle(0, 0, width, height));
}
// Create & initialize an instance of PngImage while specifying size and PngColorType
using (PngImage png = new PngImage(width, height))
{
// Save the previously loaded pixels on to the new PngImage
png.SavePixels(new Rectangle(0, 0, width, height), pixels);
// Create an instance of PngOptions, Set the horizontal & vertical resolutions and Save the result on disc
PngOptions options = new PngOptions();
options.ResolutionSettings = new ResolutionSetting(72, 96);
png.Save(dataDir + "result.png", options);
}
File.Delete(dataDir + "result.png");
using Aspose.Imaging;
using Aspose.Imaging.Brushes;
using Aspose.Imaging.FileFormats.Bmp;
using Aspose.Imaging.FileFormats.Core.VectorPaths;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Emf.Emf.Consts;
using Aspose.Imaging.FileFormats.Emf.Graphics;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.FileFormats.Tiff.PathResources;
using Aspose.Imaging.FileFormats.Webp;
using Aspose.Imaging.FileFormats.Wmf;
using Aspose.Imaging.FileFormats.Wmf.Graphics;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
string fileName = Path.Combine(dataDir, "template.tiff");
// Load an existing PNG image
using (PngImage png = (PngImage)Image.Load(dataDir + "template.png"))
{
// Create an instance of PngOptions, Set the desired ColorType, BitDepth according to the specified ColorType and save image
PngOptions options = new PngOptions();
options.ColorType = PngColorType.Grayscale;
options.BitDepth = 1;
png.Save(dataDir + "result.png", options);
}
File.Delete(dataDir + "result.png");
using Aspose.Imaging;
using Aspose.Imaging.Brushes;
using Aspose.Imaging.FileFormats.Bmp;
using Aspose.Imaging.FileFormats.Core.VectorPaths;
using Aspose.Imaging.FileFormats.Emf;
using Aspose.Imaging.FileFormats.Emf.Emf.Consts;
using Aspose.Imaging.FileFormats.Emf.Graphics;
using Aspose.Imaging.FileFormats.Gif;
using Aspose.Imaging.FileFormats.Gif.Blocks;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.FileFormats.Tiff.PathResources;
using Aspose.Imaging.FileFormats.Webp;
using Aspose.Imaging.FileFormats.Wmf;
using Aspose.Imaging.FileFormats.Wmf.Graphics;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
string fileName = Path.Combine(dataDir, "template.tiff");
// Initialize variables to hold width & height values
int width = 0;
int height = 0;
// Initialize an array of type Color to hold the pixel data
Color[] pixels = null;
// Create an instance of RasterImage and load a BMP image
using (RasterImage raster = (RasterImage)Image.Load(dataDir + "template.png"))
{
// Store the width & height in variables for later use
width = raster.Width;
height = raster.Height;
// Load the pixels of RasterImage into the array of type Color
pixels = raster.LoadPixels(new Rectangle(0, 0, width, height));
}
// Create & initialize an instance of PngImage while specifying size and PngColorType
using (PngImage png = new PngImage(width, height, PngColorType.TruecolorWithAlpha))
{
// Save the previously loaded pixels on to the new PngImage and Set TransparentColor property to specify which color to be rendered as transparent
png.SavePixels(new Rectangle(0, 0, width, height), pixels);
png.TransparentColor = Color.Black;
png.HasTransparentColor = true;
png.Save(dataDir + "result.jpg");
}
File.Delete(dataDir + "result.jpg");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment