使用VB.NET绘制图像时的“色调”属性

时间:2012-02-20 05:44:28

标签: vb.net gdi

鉴于颜色,当我用VB.NET将图像绘制到图片框中时,如何“调色”图像?

3 个答案:

答案 0 :(得分:3)

您可以使用ColorMatrix类来完成此任务:

    'Imports System.Drawing.Imaging
    ''' <summary>
    ''' Tints a bitmap using the specified color and intensity.
    ''' </summary>
    ''' <param name="b">Bitmap to be tinted</param>
    ''' <param name="color">Color to use for tint</param>
    ''' <param name="intensity">Intensity of the tint.  Good ranges are .25 to .75, depending on your preference.  Most images will white out around 2.0. 0 will not tint the image at all</param>
    ''' <returns>A bitmap with the requested Tint</returns>
    ''' <remarks></remarks>
Private function TintBitmap(b As Bitmap, color As Color, intensity As Single ) As Bitmap         
        Dim b2 As New Bitmap(b.Width,b.Height)

        Dim ia As New ImageAttributes

        Dim m As ColorMatrix 
        m = New 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() {color.R/255*intensity, color.G/255*intensity, color.B/255*intensity, 0, 1}})

        ia.SetColorMatrix(m)
        Dim g As Graphics = Graphics.FromImage(b2)
        g.DrawImage(b,new Rectangle(0, 0, b.Width, b.Height), 0, 0, b.Width, b.Height, GraphicsUnit.Pixel, ia)
        Return b2

End Function

然后使用它,你可以简单地说:

Dim b As Bitmap = New Bitmap(ofd.FileName)                        
PictureBox1.Image = TintBitmap(b,Color.Red,0.3)

答案 1 :(得分:2)

John Koerner的功能修订:

(处理原始像素格式和非托管资源)

    'Imports System.Drawing.Imaging
''' <summary>
''' Tints a bitmap using the specified color and intensity.
''' </summary>
''' <param name="b">Bitmap to be tinted</param>
''' <param name="color">Color to use for tint</param>
''' <param name="intensity">Intensity of the tint.  Good ranges are .25 to .75, depending on your preference.  Most images will white out around 2.0. 0 will not tint the image at all</param>
''' <returns>A bitmap with the requested Tint</returns>
''' <remarks></remarks>
Private function TintBitmap(b As Bitmap, color As Color, intensity As Single ) As Bitmap         

    Dim b2 As New Bitmap(b.Width,b.Height, b.PixelFormat)

    Dim ia As New ImageAttributes

    Dim m As ColorMatrix 
    m = New 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() {color.R/255*intensity, color.G/255*intensity, color.B/255*intensity, 0, 1}})

    ia.SetColorMatrix(m)
    Dim g As Graphics = Graphics.FromImage(b2)
    g.DrawImage(b,new Rectangle(0, 0, b.Width, b.Height), 0, 0, b.Width, b.Height, GraphicsUnit.Pixel, ia)


  g.Dispose()
  ia.Dispose()

    Return b2
End Function

如果您不使用上一张图片,请不要忘记处理“b”

答案 2 :(得分:0)

这是C#实现,以防有人需要它。 (因为255的int应该被显式地转换为float,以便颜色除法返回实际的float而不是int):

Bitmap TintBitmap(Bitmap bitmap, Color color, float intensity)
{
    Bitmap outBmp = new Bitmap(bitmap.Width, bitmap.Height, bitmap.PixelFormat);

    using (ImageAttributes ia = new ImageAttributes())
    {

        ColorMatrix m = new ColorMatrix(new float[][]
        {new float[] {1, 0, 0, 0, 0},
         new float[] {0, 1, 0, 0, 0},
         new float[] {0, 0, 1, 0, 0},
         new float[] {0, 0, 0, 1, 0},
         new float[] {color.R/255f*intensity, color.G/255f*intensity, color.B/255f*intensity, 0, 1}});

                ia.SetColorMatrix(m);
                using (Graphics g = Graphics.FromImage(outBmp))
                    g.DrawImage(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, ia);
            }

    return outBmp;
}