本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。vb.net写的一个图片处理组件,用于在asp中处理图片,缩放图片,成比例缩放,有固定比例背景的缩放,加半透明logo小图标等功能.
dimage.vb
imports system
imports system.drawing
<comclass(dimage.classid, dimage.interfaceid, dimage.eventsid)> _
public class dimage
#region "com guids"
' 这些 guid 提供该类的 com 标识及其 com 接口。
' 如果您更改它们,现有的客户端将再也无法
' 访问该类。
public const classid as string = "29641f37-8fa4-4ed9-9118-9da8efa306b9"
public const interfaceid as string = "06e4b037-2461-4f83-96be-2a5d1caab0ce"
public const eventsid as string = "802ebb14-2d4d-416e-ba26-e8adcd480e26"
#end region
' 可创建的 com 类必须具有不带参数的
' public sub new(),否则,该类将不会注册到 com 注册表中,
' 而且不能通过 createobject
' 来创建。
private myimage as drawing.bitmap
private syimg as drawing.bitmap
private syok as boolean = false
private myok as boolean = false
public sub new()
mybase.new()
end sub
public writeonly property bigimage() as string
set(byval value as string)
try
myimage = new bitmap(value)
myok = true
catch e as io.ioexception
myok = false
end try
end set
end property
public writeonly property logoimage() as string
set(byval value as string)
try
syimg = new bitmap(value)
syok = true
catch ex as exception
syok = false
end try
end set
end property
public function saveas(byval tofile as string, byval nwidth as integer, byval nheight as integer, byval nlogo as boolean) as string
try
if myok = false then
return "err0"
exit function
end if
dim newbmp as bitmap = new bitmap(nwidth, nheight, imaging.pixelformat.format16bppargb1555)
dim ix as integer
dim iy as integer
dim xmax as integer
dim ymax as integer
for ix = 0 to nwidth - 1
for iy = 0 to nheight - 1
newbmp.setpixel(ix, iy, color.white)
next
next
if nwidth < myimage.width or nheight < myimage.height then
if myimage.width / myimage.height > nwidth / nheight then
xmax = nwidth
ymax = myimage.height * nwidth / myimage.width
else
ymax = nheight
xmax = myimage.width * nheight / myimage.height
end if
else
xmax = myimage.width
ymax = myimage.height
end if
dim tembmp as bitmap = new bitmap(myimage, xmax, ymax)
xmax = (newbmp.width - tembmp.width) / 2
ymax = (newbmp.height - tembmp.height) / 2
for ix = 0 to tembmp.width - 1
for iy = 0 to tembmp.height - 1
newbmp.setpixel(ix + xmax, iy + ymax, tembmp.getpixel(ix, iy))
next
next
if syok and nlogo then
dim cob as color
dim coc as color
xmax = newbmp.width - syimg.width - 4
ymax = newbmp.height - syimg.height - 3
for ix = 0 to syimg.width - 1
for iy = 0 to syimg.height - 1
cob = syimg.getpixel(ix, iy)
coc = newbmp.getpixel(ix + xmax, iy + ymax)
newbmp.setpixel(ix + xmax, iy + ymax, getnewco(cob, coc))
next
next
end if
newbmp.save(tofile, imaging.imageformat.jpeg)
newbmp.dispose()
tembmp.dispose()
newbmp = nothing
tembmp = nothing
return "ok"
catch ex as exception
return ex.tostring
end try
end function
public readonly property width() as integer
get
return myimage.width
end get
end property
public readonly property height() as integer
get
return myimage.height
end get
end property
public sub close()
myimage.dispose()
syimg.dispose()
myimage = nothing
syimg = nothing
end sub
private function getnewco(byval c1 as color, byval c2 as color) as color
dim a1 as integer = c1.a
dim r1 as integer = c1.r
dim g1 as integer = c1.g
dim b1 as integer = c1.b
dim a2 as integer = c2.a
dim r2 as integer = c2.r
dim g2 as integer = c2.g
dim b2 as integer = c2.b
a2 = 255 - a1
r1 = cint((r1 * a1 / 255) + (r2 * a2 / 255))
g1 = cint((g1 * a1 / 255) + (g2 * a2 / 255))
b1 = cint((b1 * a1 / 255) + (b2 * a2 / 255))
return color.fromargb(a1, r1, g1, b1)
end function
end class