&引用;“淡色”;使用VB.NET绘制图像时的属性
给定颜色,当我使用VB.NET将图像绘制到picturebox中时,如何“着色”图像?您可以使用该类完成以下操作:&引用;“淡色”;使用VB.NET绘制图像时的属性,vb.net,gdi,Vb.net,Gdi,给定颜色,当我使用VB.NET将图像绘制到picturebox中时,如何“着色”图像?您可以使用该类完成以下操作: 'Imports System.Drawing.Imaging ''' <summary> ''' Tints a bitmap using the specified color and intensity. ''' </summary> ''' <param name="b">Bitmap to be
'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
您可以使用该类完成以下操作:
'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
John Koerner的职能修订如下: (原始像素格式和非托管资源已处置)
”导入System.Drawing.Imaging
'''
''使用指定的颜色和强度为位图着色。
'''
要着色的“”位图
“用于着色的颜色”
颜色的强度。好的范围是.25到.75,这取决于你的喜好。大多数图像将在2.0左右变白。0将不会对图像着色
''具有所需色调的位图
'''
私有函数着色位图(b为位图,颜色为颜色,强度为单个)为位图
将b2调整为新位图(b.宽度、b.高度、b.像素格式)
Dim ia作为新的图像属性
Dim作为颜色矩阵
m=新的颜色矩阵(新的单()_
{New Single(){1,0,0,0,0}_
新的Single(){0,1,0,0,0}_
新的Single(){0,0,1,0,0}_
新的Single(){0,0,0,1,0}_
新的Single(){color.R/255*强度,color.G/255*强度,color.B/255*强度,0,1})
ia.SetColorMatrix(m)
尺寸g作为图形=图形。来自图像(b2)
g、 DrawImage(b,新矩形(0,0,b.宽度,b.高度),0,0,b.宽度,b.高度,GraphicsUnit.像素,ia)
g、 处置
ia.Dispose()
返回b2
端函数
如果不使用上一张图像,请不要忘记处理“b”: (原始像素格式和非托管资源已处置)
”导入System.Drawing.Imaging
'''
''使用指定的颜色和强度为位图着色。
'''
要着色的“”位图
“用于着色的颜色”
颜色的强度。好的范围是.25到.75,这取决于你的喜好。大多数图像将在2.0左右变白。0将不会对图像着色
''具有所需色调的位图
'''
私有函数着色位图(b为位图,颜色为颜色,强度为单个)为位图
将b2调整为新位图(b.宽度、b.高度、b.像素格式)
Dim ia作为新的图像属性
Dim作为颜色矩阵
m=新的颜色矩阵(新的单()_
{New Single(){1,0,0,0,0}_
新的Single(){0,1,0,0,0}_
新的Single(){0,0,1,0,0}_
新的Single(){0,0,0,1,0}_
新的Single(){color.R/255*强度,color.G/255*强度,color.B/255*强度,0,1})
ia.SetColorMatrix(m)
尺寸g作为图形=图形。来自图像(b2)
g、 DrawImage(b,新矩形(0,0,b.宽度,b.高度),0,0,b.宽度,b.高度,GraphicsUnit.像素,ia)
g、 处置
ia.Dispose()
返回b2
端函数
如果不使用上一张图片,请不要忘记处理“b”这是C#实现,以防有人需要它。
(需要技巧的是,255 int应该显式转换为float,以便颜色分割返回实际的float而不是int):
这是C#实现,以防有人需要。
(需要技巧的是,255 int应该显式转换为float,以便颜色分割返回实际的float而不是int):
这听起来很好。有一个问题:如果我在picturebox中用Paint()方法画一些东西,我希望这些东西用这个来着色,会怎么样?我相信你的方法依赖于一幅实际的图片,但我没有:我在画矩形,这样在PictureBoxy中你可以传递你已经画的图像,所以你可以称之为PictureBox1.image=TintBitmap(PictureBox1.image,Color.Red,0.3)我有一个问题:显然,PictureBox1.image什么都不是,所以它抛出了一个错误。这很奇怪。PictureBox1确实没有图像集,但我肯定是在利用它的图形属性绘制东西。实际上,我在PictureBox1的Paint()方法的末尾调用了TintBitmap。您可以使用Graphics.FromImage()方法从位图/图像获取图形对象,可能只是增加矩阵前三行中的一些乘法器。如果你不能让它工作,作为另一个问题问。这听起来很好。有一个问题:如果我在picturebox中用Paint()方法画一些东西,我希望这些东西用这个来着色,会怎么样?我相信你的方法依赖于一幅实际的图片,但我没有:我在画矩形,这样在PictureBoxy中你可以传递你已经画的图像,所以你可以称之为PictureBox1.image=TintBitmap(PictureBox1.image,Color.Red,0.3)我有一个问题:显然,PictureBox1.image什么都不是,所以它抛出了一个错误。这很奇怪。PictureBox1确实没有图像集,但我肯定是在利用它的图形属性绘制东西。实际上,我在PictureBox1的Paint()方法的末尾调用了TintBitmap。您可以使用Graphics.FromImage()方法从位图/图像获取图形对象,可能只是增加矩阵前三行中的一些乘法器。如果你不能让它工作,问另一个问题。
'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
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;
}