【发布时间】:2012-03-10 12:45:47
【问题描述】:
给定一种颜色,当我使用 VB.NET 将图像绘制到图片框中时,如何“着色”图像?
【问题讨论】:
给定一种颜色,当我使用 VB.NET 将图像绘制到图片框中时,如何“着色”图像?
【问题讨论】:
您可以使用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)
【讨论】:
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”
【讨论】:
这是 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;
}
【讨论】: