Home All Groups Group Topic Archive Search About

Convert Color image to Gray Shade Image

Author
5 Jul 2006 6:51 AM
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

Author
5 Jul 2006 11:54 AM
Paul Larson
"Sugan" <vsu***@gmail.com> wrote in message
news:1152082297.774317.315540@b68g2000cwa.googlegroups.com...
> 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