'''' This code is part of Document Solutions for Imaging demos.'' Copyright (c) MESCIUS inc. All rights reserved.''ImportsSystem.IOImportsSystem.DrawingImportsSystem.Collections.GenericImportsSystem.LinqImportsSystem.NumericsImportsGrapeCity.Documents.DrawingImportsGrapeCity.Documents.TextImportsGrapeCity.Documents.ImagingImports GCTEXT = GrapeCity.Documents.TextImports GCDRAW = GrapeCity.Documents.Drawing '' This sample demonstrates how to use GrayscaleBitmap.AutoContrast()'' to automatically adjust the contrast of a black and white image.PublicClassAutoContrastFunctionGenerateImage(ByVal pixelSize AsSize,ByVal dpi AsSingle,ByVal opaque AsBoolean,OptionalByVal sampleParams AsString() = Nothing) AsGcBitmap opaque = TrueDim bmp = NewGcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)Using origBmp = NewGcBitmap()'' Load a sample photo:Dim imagePath = Path.Combine("Resources", "ImagesBis", "tremblant.png")Dim grayBmp AsGrayscaleBitmapUsing stm = NewFileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess) origBmp.Load(stm) origBmp.Opaque = opaque grayBmp = origBmp.ToGrayscaleBitmap()EndUsing '' Resize the original photo to fit two versions on the resulting bitmap:Dim w = pixelSize.WidthDim h = pixelSize.Height / 2Using sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic)'' Copy the resized original into the upper half of the resulting bitmap: bmp.BitBlt(sizedBmp, 0, 0)'' Auto adjust the contrast of the original image and copy the result into the lower half: grayBmp.AutoContrast()Using tbmp = grayBmp.ToGcBitmap() bmp.BitBlt(tbmp, 0, h)EndUsingEndUsing '' Add captions (original and adjusted images):Dim lineh = 2Using g = bmp.CreateGraphics(Nothing)Dim foreColor = Color.YellowDim backColor = Color.BlueDim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeMono.ttf")) g.DrawLine(0, h, w * 2, h, NewGCDRAW.Pen(Color.Gray, lineh * 2))Dim tf = NewTextFormat() With {.Font = fnt, .FontSize = 18, .ForeColor = foreColor, .BackColor = backColor, .FontBold = True}Dim th = g.MeasureString("QWERTY", tf).Height g.DrawString(" Original image ", tf, NewPointF(0, h - th + lineh)) g.DrawString(" Auto contrast applied ", tf, NewPointF(0, h * 2 + lineh - th + lineh))EndUsingEndUsingReturn bmpEndFunctionEndClass