What's New

User Manual
1. Introduction
2. Thumbnails
3. Using AspUpload
4. Image Processing
5. Drawing & Typing
6. Picture-in-Picture
7. Metadata
8. Miscellaneous
9. GIF Output

Object Reference
Live Demos

Download & Buy

Other Products
Contact Us

Search this site:

Chapter 5: Drawing and Typing Chapter 3: Working with AspUpload Chapter 4: Image Manipulation

4.1 Resizing Algorithms
4.2 Image Sharpening
4.3 Image Cropping
4.4 Image Flipping and Rotation
4.5 Adjusting Image Compression
4.6 Grayscale Conversion
4.7 Sepia Filter
4.8 Code Sample

4.1 Resizing Algorithms

AspJpeg supports three popular image resizing algorithms: Nearest-Neighbor, Bilinear and Bicubic.

The Nearest-Neighbor algorithm is the fastest of the three, but produces relatively low-quality thumbnails. The Bilinear algorithm offers a much better quality but is about twice as slow as Nearest-Neighbor. And finally, the Bicubic algorithm provides the highest quality but is approximately twice as slow as Bilinear and, therefore, 4 times as slow as Nearest-Neighbor.

A resizing algorithm for your thumbnails can be specified via the property Jpeg.Interpolation. Valid values for this property are: 0 (Nearest-Neighbor), 1 (Bilinear), and 2 (Bicubic). The default value is 1 (Bilinear). For this property to take effect, it must be set before calling Save, SendBinary or Binary.

The following table illustrates the effect of setting the Interpolation property. Notice how much smoother the thumbnails produced by the Bilinear and Bicubic algorithms are in comparison to Nearest-Neighbor. Notice also that in many cases (such as this one) Bicubic provides little, if any, improvement over Bilinear.

Nearest Neighbor (0)
Bilinear (1)
Bicubic (2)

4.2 Image Sharpening

Starting with Version 1.1, AspJpeg is capable of applying a sharpening filter to an image being resized via the method Sharpen. A regular thumbnail and two thumbnails with various degrees of sharpening applied to them are shown below.

No sharpeningSharpen(1, 120)Sharpen(1, 250)

The Sharpen method uses two Double arguments: Radius and Amount. Radius controls the size (in pixels) of an area around every pixel that the sharpening algorithm examines. This argument should normally be set to 1 or 2. Amount (expressed in %) specifies the degree of sharpness. This argument must be greater than 100.

For this property to take effect, it must be set before calling Save, SendBinary or Binary.

4.3 Image Cropping

AspJpeg 1.1+ is also capable of cutting off edges from, or cropping, the resultant thumbnails via the method Crop(x0, y0, x1, y1). The size of the cropped image is specified by the coordinates of the upper-left and lower-right corners within the resultant thumbnail, not the original large image.

If one or more coordinates passeed to the Crop method are outside the coordinate space of the image, this will actually expand the "canvas" around the image. This is useful, for example, if you need to create margins around the image. The following code creates a 10-pixel margin around an image:

jpeg.Crop -10, -10, jpeg.Width + 10, jpeg.Height + 10

IMPORTANT: Before version 1.7, the color of the margins created by "negative" cropping was always white. Starting with version 1.7, it is determined by the color specified via Canvas.Brush.Color, and is black by default. To create a white 10-pixel margin with version 1.7+, the following code should be used:

jpeg.Canvas.Brush.Color = &HFFFFFF
jpeg.Crop -10, -10, jpeg.Width + 10, jpeg.Height + 10

4.4 Image Flipping and Rotation

With AspJpeg 1.2+, you can invert an image horizontally and/or vertically by calling the methods FlipH and FlipV, respectively.

You can also rotate an image 90 degrees clockwise and counter-clockwise by calling the methods RotateR and RotateL, respectively.

4.5 Adjusting Image Compression

The JPEG format uses "lossy" compression methods. This means that some minor details of an image saved as a JPEG are lost during compression. The degree of loss can be adjusted via the Jpeg.Quality property. This property accepts an integer in the range 0 to 100, with 0 being the highest degree of loss (and hence, the lowest quality) and 100 being the lowest degree of loss and highest quality.

The lower the loss, the larger the resultant file size. The property Jpeg.Quality is set to 80 by default which provides a close-to-optimal combination of quality and file size.

4.6 Grayscale Conversion

Starting with version 1.4, AspJpeg is capable of converting a color image to grayscale via the Grayscale method. This method expects a Method argument which specifies a formula to perform the color-to-B&W conversion. The valid values are 0, 1, and 2. The value of 1 is the recommended method for most applications.

The Grayscale method sets the three color components (R, G, B) of each pixel to the same value L using the following formulas:

0L = 0.3333 R + 0.3333 G + 0.3333 B
1L = 0.2990 R + 0.5870 G + 0.1140 B
2L = 0.2125 R + 0.7154 G + 0.0721 B

The effect of the Method argument is demonstrated by the chart below:


The effect of Method 0 is what Photoshop calls desaturation (Image/Adjust/Desaturate), while Method 1 is similar to Photoshop's conversion from RGB to grayscale (Image/Mode/Grayscale).

Note that Grayscale is a method, not a property, so the '=' sign should not be used:

Jpeg.Grayscale 1

4.7 Sepia Filter

AspJpeg 1.6+ offers the Sepia method which makes an image look like an old photograph. This method's two parameters, Hue and Contrast, enable you to adjust the output to your taste.

The Hue parameter controls the brownish hue of the output image, and should usually be in the range of 25 to 60 for good results. The Contrast parameter controls the contrast of the image. The value of 1 means no contrast adjustment. Values between 1.2 and 1.5 usually produce good results.

Original ImageHue=50, Contrast=1.4

The sample Sepia conversion shown here was achieved as follows:

Jpeg.Sepia 50, 1.4

4.8 Code Sample

The following code sample demonstrates most of the above mentioned features by interactively applying various transformations to an image. The file 04_params.asp/aspx contains a form with checkboxes and radio buttons controlling the visual appearance of the image. This file invokes the script 04_process.asp/aspx which contains the actual image modification routine shown below.

VB Script:
Set Jpeg = Server.CreateObject("Persits.Jpeg")
Jpeg.Open Server.MapPath("clock.jpg")

Jpeg.Width = Jpeg.OriginalWidth * .8
Jpeg.Height = Jpeg.OriginalHeight * .8

If Request("Grayscale") = "1" Then
   Jpeg.Grayscale 1
End If

If Request("Sharpen") = "1" Then
   Jpeg.Sharpen 1, 250
End If

If Request("Horflip") = "1" Then
End If

If Request("Verflip") = "1" Then
End If

Jpeg.Quality = Request("Quality")
Jpeg.Interpolation = Request("Interpolation")

If Request("Crop") = 1 Then
   Jpeg.Crop 30, 30, 470, 320
End If


<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Import Namespace="ASPJPEGLib" %>
<%@ Page aspCompat="True" Language="C#" Debug="true" %>

<script runat="server" LANGUAGE="C#">

void Page_Load(Object Source, EventArgs E)
ASPJPEGLib.IASPJpeg objJpeg;
objJpeg = new ASPJPEGLib.ASPJpeg();

objJpeg.Open( Server.MapPath("clock.jpg") );

objJpeg.Width = (int)(objJpeg.OriginalWidth * 0.8);
objJpeg.Height = (int)(objJpeg.OriginalHeight * 0.8);

if( Request["Grayscale"] == "1" )
objJpeg.Grayscale( 1 );

if( Request["Sharpen"] == "1" )
objJpeg.Sharpen( 1, 250 );

if( Request["Horflip"] == "1" )

if( Request["Verflip"] == "1" )

objJpeg.Quality = int.Parse(Request["Quality"]);

objJpeg.Interpolation = int.Parse(Request["Interpolation"]);

if( Request["Crop"] == "1" )
objJpeg.Crop( 30, 30, 470, 320 );



Click the links below to run this code sample:

http://localhost/aspjpeg/manual_04/04_params.aspx  Why is this link not working?

Chapter 5: Drawing and Typing Chapter 3: Working with AspUpload 

Copyright © 1998 - 2007 Persits Software, Inc.
All Rights Reserved.
AspJpeg is a trademark of Persits Software, Inc.