請看這張圖片
綠色背景
紅色背景
此人的圖片背景為綠色。當我將綠色替換為另一種顏色時,它也替換了卡片的顏色我在卡片上畫了一個矩形,如何從替換中排除矩形
替換顏色代碼:
Public Function ReplaceColor(ByVal _image As Image, ByVal _colorOld As Color, ByVal _colorNew As Color, ByVal _tolerance As Integer) As Image
Dim bmap As Bitmap = CType(_image.Clone(), Bitmap)
Dim c As Color
Dim iR_Min, iR_Max As Integer
Dim iG_Min, iG_Max As Integer
Dim iB_Min, iB_Max As Integer
iR_Min = Math.Max(CInt(_colorOld.R) - _tolerance, 0)
iR_Max = Math.Min(CInt(_colorOld.R) _tolerance, 255)
iG_Min = Math.Max(CInt(_colorOld.G) - _tolerance, 0)
iG_Max = Math.Min(CInt(_colorOld.G) _tolerance, 255)
iB_Min = Math.Max(CInt(_colorOld.B) - _tolerance, 0)
iB_Max = Math.Min(CInt(_colorOld.B) _tolerance, 255)
For x As Integer = 0 To bmap.Width - 1
For y As Integer = 0 To bmap.Height - 1
c = bmap.GetPixel(x, y)
If (c.R >= iR_Min AndAlso c.R <= iR_Max) AndAlso (c.G >= iG_Min AndAlso c.G <= iG_Max) AndAlso (c.B >= iB_Min AndAlso c.B <= iB_Max) Then
If _colorNew = Color.Transparent Then
bmap.SetPixel(x, y, Color.FromArgb(0, _colorNew.R, _colorNew.G, _colorNew.B))
Else
bmap.SetPixel(x, y, Color.FromArgb(c.A, _colorNew.R, _colorNew.G, _colorNew.B))
End If
End If
Next
Next
Return CType(bmap.Clone(), Image)
End Function
我的矩形掩碼資訊:
Private Rct2 As New Rectangle(247, 378, 100, 70)
uj5u.com熱心網友回復:
如何從替換中排除矩形
只需使用Rectangle.Contains(),但Not在前面放一個這樣的:
Public Function ReplaceColor(ByVal _image As Image, ByVal _colorOld As Color, ByVal _colorNew As Color, ByVal _tolerance As Integer) As Image
' ... other code ...
For x As Integer = 0 To bmap.Width - 1
For y As Integer = 0 To bmap.Height - 1
If Not Rct2.Contains(New Point(x, y)) Then
' ... other code ...
End If
Next
Next
Return CType(bmap.Clone(), Image)
End Function
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/437680.html
標籤:VB.net
