受控内存优化!Aspose.Imaging v19.10加载JPEG,CMX和PNG示例演示

近期发布了Aspose.Imaging for .NET v19.10,其中对内存进行了优化,以更好的加载图像。接下来我们用示例为大家演示使用API加载CMX映像时的内存优化用法。

近期发布了Aspose.Imaging for .NET v19.10,其中对内存进行了优化,以更好的加载图像。接下来我们用示例为大家演示使用API加载CMX映像时的内存优化用法。

>>欢迎下载Aspose.Imaging for .NET v19.10体验

*10月狂欢季,限时购买Aspose系列产品,满额即享减免优惠,最高立减10000元,还有惊喜红包享不停!了解详情点击“咨询在线客服”哦~~


有时需要加载大图像,并且计算机上没有足够的可用RAM。 在这种情况下,加载图像时会抛出OutOfMemory异常。 我们在此新API中引入了对受控内存规范的支持。 现在,您可以通过指定缓冲区大小来控制内存加载图像的限制,这将确保内存使用量在这些范围之内。

因此,如果您设置内存优化并且机器RAM内存不足,则与图像相关的操作将开始使用硬盘驱动器。 这将确保以这种方式执行操作而不会出现内存不足异常,但是这将比使用RAM花费更多的时间。

新版本已为JPEG,CMX和PNG等图像提供了受控内存优化技术支持。

以下示例演示了使用API加载CMX映像时的内存优化用法。

// Setting a memory limit of 10 megabytes for target loaded image    using (Image image = Image.Load("example.cmx", new LoadOptions() { BufferSizeHint = 10 }))    {    image.Save(    "output.png",    new PngOptions()    {    VectorRasterizationOptions =    new CmxRasterizationOptions { TextRenderingHint = TextRenderingHint.SingleBitPerPixel, SmoothingMode = SmoothingMode.AntiAlias, Positioning = PositioningTypes.DefinedByDocument}    });    }

同样,以下示例演示了使用API加载JPEG图像时内存优化的用法。

// Setting a memory limit of 50 megabytes for target loaded imageusing (Image image = Image.Load("inputFile.jpg", new LoadOptions() { BufferSizeHint = 50 })) {    image.Save("outputFile_Baseline.jpg", new JpegOptions { CompressionType = JpegCompressionMode.Baseline, Quality = 100 });    image.Save("outputFile_Progressive.jpg", new JpegOptions { CompressionType = JpegCompressionMode.Progressive });    image.Save("outputFile_Lossless.jpg", new JpegOptions                                                  {                                                     ColorType = JpegCompressionColorMode.YCbCr,                                                     CompressionType = JpegCompressionMode.Lossless,                                                     BitsPerChannel = 4                                                  });    image.Save("outputFile_JpegLs.jpg", new JpegOptions                                                  {                                                     ColorType = JpegCompressionColorMode.YCbCr,                                                     CompressionType = JpegCompressionMode.JpegLs,                                                     JpegLsInterleaveMode = JpegLsInterleaveMode.None,                                                     JpegLsAllowedLossyError = 3,                                                     JpegLsPreset = null                                                  });}// Setting a memory limit of 50 megabytes for target created imageImageOptionsBase createOptions = new JpegOptions { CompressionType = JpegCompressionMode.Progressive };createOptions.BufferSizeHint = 50;createOptions.Source = new FileCreateSource("createdFile.jpg", false);using (var image = Image.Create(createOptions, 1000, 1000)) {    image.Save(); // save to same location}

类似的基于Java的实现如下所示:

LoadOptions options = new LoadOptions();options.setBufferSizeHint(50);// Setting a memory limit of 50 megabytes for target loaded imagetry (Image image = Image.load("inputFile.jpg", options)){JpegOptions jpegOptions = new JpegOptions();jpegOptions.setCompressionType(JpegCompressionMode.Baseline);jpegOptions.setQuality(100);image.save("outputFile_Baseline.jpg", jpegOptions);jpegOptions = new JpegOptions();jpegOptions.setCompressionType(JpegCompressionMode.Progressive);image.save("outputFile_Progressive.jpg", jpegOptions);jpegOptions = new JpegOptions();jpegOptions.setCompressionType(JpegCompressionMode.Lossless);jpegOptions.setColorType(JpegCompressionColorMode.YCbCr);jpegOptions.setBitsPerChannel((byte)4);image.save("outputFile_Lossless.jpg", jpegOptions);jpegOptions = new JpegOptions();jpegOptions.setCompressionType(JpegCompressionMode.JpegLs);jpegOptions.setColorType(JpegCompressionColorMode.YCbCr);jpegOptions.setJpegLsInterleaveMode(JpegLsInterleaveMode.None);jpegOptions.setJpegLsAllowedLossyError(3);jpegOptions.setJpegLsPreset(null);image.save("outputFile_JpegLs.jpg", jpegOptions);}// Setting a memory limit of 50 megabytes for target created imagetry (JpegOptions createOptions = new JpegOptions()){createOptions.setCompressionType(JpegCompressionMode.Progressive);createOptions.setBufferSizeHint(50);createOptions.setSource(new FileCreateSource("createdFile.jpg", false));try (Image image = Image.create(createOptions, 1000, 1000)){image.save(); // save to same location}}


ASPOSE技术交流QQ群()已开通,各类资源及时分享,欢迎交流讨论!

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年9月11日
下一篇 2019年9月11日

相关推荐

发表回复

登录后才能评论