[These pages contain ready ASP scripts. You can use them in your own applications]

VBS SCRIPT: Semi-transparent oval frame over thumbnailed image


Note: all scripts examples included in these Web pages are absolutely free. You can copy, use, and modify these scripts anyway you want. The scripts are published without warranty of any kind.

What is this

This script gets original image from file, resizes it, places white semi-transparent frame over thumbnail and write image to disk file. You need two images to get such effect: source image with original picture and black-and-white “mask” image containing black ellipse on white background. All manipulations are performed only in memory, without temporary files.


Original image
 

Mask image with black shape
 


Script will use the TransparentCopy method to make black color totally transparent and white color 50% transparent. The overlaying image can be obtained either from file or you can draw it using the Ellipse method on the fly. This page has scripts for these two cases.

The first way is more universal: commonly, you may have a file containing any shape, not necessarily ellipse. You need to resize the mask image too if its size doesn't correspond the size of thumbnail. So, you need to perform the following steps:
  1. Create two instances of ShotGraph.Image object: object 1 and object 2.
  2. Initialize primary and secondary imagespaces of object 2. All imagespaces should have size of required thumbnail.
  3. Using object 1, get dimensions of original image and resize it.
  4. Instead of saving image to file, call the ReadImage method on primary imagespace of object 2 passing object 1 as image source
  5. Using object 1, get dimensions of mask image and resize it.
  6. Instead of saving image to file, call the ReadImage method on secondary imagespace of object 2 passing object 1 as image source
  7. Use the TransparentCopy on object 2 to place content of secondary imagespace over primary one.
  8. Save image from object 2 to file.
As you see, here we use such property of ReadImage method as capability to have other ShotGraph.Image object instance as image source.

The second way (when you draw mask shape on the fly) is more simple: you needn't to read mask image from disk and resize it. You just draw shape of necessary size. You also don't need additional ShotGraph.Image object in this case. All operations are made on one ShotGraph.Image object.

File frame1.vbs
This script reads mask image from file. Two shotgraph objects are used.
Set g = CreateObject("shotgraph.image")
Set gg = CreateObject("shotgraph.image")

' Maximal dimensions for thumbnail
xsize = 150
ysize = 150

' Files
source = "original.jpg"
mask = "frame_ellipse.gif"
new_file = "result.jpg"

itype = g.GetFileDimensions(source,width,height)
if itype = 0 then
	Wscript.echo "Image is not recognized"
	Wscript.Quit(0)
end if

' Calculate thumbnail dimensions (xset x yset)
if width/xsize > height/ysize then
	xset=xsize
	yset=height*xsize\width
else
	yset=ysize
	xset=width*ysize\height
end if

g.CreateImage xset,yset,8
gg.CreateImage xset,yset,8

' Resize source image
g.InitClipboard width,height
g.SelectClipboard True
g.ReadImage source,pal,0,0
g.Stretch 0,0,xset,yset,0,0,width,height,"SRCCOPY","HALFTONE"
g.SelectClipboard False
g.Sharpen

' Read resized image to primary imagespace of gg object
gg.ReadImage g,palette,0,0

' Resize mask image
g.GetFileDimensions mask,fwidth,fheight
g.InitClipboard fwidth,fheight
g.SelectClipboard True
g.ReadImage mask,palette,0,0
g.Stretch 0,0,xset,yset,0,0,fwidth,fheight,"SRCCOPY","COLORONCOLOR"
g.SelectClipboard False

' Read resized mask image to secondary imagespace of gg object
gg.InitClipboard xset,yset
gg.SelectClipboard True
gg.ReadImage g,palette,0,0
' Copy making black color transparent
gg.TransparentCopy 0,0,xset,yset,0,0,Array(0,0,0),0,50
gg.SelectClipboard False

gg.JpegImage 90,0,new_file

File frame2.vbs
This script draws mask ellipse on secondary imagespace. One shotgraph object is used.
Set g = CreateObject("shotgraph.image")

' Maximal dimensions for thumbnail
xsize = 150
ysize = 150

' Files
source = "E:\works\shot\site\img\original.jpg"
feather = "E:\works\shot\site\img\frame_ellipse.gif"
new_file = "result.jpg"

itype = g.GetFileDimensions(source,width,height)
if itype = 0 then
	Wscript.echo "Image is not recognized"
	Wscript.Quit(0)
end if

' Calculate thumbnail dimensions (xset x yset)
if width/xsize > height/ysize then
	xset=xsize
	yset=height*xsize\width
else
	yset=ysize
	xset=width*ysize\height
end if

g.CreateImage xset,yset,8

' Resize source image
g.InitClipboard width,height
g.SelectClipboard True
g.ReadImage source,pal,0,0
g.Stretch 0,0,xset,yset,0,0,width,height,"SRCCOPY","HALFTONE"
g.SelectClipboard False
g.Sharpen

' Draw black ellipse on white background in the secondary imagespace
g.InitClipboard xset,yset
g.SelectClipboard True
g.SetColor 0,255,255,255
g.SetColor 1,0,0,0
g.SetBgColor 0
g.FillRect 0,0,xset,yset
g.SetBgColor 1
g.SetDrawColor 1
g.Ellipse xset\10,yset\10,xset-xset\10,yset-yset\10

' Copy making black color transparent
g.TransparentCopy 0,0,xset,yset,0,0,Array(0,0,0),0,50
g.SelectClipboard False

g.JpegImage 90,0,new_file


Back to examples