5.3 画中画Picture-in-Picture
AspJpeg 1.3以上版本能够通过DrawImage方法让你把若干张图片放到各自对方的上面。为了使用这种方法,你必须创建两个AspJpeg实例,并通过调用Open或者OpenBinary方法把它们移合到一起。当调用Canvas.DrawImage方法时,第二个AspJpeg实例被作为这个方法的一个算法,根据X和Y的位移(以象素为单位)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
以下代码样例为一个在线相册创建了一个“front page”,包含了相册标题(居中对齐)以及最多三张从某文件夹中读出的读出的图片的缩略图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.
为了创建一个空图片,方法New用来接收这个尺寸和背景色按算法得出的图片。方法Canvas.GetTextExtent可以用来把字符串对齐到右边。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 up to 3 images from a directory
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
Set Folder = FSO.GetFolder( Path )
Count = 0
For Each File In Folder.Files
' Is this an image file?
If Instr( File.Type, "Image") <> 0 Then
' Draw this image on front page
Set Img = Server.CreateObject("Persits.Jpeg")
Img.Open File.Path
' 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
' Limit number of images to 3
Count = Count + 1
If Count >= 3 Then Exit For
End If
Next
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>
点击下例链接来运行这些样例代码:Click the links below to run this code sample:
http://localhost/aspjpeg/manual_05/05_frontpage.asp
http://localhost/aspjpeg/manual_05/05_frontpage.aspx




上一篇
下一篇


文章来自:
Tags: 





