Свойство «Оттенок» при рисовании изображения в VB.NET - PullRequest
1 голос
/ 20 февраля 2012

Учитывая цвет, как я могу "подкрасить" изображение, когда я рисую его в окне для картинок с VB.NET?

Ответы [ 3 ]

3 голосов
/ 25 февраля 2012

Для этого можно использовать класс 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)
2 голосов
/ 20 января 2015

Пересмотр функции Джона Кернера:

(Оригинальный пиксельный формат и неуправляемые ресурсы удалены)

    '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", если вы не будете использовать предыдущее изображение

0 голосов
/ 28 сентября 2016

Вот реализация 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;
}
...