CreateThumbnails.cs
C# VB
//// This code is part of Document Solutions for Imaging demos.// Copyright (c) MESCIUS inc. All rights reserved.//using System;using System.IO;using System.Drawing;using GrapeCity.Documents.Drawing;using GrapeCity.Documents.Text;using GrapeCity.Documents.Imaging;using GCTEXT = GrapeCity.Documents.Text;
namespace DsImagingWeb.Demos{ // This sample demonstrates how to resize (downscale) an existing image // to create a thumbnail, using different interpolation modes. public class DownscaleImage { public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null) { var origImagePath = Path.Combine("Resources", "Images", "minerva.jpg"); // Create and fill the target bitmap: var targetBmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi); using (var g = targetBmp.CreateGraphics(Color.FromArgb(unchecked((int)0xff004d99)))) { // creating a graphics with a fill color does what we need } const int fontSize = 16, fpad = 4, xpad = 10, ypad = 3; TextFormat tf = new TextFormat { ForeColor = Color.FromArgb(unchecked((int)0xffffcc00)), Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSerif.ttf")), FontSize = fontSize, }; using (var origBmp = new GcBitmap()) { using (var stm = new FileStream(origImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess)) origBmp.Load(stm);
// Bitwise copy the original image with the original size: targetBmp.BitBlt(origBmp, 0, 0); using (var g = targetBmp.CreateGraphics(null)) g.DrawString($"Original image ({origBmp.Width} by {origBmp.Height} pixels)", tf, new PointF(fpad, origBmp.Height + fpad)); int dx = (int)origBmp.Width + xpad;
// Scale down the image using the various interpolation modes, // and render those on the right of the original: float f = (origBmp.Height - ypad * 3) / origBmp.Height / 4; int twidth = (int)Math.Round(origBmp.Width * f); int theight = (int)Math.Round(origBmp.Height * f);
using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.NearestNeighbor)) targetBmp.BitBlt(bmp, dx, 0); drawCaption("InterpolationMode.NearestNeighbor", dx, 0);
using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.Linear)) targetBmp.BitBlt(bmp, dx, theight + ypad); drawCaption("InterpolationMode.Linear", dx, theight + ypad);
using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.Cubic)) targetBmp.BitBlt(bmp, dx, (theight + ypad) * 2); drawCaption("InterpolationMode.Cubic", dx, (theight + ypad) * 2);
using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.Downscale)) targetBmp.BitBlt(bmp, dx, (theight + ypad) * 3); drawCaption("InterpolationMode.Downscale", dx, (theight + ypad) * 3); // void drawCaption(string caption, float x, float y) { using (var g = targetBmp.CreateGraphics(null)) g.DrawString(caption, tf, new PointF(x + twidth + fpad, y + fpad)); } } return targetBmp; } }}