|
I use the following method for coloring an image. My source image is a black font with a transparent background. I just want to color that graphical font. This font is not absolutely black it contains grayscales so ColorMap is not the right option.
private Bitmap ColorBitmap(Bitmap _b, Color _color)
{
Bitmap bmp = new Bitmap(_b.Width, _b.Height);
Graphics g = Graphics.FromImage(bmp);
float cr = _color.R / 255;
float cg = _color.G / 255;
float cb = _color.B / 255;
float ca = _color.A / 255;
float[][] colorMatrixElements = {
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[] {cr, cg, cb, ca, 1}};
ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
Rectangle rectangle = new Rectangle(0, 0, _b.Width, _b.Height);
g.DrawImage(_b, rectangle, 0, 0, _b.Width, _b.Height, GraphicsUnit.Pixel, imageAttributes);
return bmp;
}
I must admit that I don´t understand the concepts of the ColorMatrix. Is anybody able to modify my method in a way that I am able to color a grayscale image into any color ?
Best Regards, Kevin
|
|
|
|
|
Try using a matrix that combines grayscaling with the color component addition. Here goes one:
float[][] colorMatrixElements = {<br />
new float[] { 0.213f, 0.213f, 0.213f, 0, 0 }, <br />
new float[] { 0.715f, 0.715f, 0.715f, 0, 0 }, <br />
new float[] { 0.072f, 0.072f, 0.072f, 0, 0 }, <br />
new float[] { 0, 0, 0, 1, 0 },<br />
new float[] { cr, cg, cb, ca, 1 }};
|
|
|
|
|
Nice article Michael..
It helped me a lot..
Is it possible to do sharpness and blur operation on the image using the Colormatrix.
If so what are all the values we have to pass for the matrix elements..
Any help is appreciated..
Thanks in advance.
sing
-- modified at 9:36 Friday 7th July, 2006
|
|
|
|
|
I think ColorMatrix can't sharp and blur image. ColorMatrix only changes every pixel, doesn't consider the relation of pixels, while sharp and blur must consider neighboring pixels.
It's easy to blur image, create a 3*3 matrix, calculate every pixel by matrix and neighboring pixel, for example,
matrix is
0.05 0.05 0.05
0.05 0.60 0.05
0.05 0.05 0.05
pixels' gray value are
100 110 120
130 140 160
170 180 190
after blur, the center pixel's gray value is not 140, it's
(100+110+120+130+160+170+180+190)*0.05+140*0.6=145.
It's easy to apply this method to RGB color space.
I don't how to sharp an image.
-- modified at 5:48 Thursday 20th July, 2006
|
|
|
|
|
There seems to be a bug in the .net2.0 printing support while using imageattributes.
I had been trying to print a jpeg image with an alpha adjust in the colormatrix. In a print preview dialog it works perfectly, however when it got to the stage of printing I was getting an outofmemory exception.
I figured it out eventually after I had tried using a png with a transparent background. The format of the image has to be 32bppARGB or changed to it, you can do this by creating a new bitmap with the 32bppARGB format that is the same size as the original and draw the original in that, the code I used is below.
Dim newImage As System.Drawing.Image = image.fromfile("c:\sample.jpg")<br />
Dim bm As New Bitmap(newImage.Width, newImage.Height, imaging.PixelFormat.Format32bppArgb)<br />
<br />
Dim g As Graphics = Graphics.FromImage(bm)<br />
g.DrawImage(newImage, 0, 0, newImage.Width, newImage.Height)<br />
g.dispose()<br />
newimage.dispose()
bm now holds an image that can be printed using a colormatrix.
This does not affect a png with a transparent background.
-- modified at 7:26 Wednesday 10th May, 2006
ok I've just found out something even better, the bug only seems to affect loading an image in the system.drawing.image class. If you use the system.drawing.bitmap class to load your image, printing works perfectly even if you do a conversion to the system.drawing.image class! I can only assume that the bitmap class defaults to the 32bppArgb format, where as the image class defaults to the files colour format.
Obviously if your using a third party class then the first method may still be appropriate as you have no control on how pictures are loaded.
|
|
|
|
|
This class can be used to convert bitmaps (as well as color objects) to grayscale using several semi-standard formulas.
I had to remove the Xml documentation since it was interfering with the message format, but it should be fairly straightforward to figure out.
<code>
Dim oGray As New OpenBook.Graphics.Colors.Grayscale
_newImage = oGray.ConvertBitmapToGrayscale(_oldImage, OpenBook.Graphics.Colors.Grayscale.Formula.CCIRRec709)
</code>
You will have to replace [OpenBook] with something else. It's simply the way I've organized the namespaces for myself.
Thanks to the original author of this tutorial on the help with ColorMatrix. It makes an enormous performance difference (even if I wish it was a bit quicker still).
Kevin Townsend
<code>
Imports System.Drawing
Imports System.Drawing.Imaging
Namespace Graphics.Colors
Public Class Grayscale
Public Enum Formula
SimpleAverage = 0 ' Least accurate
WeightAverage = 1
NtscPal = 2 ' CCIR Recommendation 601-1 (Used in Ntsc/Pal Standards)
CCIRRec709 = 3 ' CCIR Recommendation 709
End Enum
Public Function ConvertRgbToGrayscale(ByVal sourceColor As Drawing.Color, Optional ByVal formula As Formula = Formula.CCIRRec709)
Dim newColor As New Drawing.Color
Dim a1, r1, g1, b1 As Int16
Dim a2, r2, g2, b2 As Int16
' Get source color ARGB attributes
a1 = Convert.ToInt16(sourceColor.A)
r1 = Convert.ToInt16(sourceColor.R)
g1 = Convert.ToInt16(sourceColor.G)
b1 = Convert.ToInt16(sourceColor.B)
' Calculate changes depending on foruma selected
Select Case formula
Case formula.SimpleAverage
a2 = a1
r2 = Convert.ToInt16((r1 + g1 + b1) / 3)
g2 = Convert.ToInt16((r1 + g1 + b1) / 3)
b2 = Convert.ToInt16((r1 + g1 + b1) / 3)
Case formula.WeightAverage
a2 = a1
r2 = Convert.ToInt16((3 * r1) + (4 * b1) + (2 * g1) / 9)
g2 = Convert.ToInt16((3 * r1) + (4 * b1) + (2 * g1) / 9)
b2 = Convert.ToInt16((3 * r1) + (4 * b1) + (2 * g1) / 9)
Case formula.NtscPal
a2 = a1
r2 = Convert.ToInt16((0.299 * r1) + (0.587 * g1) + (0.114 * b1))
g2 = Convert.ToInt16((0.299 * r1) + (0.587 * g1) + (0.114 * b1))
b2 = Convert.ToInt16((0.299 * r1) + (0.587 * g1) + (0.114 * b1))
Case formula.CCIRRec709
a2 = a1
r2 = Convert.ToInt16((0.2125 * r1) + (0.7154 * g1) + (0.0721 * b1))
g2 = Convert.ToInt16((0.2125 * r1) + (0.7154 * g1) + (0.0721 * b1))
b2 = Convert.ToInt16((0.2125 * r1) + (0.7154 * g1) + (0.0721 * b1))
Case Else
' Use CCIR Rec 709 as default
a2 = a1
r2 = Convert.ToInt16((0.2125 * r1) + (0.7154 * g1) + (0.0721 * b1))
g2 = Convert.ToInt16((0.2125 * r1) + (0.7154 * g1) + (0.0721 * b1))
b2 = Convert.ToInt16((0.2125 * r1) + (0.7154 * g1) + (0.0721 * b1))
End Select
' Check limits
If r2 > 255 Then r2 = 255
If g2 > 255 Then g2 = 255
If b2 > 255 Then b2 = 255
If r2 < 0 Then r2 = 0
If g2 < 0 Then g2 = 0
If b2 < 0 Then b2 = 0
' Set newColor
newColor = Drawing.Color.FromArgb(a2, r2, g2, b2)
' Return
Return newColor
End Function
Public Function ConvertBitmapToGrayscale(ByVal sourceImage As Bitmap, Optional ByVal formula As Formula = Formula.CCIRRec709) As Bitmap
Dim output As Bitmap = New Bitmap(sourceImage.Width, sourceImage.Height)
Dim rect As New Rectangle(0, 0, sourceImage.Width, sourceImage.Height)
Dim cm As ColorMatrix
Dim ia As New ImageAttributes
' Assign sourceBitmap to a new graphics object
Dim g As Drawing.Graphics = Drawing.Graphics.FromImage(output)
' Apply selected grayscale formula
Select Case formula
Case Colors.Grayscale.Formula.CCIRRec709
cm = New ColorMatrix(New Single()() _
{New Single() {0.213, 0.213, 0.213, 0, 0}, _
New Single() {0.715, 0.715, 0.715, 0, 0}, _
New Single() {0.072, 0.072, 0.072, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
Case Colors.Grayscale.Formula.NtscPal
cm = New ColorMatrix(New Single()() _
{New Single() {0.299, 0.299, 0.299, 0, 0}, _
New Single() {0.587, 0.587, 0.587, 0, 0}, _
New Single() {0.114, 0.114, 0.114, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
Case Colors.Grayscale.Formula.SimpleAverage
cm = New ColorMatrix(New Single()() _
{New Single() {0.333, 0.333, 0.333, 0, 0}, _
New Single() {0.333, 0.333, 0.333, 0, 0}, _
New Single() {0.333, 0.333, 0.333, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
Case Colors.Grayscale.Formula.WeightAverage
cm = New ColorMatrix(New Single()() _
{New Single() {0.333, 0.333, 0.333, 0, 0}, _
New Single() {0.444, 0.444, 0.444, 0, 0}, _
New Single() {0.222, 0.222, 0.222, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
Case Else
' Use CCIR Rec. 709 as catch all
cm = New ColorMatrix(New Single()() _
{New Single() {0.213, 0.213, 0.213, 0, 0}, _
New Single() {0.715, 0.715, 0.715, 0, 0}, _
New Single() {0.072, 0.072, 0.072, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
End Select
' Set color matrix
ia.SetColorMatrix(cm)
' Render new bitmap image
g.DrawImage(sourceImage, rect, 0, 0, sourceImage.Width, sourceImage.Height, GraphicsUnit.Pixel, ia)
' Return converted image
Return output
End Function
End Class
End Namespace
</code>
|
|
|
|
|
I try this on 256-colored pictures and have no result.
What can you advice me?
|
|
|
|
|
I noticed this on GIF pictures.
So, I decided to promote the image, if it isn't in the Format32bppARGB Pixel Format, to this Pixel Format, and it worked just fine.
That's the code I use:
Public Shared Function PictureConvertToRGB(ByVal bmp As Bitmap) As Bitmap
Dim img As New Bitmap(bmp.Width, bmp.Height, _
Imaging.PixelFormat.Format32bppArgb)
img.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution)
Using gr As Graphics = Graphics.FromImage(img)
gr.DrawImageUnscaled(bmp, 0, 0)
End Using
Return img
End Function
Give it a trial: it will surely work for you, too.
<div class="ForumMod">modified on Monday, May 26, 2008 7:38 AM</div>
|
|
|
|
|
Any ideas on how to implement...
Currently working on a project which requires hue, looks as if this could do it, somehow!
Help would be greatly appreciated.
|
|
|
|
|
' Matrix Operations for Image Processing
' Converted from C to VB.NET By Headkaze (headkaze [at] gmail.com)
' Thanks to Paul Haeberli
' http://www.sgi.com/misc/grafica/matrix/
Option Explicit On
Option Strict On
Imports System.Drawing.Imaging
Module ColourProcessing
Const RLUM As Single = 0.3086
Const GLUM As Single = 0.6094
Const BLUM As Single = 0.082
' MultiplyMatrix - multiply two matricies
Public Sub MultiplyMatrix(ByRef a As ColorMatrix, ByRef b As ColorMatrix, ByRef c As ColorMatrix)
Dim x, y As Integer
Dim temp As New ColorMatrix
For y = 0 To 4
For x = 0 To 4
temp(y, x) = b(y, 0) * a(0, x) _
+ b(y, 1) * a(1, x) _
+ b(y, 2) * a(2, x) _
+ b(y, 3) * a(3, x) _
+ b(y, 4) * a(4, x)
Next
Next
For y = 0 To 4
For x = 0 To 4
c(y, x) = temp(y, x)
Next
Next
End Sub
' CreateIdentityMatrix - make an identity matrix
Public Sub CreateIdentityMatrix(ByRef matrix As ColorMatrix)
matrix = 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() {0, 0, 0, 0, 1}})
End Sub
' Transform3dPointMatrix - transform a 3D point using a matrix
Public Sub Transform3dPointMatrix(ByRef matrix As ColorMatrix, ByVal x As Single, ByVal y As Single, ByVal z As Single, ByRef tx As Single, ByRef ty As Single, ByRef tz As Single)
tx = x * matrix(0, 0) + y * matrix(1, 0) + z * matrix(2, 0) + matrix(3, 0)
ty = x * matrix(0, 1) + y * matrix(1, 1) + z * matrix(2, 1) + matrix(3, 1)
tz = x * matrix(0, 2) + y * matrix(1, 2) + z * matrix(2, 2) + matrix(3, 2)
End Sub
' ColourScaleMatrix - make a color scale matrix
Public Sub ColourScaleMatrix(ByRef mat As ColorMatrix, ByVal rscale As Single, ByVal gscale As Single, ByVal bscale As Single)
Dim nmat As ColorMatrix = New ColorMatrix(New Single()() _
{New Single() {rscale, 0, 0, 0, 0}, _
New Single() {0, gscale, 0, 0, 0}, _
New Single() {0, 0, bscale, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
MultiplyMatrix(nmat, mat, mat)
End Sub
' LuminanceMatrix - make a luminance matrix
Public Sub LuminanceMatrix(ByRef mat As ColorMatrix)
Dim rwgt, gwgt, bwgt As Single
rwgt = RLUM
gwgt = GLUM
bwgt = BLUM
Dim mmat As ColorMatrix = New ColorMatrix(New Single()() _
{New Single() {rwgt, rwgt, rwgt, 0, 0}, _
New Single() {gwgt, gwgt, gwgt, 0, 0}, _
New Single() {bwgt, bwgt, bwgt, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
MultiplyMatrix(mmat, mat, mat)
End Sub
' SaturateMatrix - make a saturation matrix
Public Sub SaturateMatrix(ByRef mat As ColorMatrix, ByVal sat As Single)
Dim a, b, c, d, e, f, g, h, i As Single
Dim rwgt, gwgt, bwgt As Single
rwgt = RLUM
gwgt = GLUM
bwgt = BLUM
a = CSng((1.0# - sat) * rwgt + sat)
b = CSng((1.0# - sat) * rwgt)
c = CSng((1.0# - sat) * rwgt)
d = CSng((1.0# - sat) * gwgt)
e = CSng((1.0# - sat) * gwgt + sat)
f = CSng((1.0# - sat) * gwgt)
g = CSng((1.0# - sat) * bwgt)
h = CSng((1.0# - sat) * bwgt)
i = CSng((1.0# - sat) * bwgt + sat)
Dim mmat As ColorMatrix = New ColorMatrix(New Single()() _
{New Single() {a, b, c, 0, 0}, _
New Single() {d, e, f, 0, 0}, _
New Single() {g, h, i, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
MultiplyMatrix(mmat, mat, mat)
End Sub
' OffsetMatrix - offset r, g, and b
Public Sub OffsetMatrix(ByRef mat As ColorMatrix, ByVal roffset As Single, ByVal goffset As Single, ByVal boffset As Single)
Dim mmat As ColorMatrix = 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() {roffset, goffset, boffset, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
MultiplyMatrix(mmat, mat, mat)
End Sub
' XRotateMatrix - rotate about the x (red) axis
Public Sub XRotateMatrix(ByRef mat As ColorMatrix, ByVal rs As Single, ByVal rc As Single)
Dim mmat As ColorMatrix = New ColorMatrix(New Single()() _
{New Single() {1, 0, 0, 0, 0}, _
New Single() {0, rc, rs, 0, 0}, _
New Single() {0, -rs, rc, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
MultiplyMatrix(mmat, mat, mat)
End Sub
' YRotateMatrix - rotate about the y (green) axis
Public Sub YRotateMatrix(ByRef mat As ColorMatrix, ByVal rs As Single, ByVal rc As Single)
Dim mmat As ColorMatrix = New ColorMatrix(New Single()() _
{New Single() {rc, 0, -rs, 0, 0}, _
New Single() {0, 1, 0, 0, 0}, _
New Single() {rs, 0, rc, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
MultiplyMatrix(mmat, mat, mat)
End Sub
' ZRotateMatrix - rotate about the z (blue) axis
Public Sub ZRotateMatrix(ByRef mat As ColorMatrix, ByVal rs As Single, ByVal rc As Single)
Dim mmat As ColorMatrix = New ColorMatrix(New Single()() _
{New Single() {rc, rs, 0, 0, 0}, _
New Single() {-rs, rc, 0, 0, 0}, _
New Single() {0, 0, 1, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
MultiplyMatrix(mmat, mat, mat)
End Sub
' ZShearMatrix - shear z using x and y.
Public Sub ZShearMatrix(ByRef mat As ColorMatrix, ByVal dx As Single, ByVal dy As Single)
Dim mmat As ColorMatrix = New ColorMatrix(New Single()() _
{New Single() {1, 0, dx, 0, 0}, _
New Single() {0, 1, dy, 0, 0}, _
New Single() {0, 0, 1, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
MultiplyMatrix(mmat, mat, mat)
End Sub
' HueRotateMatrix - rotate the hue, while maintaining luminance.
Public Sub HueRotateMatrix(ByRef mat As ColorMatrix, ByVal rot As Single)
Dim mmat As ColorMatrix
Dim mag As Single
Dim lx, ly, lz As Single
Dim xrs, xrc As Single
Dim yrs, yrc As Single
Dim zrs, zrc As Single
Dim zsx, zsy As Single
CreateIdentityMatrix(mmat)
' rotate the grey vector into positive Z
mag = CSng(Math.Sqrt(2.0))
xrs = CSng(1.0# / mag)
xrc = CSng(1.0# / mag)
XRotateMatrix(mmat, xrs, xrc)
mag = CSng(Math.Sqrt(3.0#))
yrs = CSng(-1.0# / mag)
yrc = CSng(Math.Sqrt(2.0#) / mag)
YRotateMatrix(mmat, yrs, yrc)
' shear the space to make the luminance plane horizontal
Transform3dPointMatrix(mmat, RLUM, GLUM, BLUM, lx, ly, lz)
zsx = lx / lz
zsy = ly / lz
ZShearMatrix(mmat, zsx, zsy)
' rotate the hue
zrs = CSng(Math.Sin(rot * Math.PI / 180.0#))
zrc = CSng(Math.Cos(rot * Math.PI / 180.0#))
ZRotateMatrix(mmat, zrs, zrc)
' unshear the space to put the luminance plane back
ZShearMatrix(mmat, -zsx, -zsy)
' rotate the grey vector back into place
YRotateMatrix(mmat, -yrs, yrc)
XRotateMatrix(mmat, -xrs, xrc)
MultiplyMatrix(mmat, mat, mat)
End Sub
Public Function DrawImageWithMatrix(ByVal img As Image, ByVal cm As ColorMatrix) As Boolean
Try
Dim bmp As New Bitmap(img) ' create a copy of the source image
Dim imgattr As New ImageAttributes
Dim rc As New Rectangle(0, 0, img.Width, img.Height)
Dim g As Graphics = Graphics.FromImage(img)
' associate the ColorMatrix object with an ImageAttributes object
imgattr.SetColorMatrix(cm)
' draw the copy of the source image back over the original image,
'applying the ColorMatrix
g.DrawImage(bmp, rc, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgattr)
g.Dispose()
Return True
Catch
Return False
End Try
End Function
End Module
|
|
|
|
|
What are the range of values that can be passed into for the HueRotateMatrix Value parameter? -180 to 180? -1 to 1? 0 to 360?
|
|
|
|
|
HueRotateMatrix() takes 0 to 360 (0 being normal)
SaturateMatrix() takes 0 to 2 (1 being normal)
ColourScaleMatrix() (Brightness) takes 0 to 255 (I think, havn't tried it yet)
Check this link for more information
http://www.sgi.com/misc/grafica/matrix/[^]
|
|
|
|
|
Sub OffsetMatrix change:
Dim mmat as ColorMatrix = _
New ColorMatrix(New Single()() _
{New Single() {1.0, 0.0, 0.0, 0.0, 0.0}, _
New Single() {0.0, 1.0, 0.0, 0.0, 0.0}, _
New Single() {0.0, 0.0, 1.0, 0.0, 0.0}, _
New Single() {0.0, 0.0, 0.0, 1.0, 0.0}, _
New Single() {roffset, goffset, boffset, 0.0, 1.0}})
|
|
|
|
|
Public Function Translucent(ByVal img As Image) As Boolean
Dim cm As New Imaging.ColorMatrix(New Single()() { _
New Single() {1.0F, 0.0F, 0.0F, 0.0F, 0.0F}, _
New Single() {0.0F, 1.0F, 0.0F, 0.0F, 0.0F}, _
New Single() {0.0F, 0.0F, 1.0F, 0.0F, 0.0F}, _
New Single() {0.0F, 0.0F, 0.0F, 0.0F, 0.0F}, _
New Single() {0.0F, 0.0F, 0.0F, 0.4F, 1.0F}})
Return draw_adjusted_image(img, cm)
End Function
Thanks,
Michaelas10;)
|
|
|
|
|
Hay There,
This article looks to have what i need. I copied the code to get started and now i get an error Type expected
My code looks like:
Imports System.Drawing.Imaging
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container
Me.Text = "Form1"
End Sub
#End Region
Public Function translate(ByVal img As Image, ByVal red As Single, _
ByVal green As Single, ByVal blue As Single, _
Optional ByVal alpha As Single = 0) As Boolean
Dim sr, sg, sb, sa As Single
' noramlize the color components to 1
sr = red / 255
sg = green / 255
sb = blue / 255
sa = alpha / 255
' create the color matrix
Dim cm As 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() {sr, sg, sb, sa, 1}})
' apply the matrix to the image
Return draw_adjusted_image(img, cm)
End Function
End Class
VB.NET tells me that the the type expected error is under ColorMatrix
Can anyone tell me what i'am doing wrong?
|
|
|
|
|
Hi BigBertB,
you're missing the path to ColorMatrix. Either use an imports-statement (Imports System.Drawing.Drawing2D) at the top of your class or precede the full path when declaring your cm-variable.
BTW - in cases like this, hit F2 (Object Browser) and locate an object. Use the "Find Symbol"-command to search for the object you don't know the path to (either by Edit/Find and Replace/Find Symbol or using the commandbar's button on the RHS)
Cheers,
Olaf
|
|
|
|
|
Imports System.Drawing.Imaging.ColorMatrix
|
|
|
|
|
Michael,
This is a great (if not the better!) article for color adjustment. Thank You a lot for share it with us.
Best ragards
Khe ps
Merry Christmas
|
|
|
|
|
Good article, I understand what's going on, but how on earth do you impliment it. I've been playing with it for about an hour and I still can't get it to display the image with the new Colormatrix...
|
|
|
|
|
Michael,
Thank you for the excellent article.
I've noticed a little issue with the way you define your negative matrix. With your definition of the matrix pure black colors (0,0,0,x) will not be inverted. Some other strange color "artifacts" could be produced as well.
I believe that a better negative could be produced by subtracting current color components from 255 (from 1 in the matrix world). To achieve this you should scale components by -1 and then translate them by 1. Following matrix should do the trick:
{-1, 0, 0, 0, 0}
{ 0, -1, 0, 0, 0}
{ 0, 0, -1, 0, 0}
{ 0, 0, 0, 1, 0}
{ 1, 1, 1, 0, 1}
Thank you.
|
|
|
|
|
Yeah, I came up with the same matrix you did. It does produce a better looking negative image.
|
|
|
|
|
With the original definition all the values where out by 1, so when a color should have been 255 it was 0. For some reason when the values in the last row are 0 the results from the matrix are rolled over (eg 256 becomes 0) but when the values in the last row are no zero the results are clamped (eg 256 becomes 255). I'm not sure if this is a bug or feature but the original example shouldn't work at all because the colors all come out to be negative so should really be clamped at zero.
|
|
|
|
|
Hi
i have tried this code. its working okay. i have a question. when i change the color of picture by giving red, green, blue color values. it displays the new picture on the picturebox. but when i minimize the form and then maximize it, the picture is no more there.
how can i save the changed picture because i need to store this new changed picture in database.
thanks a lot in advance.
bye
|
|
|
|
|
It really depends on how you are using the code. It sounds like you are updating an image using the image property of a picturebox. I would suggest loading the image from disk into an image object and then making modifications to that object. You can then resave the image to disk, display the results in a picturebox, etc. Otherwise, I would suggest reading up on the paint related events and properties of a picturebox control.
-Michael Combs
|
|
|
|
|
hey Michael
sorry, actually i m new to VB.NET. i have worked much on Java. now i m switching to VB.NET just to change taste
anyway, can u write a small code to save this picture.
i hope u will not mind it
thanks a lot in advance
|
|
|
|
|