2013-12-18 60 views
4

嘿我試圖讓我的圖片看起來不錯,平滑(抗鋸齒)的,以使圓圖像使用掩模,你看下面:Vb.net圖像屏蔽沿邊緣光滑製作

round image

原始圖像看起來像這樣:

org image

用於圖像的掩模上方看起來像這樣(紅色是所述屏蔽顏色取出):

mask image

它的作品,但它給了我不那麼好看的鋸齒狀邊緣。蒙版是.png,圖片本身也是.png。

我使用,使蒙版的代碼是這樣的:

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. 

的ApplyMask功能是這樣的:

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 

其中採用FastPix發現here

任何幫助緩解這一點將是偉大的!謝謝!

UPDATE透明形式 代碼,我有:

Public Sub InitializeMyForm() 
    BackColor = Color.Plum 
    TransparencyKey = BackColor 
End Sub 
+0

取而代之的是二進制掩碼的,你可以把阿爾法遮掩它有屏蔽,屏蔽之間的平滑過渡。 – djv

回答

0

玩這個的時候,我還是設法使一個平滑的圖像這種方式使用的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 

的TranslateTransform和AddEllipse位置使用相同的點信息來恰當地「集中」紋理筆刷。

結果:

enter image description here

+0

即使表單窗口本身像我的透明一樣透明嗎? – StealthRT

+0

也從你的例子看來,你只使用1圖像,而不是面具? – StealthRT

+0

@StealthRT正確。不需要面具。透明背景不會真正起作用,因爲您需要* alias * with的背景。在應用我的示例之前,您將不得不復制源的區域。 – LarsTech