You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将带渐变粉色背景的图片叠加并按色相范围移除背景

解决方案

要实现带渐变粉色背景的图片透明叠加,核心是先将图片中指定色相范围内的背景像素设置为透明,再进行绘制。以下提供两种适配GDI+的实现方案,基于你使用的VB.NET代码风格:

方法1:逐像素处理(直观易调试)

遍历图片每个像素,通过色相判断是否属于粉色背景范围,将符合条件的像素Alpha通道设为0(完全透明):

Function MakeBackgroundTransparent(ByVal srcBmp As Bitmap, ByVal minHue As Integer, ByVal maxHue As Integer) As Bitmap
    Dim transparentBmp As New Bitmap(srcBmp.Width, srcBmp.Height, Imaging.PixelFormat.Format32bppArgb)
    Using g As Graphics = Graphics.FromImage(transparentBmp)
        g.DrawImage(srcBmp, New Rectangle(0, 0, srcBmp.Width, srcBmp.Height), 0, 0, srcBmp.Width, srcBmp.Height, GraphicsUnit.Pixel)
    End Using

    For y As Integer = 0 To transparentBmp.Height - 1
        For x As Integer = 0 To transparentBmp.Width - 1
            Dim pixelColor As Color = transparentBmp.GetPixel(x, y)
            ' 将RGB转换为HSV获取色相
            Dim hsv As Double() = RgbToHsv(pixelColor.R, pixelColor.G, pixelColor.B)
            Dim hue As Integer = CInt(hsv(0))
            ' 判断色相是否在粉色范围内(粉色色相大致在300-360或0-30之间,可根据实际调整)
            If (hue >= minHue AndAlso hue <= maxHue) OrElse (minHue > maxHue AndAlso (hue >= minHue Or hue <= maxHue)) Then
                ' 设置为完全透明
                transparentBmp.SetPixel(x, y, Color.Transparent)
            End If
        Next
    Next
    Return transparentBmp
End Function

' 辅助函数:RGB转HSV(获取色相值)
Function RgbToHsv(ByVal r As Integer, ByVal g As Integer, ByVal b As Integer) As Double()
    Dim h, s, v As Double
    Dim minVal As Double = Math.Min(Math.Min(r, g), b)
    Dim maxVal As Double = Math.Max(Math.Max(r, g), b)
    Dim delta As Double = maxVal - minVal

    v = maxVal / 255.0

    If delta = 0 Then
        h = 0
        s = 0
    Else
        s = delta / maxVal
        Dim dr As Double = ((maxVal - r) / 6.0 + delta / 2.0) / delta
        Dim dg As Double = ((maxVal - g) / 6.0 + delta / 2.0) / delta
        Dim db As Double = ((maxVal - b) / 6.0 + delta / 2.0) / delta

        If r = maxVal Then
            h = db - dg
        ElseIf g = maxVal Then
            h = 1.0 / 3.0 + dr - db
        Else
            h = 2.0 / 3.0 + dg - dr
        End If

        If h < 0 Then h += 1.0
        If h > 1 Then h -= 1.0
    End If

    ' 转换为0-360的色相值
    h *= 360.0
    Return New Double() {h, s, v}
End Function

使用时,先处理待叠加图片,再绘制:

' 处理待叠加图片,粉色色相范围示例:300-30(覆盖洋红到浅粉)
Dim transparentFrBm As Bitmap = MakeBackgroundTransparent(fr_bm, 300, 30)
' 执行叠加绘制
gr.DrawImage(transparentFrBm, to_rect, fr_rect, GraphicsUnit.Pixel)
' 记得释放资源
transparentFrBm.Dispose()

方法2:使用ImageAttributes(性能更优)

通过ColorMatrix和ImageAttributes在绘制时直接过滤背景色,无需修改原始图片,适合大尺寸图片:

' 创建颜色矩阵,用于设置Alpha通道
Dim colorMatrix As New Imaging.ColorMatrix(New 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, 1, 0},
    New Single() {0, 0, 0, 0, 1}
})

' 创建ImageAttributes并设置颜色矩阵
Dim imageAttr As New Imaging.ImageAttributes()
' 添加颜色过滤:将粉色范围内的像素Alpha设为0
imageAttr.SetColorKey(Color.FromArgb(255, 200, 200, 255), Color.FromArgb(255, 255, 100, 255)) ' 示例粉色范围,可根据实际调整RGB值
' 若渐变范围大,建议结合色相判断自定义ColorMap,或者使用ColorMatrix的Alpha通道计算

' 绘制时应用ImageAttributes
gr.DrawImage(fr_bm, to_rect, fr_rect.X, fr_rect.Y, fr_rect.Width, fr_rect.Height, GraphicsUnit.Pixel, imageAttr)

注意:SetColorKey适合纯色或小范围渐变背景,若背景渐变跨度大,建议自定义ColorMap或结合色相计算动态调整Alpha通道,避免边缘生硬。

关键提示

  • 粉色的色相范围通常在300°-360°(深粉/洋红)和0°-30°(浅粉)之间,可通过取色工具(如Photoshop吸管)获取实际图片的色相值,调整minHue和maxHue参数。
  • 处理图片后记得调用Dispose()释放资源,避免内存泄漏。
  • 若需要更平滑的边缘,可在设置Alpha时加入过渡(比如色相接近范围时设置半透明),避免透明边缘生硬。

内容的提问来源于stack exchange,提问作者sharkyenergy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 23:31:47