Imports System.Net
Imports System.IO
Imports System.Drawing.Imaging
'....................
Const Tollerance As Single = 0.2F
Private Sub Sep(ByVal InVal1 As Integer, ByVal InVal2 As Integer, ByRef OutVal1 As Integer, ByRef OutVal2 As Integer)
If InVal1 > InVal2 Then
OutVal1 = InVal2
OutVal2 = InVal1
Else
OutVal1 = InVal1
OutVal2 = InVal2
End If
ApplyTol(OutVal1, 1 - Tollerance)
ApplyTol(OutVal2, 1 + Tollerance)
End Sub
Private Sub ApplyTol(ByRef Val As Integer, ByVal Tol As Single)
Val = CInt(Val * Tol)
If Val < 0 Then Val = 0
If Val > 255 Then Val = 255
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.WindowState = FormWindowState.Maximized
Dim gr As Graphics = Me.CreateGraphics
Dim mImage As Bitmap
Try ' download wossnames avatar
Dim Req As WebRequest = WebRequest.Create("http://www.vbforums.com/image.php?u=37340&dateline=1117135946")
Dim Resp As WebResponse = Req.GetResponse
Dim ImgStream As Stream = Resp.GetResponseStream
mImage = CType(Image.FromStream(ImgStream), Bitmap)
Resp.Close()
ImgStream.Close()
Catch : Exit Sub : End Try ' don't care
' define drawing location
Dim UpLeftPoint As New Point(Me.Width \ 2 - mImage.Width * 2, _
Me.Height \ 2 - mImage.Height * 2)
Dim DrawingSize As New Size(mImage.Width * 4, mImage.Height * 4)
Dim UpLeftShadowPoint As New Point(UpLeftPoint.X + 15, UpLeftPoint.Y + 15)
' show the original
gr.DrawImage(mImage, New Rectangle(UpLeftPoint, DrawingSize))
' wait a while
Threading.Thread.Sleep(2000)
gr.Clear(Me.BackColor)
' sample two colors for the colorkey in order to remove the background
Dim MaskColor1 As Color = mImage.GetPixel(mImage.Width - 7, mImage.Height - 7)
Dim MaskColor2 As Color = mImage.GetPixel(0, 0)
Dim R1, R2, G1, G2, B1, B2 As Integer
' create a low and a high color from the samples
' would like to know if there is a better way.
Sep(MaskColor1.R, MaskColor2.R, R1, R2)
Sep(MaskColor1.G, MaskColor2.G, G1, G2)
Sep(MaskColor1.B, MaskColor2.B, B1, B2)
Dim MaskColorLow As Color = Color.FromArgb(R1, G1, B1)
Dim MaskColorHigh As Color = Color.FromArgb(R2, G2, B2)
' create attributes for the shadow
Dim AttrShadow As New ImageAttributes
' R, G and B components in the low color must all be lower
' than those components in the high color
AttrShadow.SetColorKey(MaskColorLow, MaskColorHigh)
' make it transparent
Dim AttrShadowMatrixItems As Single()() = { _
New Single() {1, 0, 0, 0, 0}, _
New Single() {0, 1, 0, 0, 0}, _
New Single() {0, 0, 1, 0, 0}, _
New Single() {0, 0, 0, 0.3F, 0}, _
New Single() {0, 0, 0, 0, 1} _
}
Dim colorMatrix As New ColorMatrix(AttrShadowMatrixItems)
AttrShadow.SetColorMatrix(colorMatrix)
' create attributes for foreground pic
Dim AttrFore As New ImageAttributes
AttrFore.SetColorKey(MaskColorLow, MaskColorHigh)
' draw shadow
gr.DrawImage( _
mImage, _
New Rectangle(UpLeftShadowPoint, _
DrawingSize), _
0, _
0, _
mImage.Width, _
mImage.Height, _
GraphicsUnit.Pixel, _
AttrShadow _
)
' draw pic
gr.DrawImage(mImage, _
New Rectangle(UpLeftPoint, DrawingSize), _
0, _
0, _
mImage.Width, _
mImage.Height, _
GraphicsUnit.Pixel, _
AttrFore _
)
End Sub