<%@ LANGUAGE = JScript%> <% // JavaScript Scripting Example 03 // // "Textured Text" // // Dimac 2002-02-18 (www.dimac.net) var errormsg = new String(""); errormsg = Texture(); // Check if errors occured if (errormsg != "") DisplayError(errormsg); // Create a textured text at the bottom of the surf image // Try make the text smooth and antialiased. Even make // a transparent shadow of the text in the background. function Texture() { try { var imageobj1; var imageobj2; var imageobj3; var width; var height; var top; var left; var text; var fontobj; var path; // Fix path path = Server.MapPath("..\\..\\images") + "\\"; // Set extra space for alpha blend text var extra = 1; // Shadow space var shadow = 15; imageobj1 = Server.CreateObject("W3Image.Image"); imageobj2 = Server.CreateObject("W3Image.Image"); imageobj3 = Server.CreateObject("W3Image.Image"); // Load texture if (imageobj1.LoadImage(path + "tools.jpg") == false) { // Do your error handling here... return "Error when loading image 'tools.jpg'."; } // Create a small surface (to get text size) imageobj2.CreateEmptySurface(1,1) // Load background image if (imageobj3.LoadImage(path + "surf.jpg") == false) { // Do your error handling here... return "Error when loading image 'surf.jpg'."; } // Create a white font fontobj = imageobj2.CreateFont('Arial',180,0,'normal',0,0xFFFFFF,false,false,false); imageobj2.SetFont(fontobj); // Text to center text = "w3 Image"; // Get size of the text width = imageobj2.GetTextWidth(text); height = imageobj2.GetTextHeight(text); // Calc postion for text top = ((imageobj3.height - height) - 20); if (top < 0) top = 0; left = ((imageobj3.width - width)/2); if (left <= 0) left = 0; // Set background to black imageobj2.bkColor = 0x000000; // Finally create the computed surface to use as a mask (text is white) imageobj2.CreateEmptySurface(imageobj3.width, imageobj3.height); // Important!!!! Select the font again (is lost when creating a new surface) imageobj2.SetFont(fontobj); // Draw the text imageobj2.DrawText(text, left, top); imageobj1.StretchBltExt(imageobj2, 0, 0, imageobj2.width, imageobj2.height, 0, 0, imageobj1.width, imageobj1.height, "srcand"); // Make a shadow text imageobj2.StretchBltExt(imageobj3, left + shadow, top + shadow, (left + width) + shadow, (top + height) + shadow, left, top, (left + width), (top + height), "alphatransparent", 20); // Fix antialiased text imageobj2.StretchBltExt(imageobj3, left, top, (left + width), (top + height), left, top, (left + width), (top + height), "alphatransparent", 64); imageobj2.StretchBltExt(imageobj3, left, top + extra, (left + width), (top + height) + extra, left, top, (left + width), (top + height), "alphatransparent", 64); imageobj2.StretchBltExt(imageobj3, left + extra, top, (left + width) + extra, (top + height), left, top, (left + width), (top + height), "alphatransparent", 64); imageobj2.StretchBltExt(imageobj3, left + extra, top + extra, (left + width) + extra, (top + height) + extra, left, top, (left + width), (top + height), "alphatransparent", 64); if (imageobj3.StreamImage(Response, "JPG", 24) == false) { // Do your error handling here... return "Error when streaming image."; } // Success return ""; } catch(err) { // Do your error handling here... return "Error: " + (err.number & 0xFFFF) + " - " + err.description + "."; } } // Example of a error handler - Displaying the error as an image function DisplayError(msgcode) { // Create an error image var errorimage = Server.CreateObject("W3Image.Image"); errorimage.CreateEmptySurface(1,1); // Create and select the font var fontobj = errorimage.CreateFont("Tahoma",24,0,"normal",0,0x000000,false,false,true); errorimage.SetFont(fontobj); // Get size of the text var width = errorimage.GetTextWidth(msgcode); var height = errorimage.GetTextHeight(msgcode); // Create a surface as large as the error message errorimage.CreateEmptySurface(width,height); // Select the font again (font is deselected when creating a new surface) errorimage.SetFont(fontobj); // Write the error message errorimage.DrawText(msgcode,0,0); // Stream the image errorimage.StreamImage(Response, "JPG", 24); } %>