VB.NET写的一个图片处理组件,用于在ASP中处理图片,缩放图片,成比例缩放,有固定比例背景的缩放,加半透明LOGO小图标等功能.
dImage.vb
Imports System
Imports System.Drawing
1<comclass(dimage.classid, dimage.eventsid)="" dimage.interfaceid,=""> _
2Public Class dImage
3
4#Region "COM GUIDs"
5' 这些 GUID 提供该类的 COM 标识及其 COM 接口。
6' 如果您更改它们,现有的客户端将再也无法
7' 访问该类。
8Public Const ClassId As String = "29641F37-8FA4-4ED9-9118-9DA8EFA306B9"
9Public Const InterfaceId As String = "06E4B037-2461-4F83-96BE-2A5D1CAAB0CE"
10Public Const EventsId As String = "802EBB14-2D4D-416E-BA26-E8ADCD480E26"
11#End Region
12
13' 可创建的 COM 类必须具有不带参数的
14' Public Sub New(),否则,该类将不会注册到 COM 注册表中,
15' 而且不能通过 CreateObject
16' 来创建。
17Private myImage As Drawing.Bitmap
18Private syimg As Drawing.Bitmap
19Private syok As Boolean = False
20Private myok As Boolean = False
21Public Sub New()
22MyBase.New()
23End Sub
24Public WriteOnly Property bigImage() As String
25Set(ByVal Value As String)
26Try
27myImage = New Bitmap(Value)
28myok = True
29Catch e As IO.IOException
30myok = False
31End Try
32End Set
33End Property
34Public WriteOnly Property LogoImage() As String
35Set(ByVal Value As String)
36Try
37syimg = New Bitmap(Value)
38syok = True
39Catch ex As Exception
40syok = False
41End Try
42End Set
43End Property
44Public Function SaveAs(ByVal ToFile As String, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal nLogo As Boolean) As String
45Try
46If myok = False Then
47Return "err0"
48Exit Function
49End If
50Dim newbmp As Bitmap = New Bitmap(nWidth, nHeight, Imaging.PixelFormat.Format16bppArgb1555)
51Dim iX As Integer
52Dim iY As Integer
53Dim xMax As Integer
54Dim yMax As Integer
55For iX = 0 To nWidth - 1
56For iY = 0 To nHeight - 1
57newbmp.SetPixel(iX, iY, Color.White)
58Next
59Next
60If nWidth < myImage.Width Or nHeight < myImage.Height Then
61If myImage.Width / myImage.Height > nWidth / nHeight Then
62xMax = nWidth
63yMax = myImage.Height * nWidth \ myImage.Width
64Else
65yMax = nHeight
66xMax = myImage.Width * nHeight \ myImage.Height
67End If
68Else
69xMax = myImage.Width
70yMax = myImage.Height
71End If
72Dim tembmp As Bitmap = New Bitmap(myImage, xMax, yMax)
73xMax = (newbmp.Width - tembmp.Width) \ 2
74yMax = (newbmp.Height - tembmp.Height) \ 2
75For iX = 0 To tembmp.Width - 1
76For iY = 0 To tembmp.Height - 1
77newbmp.SetPixel(iX + xMax, iY + yMax, tembmp.GetPixel(iX, iY))
78Next
79Next
80If syok And nLogo Then
81Dim cob As Color
82Dim coc As Color
83xMax = newbmp.Width - syimg.Width - 4
84yMax = newbmp.Height - syimg.Height - 3
85For iX = 0 To syimg.Width - 1
86For iY = 0 To syimg.Height - 1
87cob = syimg.GetPixel(iX, iY)
88coc = newbmp.GetPixel(iX + xMax, iY + yMax)
89newbmp.SetPixel(iX + xMax, iY + yMax, getnewco(cob, coc))
90Next
91Next
92End If
93newbmp.Save(ToFile, Imaging.ImageFormat.Jpeg)
94newbmp.Dispose()
95tembmp.Dispose()
96newbmp = Nothing
97tembmp = Nothing
98Return "OK"
99Catch ex As Exception
100Return ex.ToString
101End Try
102End Function
103
104Public ReadOnly Property Width() As Integer
105Get
106Return myImage.Width
107End Get
108End Property
109Public ReadOnly Property Height() As Integer
110Get
111Return myImage.Height
112End Get
113End Property
114Public Sub Close()
115myImage.Dispose()
116syimg.Dispose()
117myImage = Nothing
118syimg = Nothing
119End Sub
120Private Function getnewco(ByVal c1 As Color, ByVal c2 As Color) As Color
121Dim a1 As Integer = c1.A
122Dim r1 As Integer = c1.R
123Dim g1 As Integer = c1.G
124Dim b1 As Integer = c1.B
125Dim a2 As Integer = c2.A
126Dim r2 As Integer = c2.R
127Dim g2 As Integer = c2.G
128Dim b2 As Integer = c2.B
129a2 = 255 - a1
130r1 = CInt((r1 * a1 / 255) + (r2 * a2 / 255))
131g1 = CInt((g1 * a1 / 255) + (g2 * a2 / 255))
132b1 = CInt((b1 * a1 / 255) + (b2 * a2 / 255))
133Return Color.FromArgb(a1, r1, g1, b1)
134End Function
135
136End Class</comclass(dimage.classid,>