【问题标题】:"Tint" property when drawing Image with VB.NET使用 VB.NET 绘制图像时的“Tint”属性
【发布时间】:2012-03-10 12:45:47
【问题描述】:

给定一种颜色,当我使用 VB.NET 将图像绘制到图片框中时,如何“着色”图像?

【问题讨论】:

    标签: vb.net gdi


    【解决方案1】:

    您可以使用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)
    

    【讨论】:

    • 这听起来很不错。一个问题:如果我实际上在 Paint() 方法中的图片框中绘制了一些东西,并且我希望 那个 的东西被这个着色?我相信您的方法依赖于实际图片,但我没有:我在图片框中绘制矩形等
    • 你可以传递你已经绘制的图像,所以你可以调用 PictureBox1.Image = TintBitmap(PictureBox1.Image,Color.Red, 0.3)
    • 我遇到了一个问题:显然,PictureBox1.Image 是 Nothing,所以它会引发错误。这很奇怪。确实,PictureBox1 没有图像集,但我肯定是在它的 Graphics 属性上绘制东西。其实我是在PictureBox1Paint()方法的最后调用TintBitmap
    • 您可以使用 Graphics.FromImage() 方法从位图/图像中获取图形对象
    • 可能只是增加了矩阵前三行中的一些乘数。如果您无法使其正常工作,请提出另一个问题。
    【解决方案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”

    【讨论】:

      【解决方案3】:

      这是 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;
      }
      

      【讨论】:

        猜你喜欢
        • 2014-12-12
        • 1970-01-01
        • 2017-03-31
        • 1970-01-01
        • 2019-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多