//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem.IO;usingSystem.Drawing;usingGrapeCity.Documents.Pdf;usingGrapeCity.Documents.Text;usingGrapeCity.Documents.Drawing;usingGCTEXT = GrapeCity.Documents.Text;usingGCDRAW = GrapeCity.Documents.Drawing; namespaceDsPdfWeb.Demos{// This example shows how SaveAsImageOptions.InterpolationMode affects the result// when a PDF is saved to raster images using SaveAsJpeg or other SaveAsXXX methods.publicclassInterpolationModes {publicintCreatePDF(Stream stream) {// Small image of a QRCode that will be enlarged in PDFs that are then saved as JPEGs:usingvar image = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "QRCode-57x57.png")); // Create 4 PDFs, each is one page on which the small image is enlarged,// save each PDF as JPEG with different interpolation modes specified in SaveAsImageOptions:var tmpNearestNeighbor = Path.GetTempFileName();MakePDF(image, InterpolationMode.NearestNeighbor).SaveAsJpeg(tmpNearestNeighbor, null, newSaveAsImageOptions() { InterpolationMode = InterpolationMode.NearestNeighbor });usingvar imgNearestNeighbor = GCDRAW.Image.FromFile(tmpNearestNeighbor);var tmpLinear = Path.GetTempFileName();MakePDF(image, InterpolationMode.Linear).SaveAsJpeg(tmpLinear, null,newSaveAsImageOptions() { InterpolationMode = InterpolationMode.Linear });usingvar imgLinear = GCDRAW.Image.FromFile(tmpLinear);var tmpCubic = Path.GetTempFileName();MakePDF(image, InterpolationMode.Cubic).SaveAsJpeg(tmpCubic, null,newSaveAsImageOptions() { InterpolationMode = InterpolationMode.Cubic });usingvar imgCubic = GCDRAW.Image.FromFile(tmpCubic);var tmpDownscale = Path.GetTempFileName();MakePDF(image, InterpolationMode.Downscale).SaveAsJpeg(tmpDownscale, null,newSaveAsImageOptions() { InterpolationMode = InterpolationMode.Downscale });usingvar imgDownscale = GCDRAW.Image.FromFile(tmpDownscale); // Draw each JPEG at its natural size onto a page of the resulting PDF:var doc = newGcPdfDocument();var page = doc.NewPage(); page.Graphics.DrawImage(imgNearestNeighbor, page.Bounds, null, ImageAlign.Default); page = doc.NewPage(); page.Graphics.DrawImage(imgLinear, page.Bounds, null, ImageAlign.Default); page = doc.NewPage(); page.Graphics.DrawImage(imgCubic, page.Bounds, null, ImageAlign.Default); page = doc.NewPage(); page.Graphics.DrawImage(imgDownscale, page.Bounds, null, ImageAlign.Default);// Save the resulting PDF: doc.Save(stream);// CLeanup:File.Delete(tmpNearestNeighbor);File.Delete(tmpLinear);File.Delete(tmpCubic);File.Delete(tmpDownscale); // Done:return doc.Pages.Count; } // InterpolationMode parameter here is only to show in the resulting PDF,// GcPdfGraphics only supports InterpolationMode.NearestNeighbor:publicGcPdfDocumentMakePDF(GCDRAW.Image image, InterpolationMode imode) {var tfCaption = newTextFormat {Font = StandardFonts.Times,FontSize = 20, };var tf = newTextFormat {Font = StandardFonts.Times,FontSize = 18, }; var doc = newGcPdfDocument();var page = doc.NewPage();var g = page.Graphics; var xpad = 36;var ypad = 36;var ip = newPointF(xpad, ypad); g.DrawString($"SaveAsImageOptions.InterpolationMode is {imode}", tfCaption, ip); ip.Y *= 2.5f; // Draw the original with the original size: g.DrawImage(image, newRectangleF(ip.X, ip.Y, image.Width, image.Height), null, ImageAlign.Default); g.DrawString($"⟵ Original image ({image.Width} by {image.Height} pixels)", tf, newPointF(ip.X * 2 + image.Width, ip.Y)); ip.Y += image.Height + ypad; // Enlarge the original small image by a factor of 8:var f = 8;int twidth = image.Width * f;int theight = image.Height * f; // Draw the enlarged image: g.DrawImage(image, newRectangleF(ip.X, ip.Y, twidth, theight), null, ImageAlign.StretchImage); g.DrawString($"Image enlarged to {twidth} by {theight} pixels:", tf, newPointF(ip.X, ip.Y - ypad)); return doc; } }}