Skip to content

Instantly share code, notes, and snippets.

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/fe98652f3eac2dcb6eca9da8f31bff01 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/fe98652f3eac2dcb6eca9da8f31bff01 to your computer and use it in GitHub Desktop.

Aspose.Imaging .NET API allows to easily manipulate cache for loading/saving your images or photos in your .NET application or Web service.

You can:

  • Manipulate cache for loading/saving images.

Interested ?

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

using Aspose.Imaging;
using Aspose.Imaging.Brushes;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Load an existing JPG image
using (JpegImage image = (JpegImage)Image.Load(dataDir + "template.jpg"))
{
StreamSource rgbprofile = new StreamSource(File.OpenRead(dataDir + "iccprofiles\\eciRGB_v2.icc"));
StreamSource cmykprofile = new StreamSource(File.OpenRead(dataDir + "iccprofiles\\ISOcoated_v2_FullGamut4.icc"));
image.DestinationRgbColorProfile = rgbprofile;
image.DestinationCmykColorProfile = cmykprofile;
image.Save(dataDir + "result.jpg");
}
File.Delete(dataDir + "result.jpg");
using Aspose.Imaging;
using Aspose.Imaging.Brushes;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Load an existing JPG image
using (JpegImage image = (JpegImage)Image.Load(dataDir + "template.jpg"))
{
StreamSource rgbprofile = new StreamSource(File.OpenRead(dataDir + "iccprofiles\\eciRGB_v2.icc"));
StreamSource cmykprofile = new StreamSource(File.OpenRead(dataDir + "iccprofiles\\ISOcoated_v2_FullGamut4.icc"));
image.RgbColorProfile = rgbprofile;
image.CmykColorProfile = cmykprofile;
Color[] colors = image.LoadPixels(new Rectangle(0, 0, image.Width, image.Height));
}
using Aspose.Imaging;
using Aspose.Imaging.Brushes;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
string cacheFolder = dataDir + @"temp\";
if (!Directory.Exists(cacheFolder))
{
Directory.CreateDirectory(cacheFolder);
}
// By default the cache folder is set to the local temp directory. You can specify a different cache folder from the default this way:
Cache.CacheFolder = cacheFolder;
// Auto mode is flexible and efficient
Cache.CacheType = CacheType.Auto;
// The default cache max value is 0, which means that there is no upper limit
Cache.MaxDiskSpaceForCache = 1073741824; // 1 gigabyte
Cache.MaxMemoryForCache = 1073741824; // 1 gigabyte
// We do not recommend that you change the following property because it may greatly affect performance
Cache.ExactReallocateOnly = false;
// At any time you can check how many bytes are currently allocated for the cache in memory or on disk By examining the following properties
long l1 = Cache.AllocatedDiskBytesCount;
long l2 = Cache.AllocatedMemoryBytesCount;
GifOptions options = new GifOptions();
options.Palette = new ColorPalette(new[] { Color.Red, Color.Blue, Color.Black, Color.White });
options.Source = new StreamSource(new MemoryStream(), true);
using (RasterImage image = (RasterImage)Image.Create(options, 100, 100))
{
Color[] pixels = new Color[10000];
for (int i = 0; i < pixels.Length; i++)
{
pixels[i] = Color.White;
}
image.SavePixels(image.Bounds, pixels);
// After executing the code above 40000 bytes are allocated to memory.
long diskBytes = Cache.AllocatedDiskBytesCount;
long memoryBytes = Cache.AllocatedMemoryBytesCount;
}
// The allocation properties may be used to check whether all Aspose.Imaging objects were properly disposed. If you've forgotten to call dispose on an object the cache values will not be 0.
l1 = Cache.AllocatedDiskBytesCount;
l2 = Cache.AllocatedMemoryBytesCount;
Cache.CacheType = CacheType.Auto;
Directory.Delete(cacheFolder);
using Aspose.Imaging;
using Aspose.Imaging.Brushes;
using Aspose.Imaging.CoreExceptions;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Multithreading;
using Aspose.Imaging.Sources;
using System;
using System.IO;
using System.Threading;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
ImageOptionsBase saveOptions = new PngOptions();
InterruptMonitor monitor = new InterruptMonitor();
SaveImageWorker worker = new SaveImageWorker(dataDir + "template.jpg", dataDir + "result.png", saveOptions, monitor);
Thread thread = new Thread(new ThreadStart(worker.ThreadProc));
try
{
thread.Start();
// The timeout should be less than the time required for full image conversion (without interruption).
Thread.Sleep(3000);
// Interrupt the process
monitor.Interrupt();
Console.WriteLine("Interrupting the save thread #{0} at {1}", thread.ManagedThreadId, System.DateTime.Now);
// Wait for interruption...
thread.Join();
}
finally
{
// If the file to be deleted does not exist, no exception is thrown.
File.Delete(dataDir + "result.png");
}
/// <summary>
/// Initiates image conversion and waits for its interruption.
/// </summary>
class SaveImageWorker
{
/// <summary>
/// The path to the input image.
/// </summary>
private readonly string inputPath;
/// <summary>
/// The path to the output image.
/// </summary>
private readonly string outputPath;
/// <summary>
/// The interrupt monitor.
/// </summary>
private readonly InterruptMonitor monitor;
/// <summary>
/// The save options.
/// </summary>
private readonly ImageOptionsBase saveOptions;
/// <summary>
/// Initializes a new instance of the <see cref="SaveImageWorker" /> class.
/// </summary>
/// <param name="inputPath">The path to the input image.</param>
/// <param name="outputPath">The path to the output image.</param>
/// <param name="saveOptions">The save options.</param>
/// <param name="monitor">The interrupt monitor.</param>
public SaveImageWorker(string inputPath, string outputPath, ImageOptionsBase saveOptions, InterruptMonitor monitor)
{
this.inputPath = inputPath;
this.outputPath = outputPath;
this.saveOptions = saveOptions;
this.monitor = monitor;
}
/// <summary>
/// Tries to convert image from one format to another. Handles interruption.
/// </summary>
public void ThreadProc()
{
using (Image image = Image.Load(this.inputPath))
{
InterruptMonitor.ThreadLocalInstance = this.monitor;
try
{
image.Save(this.outputPath, this.saveOptions);
}
catch (OperationInterruptedException e)
{
Console.WriteLine("The save thread #{0} finishes at {1}", Thread.CurrentThread.ManagedThreadId, DateTime.Now);
Console.WriteLine(e);
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
InterruptMonitor.ThreadLocalInstance = null;
}
}
}
}
using Aspose.Imaging;
using Aspose.Imaging.Brushes;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// First convert the image to raw PSD format.
using (PngImage pngImage = (PngImage)Image.Load(dataDir + "template.png"))
{
PngOptions saveOptions = new PngOptions();
pngImage.Save(dataDir + "result.png", saveOptions);
}
// Now reopen the newly created image.
using (PngImage pngImage = (PngImage)Image.Load(dataDir + "result.png"))
{
Graphics graphics = new Graphics(pngImage);
// Perform graphics operations.
}
File.Delete(dataDir + "result.png");
using Aspose.Imaging;
using Aspose.Imaging.Brushes;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Png;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Create an instance of MemoryStream to hold the uncompressed image data.
using (MemoryStream stream = new MemoryStream())
{
// First convert the image to raw PSD format.
using (PngImage pngImage = (PngImage)Image.Load(dataDir + "template.png"))
{
PngOptions saveOptions = new PngOptions();
pngImage.Save(dataDir + "result.png", saveOptions);
}
// Now reopen the newly created image.
using (PngImage pngImage = (PngImage)Image.Load(dataDir + "result.png"))
{
Graphics graphics = new Graphics(pngImage);
// Perform graphics operations.
}
}
File.Delete(dataDir + "result.png");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment