【问题标题】:Vb.net image mask making smooth along the edgesVb.net图像蒙版沿边缘平滑
【发布时间】:2014-01-06 19:22:30
【问题描述】:

大家好,我正在尝试通过使用蒙版来使我的图像看起来漂亮和平滑(抗锯齿),以便制作如下所示的圆形图像:

原图如下:

上图的蒙版是这样的(红色是要取出的蒙版颜色):

它有效,但它给了我那些不太好的锯齿状边缘。掩码是 .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

任何帮助解决这个问题都会很棒!谢谢!

更新 我拥有的透明表单代码:

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

【问题讨论】:

  • 您可以制作一个 alpha 蒙版,而不是二进制蒙版,它在未蒙版和蒙版之间具有平滑过渡。

标签: vb.net image mask antialiasing


【解决方案1】:

玩弄这个,我确实设法使用 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 位置使用相同的点信息来适当地“居中”纹理画笔。

结果:

【讨论】:

  • 即使表单窗口本身像我的一样是透明的,你会得到相同的结果吗?
  • 而且从你的例子看来你只使用了一张图片而不是面具?
  • @StealthRT 正确。不需要面具。透明背景不会真正起作用,因为您需要一个背景来别名。在应用我的示例之前,您必须复制源区域。
  • 是的,这就是我想的......但我需要背景表格是透明的。 :o/
  • 我可以在 WPF 中很好地做到这一点,但我现在拥有的大部分代码都不能在 WPF 中工作 - 只是一个 Windows 窗体应用程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-07
  • 2016-09-21
  • 1970-01-01
  • 1970-01-01
  • 2019-04-30
  • 1970-01-01
相关资源
最近更新 更多