Reputation: 10542
Hey all i am trying to get my images to look nice and smooth (antialiasing) from using a mask in order to make the round image as you see below:
The original image looks like this:
The mask for the image above looks like this (the red being the mask color to take out):
It works but it gives me those not-so-nice jagged edges around it. The mask is an .png and also the image itself is a .png.
The code i use to make the mask is this:
picNextTopic1.Image = Image.FromStream(wc.OpenRead(anAPI.wallOrgPostImage(keying).Replace("{width}", "50").Replace("{height}", "50"))) 'Download the image from the website.
picNextTopic1.Image = ApplyMask(New Bitmap(picNextTopic1.Image), New Bitmap(My.Resources.mask), Color.Red) 'Apply mask to the downloaded image above.
The ApplyMask function is this:
Public Function ApplyMask(ByVal bImg As Bitmap, ByVal bMask As Bitmap, ByVal maskColor As Color) As Image
Dim wImg As Integer = bImg.Width
Dim hImg As Integer = bImg.Height
Dim wMask As Integer = bMask.Width
Dim hMask As Integer = bMask.Height
Dim intMask As Integer = maskColor.ToArgb
Dim intTransparent As Integer = Color.Transparent.ToArgb
Using fpImg As New FastPix(bImg)
Using fpMask As New FastPix(bMask)
Dim pixelsImg = fpImg.PixelArray
Dim pixelsMask = fpMask.PixelArray
For y As Integer = 0 To Math.Min(hImg, hMask) - 1
For x As Integer = 0 To Math.Min(wImg, wMask) - 1
Dim iImg As Integer = (y * wImg) + x
Dim iMask As Integer = (y * wMask) + x
If pixelsMask(iMask) = intMask Then
pixelsImg(iImg) = intTransparent
End If
Next
Next
End Using
End Using
Return bImg
End Function
Which uses FastPix found here.
Any help to smooth this out would be great! Thanks!
UPDATE code for transparent form that i have:
Public Sub InitializeMyForm()
BackColor = Color.Plum
TransparencyKey = BackColor
End Sub
Upvotes: 4
Views: 2696
Reputation: 81620
Playing around with this, I did manage to make a smooth image this way using a TextureBrush:
Dim profile As Image = Image.FromFile("c:\...\profile.png")
Protected Overrides Sub OnPaint(e As PaintEventArgs)
e.Graphics.Clear(Color.SteelBlue)
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
Using tb As New TextureBrush(profile)
tb.TranslateTransform(120, 64)
Using p As New GraphicsPath
p.AddEllipse(120, 64, profile.Width, profile.Width)
e.Graphics.FillPath(tb, p)
End Using
End Using
MyBase.OnPaint(e)
End Sub
The TranslateTransform and the AddEllipse location use the same point information in order to "center" the texture brush appropriately.
The result:
Upvotes: 1