AspJpeg 1.3+ enables you to place images on top of each other via the method
DrawImage. To use this method, you must create two instances of the
AspJpeg objects and populate both of them with images via calls to Open (or OpenBinary).
When calling Canvas.DrawImage, the 2nd instance of AspJpeg
is passed as an argument to this method, along with the X and Y offsets (in pixels):
Set Jpeg1 = Server.CreateObject("Persits.Jpeg")
Set Jpeg2 = Server.CreateObject("Persits.Jpeg")
Jpeg1.Open Path1
Jpeg2.Open Path2
...
Jpeg1.Canvas.DrawImage 10, 10, Jpeg2 ' optional arguments omitted
The following code sample creates a "front page" for an online album
which contains the album title (aligned to the center) and up to 3 thumbnails
of the images read from a folder.
To create a blank image, the New method is used which accepts
the image dimensions and background color as arguments.
To align a text string to the right, the method Canvas.GetTextExtent is used.
VB Script:
<%
' Directory with images
Path = Server.MapPath("../images")
' Album Title
Title = "My Favorite Photographs"
Set Jpeg = Server.CreateObject("Persits.Jpeg")
' Create a "blank" image
Jpeg.New 380, 150, &HFFFFFF
' Draw 1-pixel blue frame
Jpeg.Canvas.Pen.Color = &H000080 ' Blue
Jpeg.Canvas.Brush.Solid = False ' to avoid solid bar
Jpeg.Canvas.DrawBar 1, 1, Jpeg.Width, Jpeg.Height
' Set font options
Jpeg.Canvas.Font.Color = &H000000 ' black
Jpeg.Canvas.Font.Family = "Helvetica"
Jpeg.Canvas.Font.Bold = True
Jpeg.Canvas.Font.Size = 15
Jpeg.Canvas.Font.Quality = 4 ' antialiased
Jpeg.Canvas.Font.BkMode = "Opaque"
' Draw album title centered
TitleWidth = Jpeg.Canvas.GetTextExtent( Title )
Jpeg.Canvas.Print (Jpeg.Width - TitleWidth) / 2, 13, Title
' Read images from Images directory of the installation
Dim FileNames(3)
FileNames(0) = "apple.jpg"
FileNames(1) = "clock.jpg"
FileNames(2) = "photo.jpg"
Count = 0
While Count < 3
' Draw this image on front page
Set Img = Server.CreateObject("Persits.Jpeg")
Img.Open Path & "\" & FileNames(Count)
' Resize to inscribe in 100x100 square
Img.PreserveAspectRatio = True
If Img.OriginalWidth > 100 or Img.OriginalHeight > 100 Then
If Img.OriginalWidth > Img.OriginalHeight Then
Img.Width = 100
Else
Img.Height = 100
End If
End If
X = 20 + 120 * Count
Y = 40
' Draw frame for each thumbnail
Jpeg.Canvas.DrawBar X - 1, Y - 1, X + 101, Y + 101
' center image inside frame vert or horiz as needed
Jpeg.Canvas.DrawImage X + (100 - Img.Width)/2, Y + (100 - Img.Height)/2, Img
Count = Count + 1
Wend
Jpeg.Save Server.MapPath("frontpage.jpg")
%>
|
C#:
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="ASPJPEGLib" %>
<script runat="server" LANGUAGE="C#">
void Page_Load(Object Source, EventArgs E)
{
// Source directory with images
String strPath = Server.MapPath("../images");
// Album Title
String strTitle = "My Favorite Photographs";
IASPJpeg objJpeg;
objJpeg = new ASPJpeg();
// Create a "blank" image for the front page, white background
objJpeg.New( 380, 150, 0xFFFFFF );
// Draw 1-pixel blue frame
objJpeg.Canvas.Pen.Color = 0x000080; // Blue
objJpeg.Canvas.Brush.Solid = 0; // or a solid bar would be drawn
objJpeg.Canvas.DrawBar( 1, 1, objJpeg.Width, objJpeg.Height );
// Set font options
objJpeg.Canvas.Font.Color = 0x000000; // black
objJpeg.Canvas.Font.Family = "Helvetica";
objJpeg.Canvas.Font.Bold = 1;
objJpeg.Canvas.Font.Size = 15;
objJpeg.Canvas.Font.Quality = 4; // antialiased
objJpeg.Canvas.Font.BkMode = "Opaque"; // for antialiasing
// Draw album title centered
int TitleWidth = objJpeg.Canvas.GetTextExtent( strTitle, Missing.Value );
objJpeg.Canvas.Print( (objJpeg.Width - TitleWidth) / 2, 13,
strTitle, Missing.Value );
// Enumerate images in the source directory
String[] files = Directory.GetFiles( strPath, "*.*");
int nCount = 0;
foreach( String strFile in files )
{
String strExt =Path.GetExtension( strFile ).ToUpper();
if( strExt != ".JPG" && strExt != ".GIF" && strExt != ".TIF" )
continue;
// Draw this image on front page
IASPJpeg objImg = new ASPJpeg();
objImg.Open( strFile );
// Resize to inscribe in 100x100 square
objImg.PreserveAspectRatio = 1;
if(objImg.OriginalWidth > 100 || objImg.OriginalHeight > 100)
{
if( objImg.OriginalWidth > objImg.OriginalHeight )
{
objImg.Width = 100;
}
else
{
objImg.Height = 100;
}
}
int X = 20 + 120 * nCount;
int Y = 40;
// Draw frame for each thumbnail
objJpeg.Canvas.DrawBar( X - 1, Y - 1, X + 101, Y + 101 );
// Center image inside frame vert or horiz as needed
objJpeg.Canvas.DrawImage( X + (100 - objImg.Width) / 2, Y + (100 - objImg.Height) / 2, (ASPJpeg)objImg, Missing.Value, Missing.Value, Missing.Value );
nCount++;
if( nCount >= 3 )
break;
}
objJpeg.Save( Server.MapPath("frontpage.jpg") );
FramedImage.Src = "frontpage.jpg";
}
</script>
|
(Note: starting with Version 1.8, the code sample 06_frontpage.asp is re-written
to not use Microsoft FileSystemObject, as this object may
cause IIS to hang.)
Click the links below to run this code sample:
http://localhost/aspjpeg/manual_06/06_frontpage.asp
http://localhost/aspjpeg/manual_06/06_frontpage.aspx
6.4.1 Opacity & Transparency
The DrawImage method also provides three optional arguments to specify opacity and
transparency parameters.
By default, an image being embedded is fully opaque (non-transparent).
Opacity can be changed via the fourth optional argument which
must be a number in the range 0 to 1. The value of 0 means the image is
to be displayed fully transparent (invisible), and 1
fully opaque.
The following code embeds an image at 50% opacity:
Photo.Canvas.DrawImage 50, 20, Logo, 0.5
Certain pixels of an image can be made transparent when
this image is placed on top of another image. The color
of pixels to be made transparent is specified via the fifth argument of the DrawImage method.
The following code makes all red pixels of an image transparent
when displayed on top of another image:
Photo.Canvas.DrawImage 50, 20, Logo, , &HFF0000 ' opacity arg omitted
Since JPEG uses lossy compression, pixel colors often get slightly
distorted (although it may not be visible to the human eye).
For example, one or all color components of a pixel may change from 255 (FF)
to 254 during JPEG compression. To accommodate for this
deficiency, the DrawImage method provides yet another argument,
an allowable deviation from the color specified by argument 5.
This number must be between 0 and 255 and affects all three color components
of a pixel. For example, the following code turns transparent all pixels
in the range &H16D636 to &H2AEA4A:
Photo.Canvas.DrawImage 50, 20, Logo, , &H20E040, 10