Convert Color image to Gray Shade Image

S

Sugan

Hi all,

I'm creating a custom button control. I need to show a color image when
the button control is enabled and a gray shade of the same image when
the button control is disabled. How can i convert a color image to a
gray image in VB 2005.

Thanks,
Sugan
Chennai, INDIA
 
P

Paul Larson

Sugan said:
Hi all,

I'm creating a custom button control. I need to show a color image when
the button control is enabled and a gray shade of the same image when
the button control is disabled. How can i convert a color image to a
gray image in VB 2005.

Thanks,
Sugan
Chennai, INDIA


Try this:

Private Function Convert2Greyscale(ByVal BitmapIn As System.Drawing.Bitmap)
As System.Drawing.Bitmap
Dim newB As Bitmap = CType(BitmapIn.Clone(), Bitmap)
Dim bounds As System.Drawing.Rectangle = New System.Drawing.Rectangle(0,
0, newB.Width, newB.Height)
Dim clrMatrix As System.Drawing.Imaging.ColorMatrix = New
System.Drawing.Imaging.ColorMatrix
Dim mX, mY As Integer
For mX = 0 To 2
For mY = 0 To 2
clrMatrix(mX, mY) = 0.333333
Next
Next
Dim imgAttr As System.Drawing.Imaging.ImageAttributes = New
System.Drawing.Imaging.ImageAttributes
imgAttr.SetColorMatrix(clrMatrix)
Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(newB)
g.DrawImage(newB, bounds, 0, 0, newB.Width, newB.Height,
System.Drawing.GraphicsUnit.Pixel, imgAttr)
g.Dispose()
Return newB
End Function



HTH
Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top