2010-04-16 102 views

回答

1

如果您将图像加载到Bitmap类(System.Drawing.Bitmap), 中,则可以使用GetPixel()方法检索单个像素的颜色。

1

其实,这是VB.NET,但使用它转换为C#http://www.developerfusion.com/tools/convert/vb-to-csharp/

Private Sub btnOverlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOverlay.Click 
    Dim rgbColour = New Color 
    Dim bmpPicture As Bitmap = Nothing 
    Dim bmpPicture_Negative As Bitmap = Nothing 

    Try 
     bmpPicture = New Bitmap("/root/Desktop/head.jpg") 
     bmpPicture_Negative = New Bitmap("/root/Desktop/head.jpg") 
    Catch ex As Exception 
     MsgBox("One of the images is not present.") 
    End Try 


    Dim rectPicSize As System.Drawing.RectangleF = bmpPicture.GetBounds(System.Drawing.GraphicsUnit.Pixel) 
    Dim iMaxX As Integer = CInt(rectPicSize.Width) 
    Dim iMaxY As Integer = CInt(rectPicSize.Height) 

    Dim iYindex As Integer = 0 
    Dim iXindex As Integer = 0 

    For iYindex = 0 To iMaxY - 1 Step 1 
     For iXindex = 0 To iMaxX - 1 Step 1 
      rgbColour = bmpPicture.GetPixel(iXindex, iYindex) 
      rgbColour = Color.FromArgb(255 - rgbColour.R, 255 - rgbColour.G, 255 - rgbColour.B) 
      bmpPicture_Negative.SetPixel(iXindex, iYindex, rgbColour) 
     Next iXindex 
    Next iYindex 



    Dim strHTMLColor As String = System.Drawing.ColorTranslator.ToHtml(rgbColour) 

    TextBox1.Text = Nothing 
    TextBox1.Text = "R: " 
    TextBox1.Text += rgbColour.R.ToString() + vbCrLf 
    TextBox1.Text += "G: " + rgbColour.G.ToString() + vbCrLf 
    TextBox1.Text += "B: " + rgbColour.B.ToString() + vbCrLf 
    TextBox1.Text += "HTML: " + strHTMLColor + vbCrLf 
    PictureBox1.Image = bmpPicture 
    PictureBox2.Image = bmpPicture_Negative 


    'bmpPicture.Dispose() 
End Sub