Home All Groups Group Topic Archive Search About

Code to INVERT image in VB6

Author
1 Jun 2005 11:46 PM
jm7
I am looking for some simple code in VB6 that willl invert a greyscale
image.  ie:  Black to white, white to black  and all the other shades in
between.  Only needs to INVERT the portion of  the IMAGE that is visible on
the screen.

Thanks,

Author
2 Jun 2005 1:04 AM
Larry Serflaten
"jm7" <x@ht> wrote in message news:OLvEhQwZFHA.2412@TK2MSFTNGP10.phx.gbl...
> I am looking for some simple code in VB6 that willl invert a greyscale
> image.  ie:  Black to white, white to black  and all the other shades in
> between.  Only needs to INVERT the portion of  the IMAGE that is visible on
> the screen.

See if this gets you started:

LFS


Private Sub Form_Load()
  Picture1.Move 0, 0, 900, 900
  Picture1.AutoRedraw = True
  Picture1.BackColor = vbWhite
  Picture1.Line (0, 0)-Step(300, 300), &HC0C0C0, BF
  Picture1.Line (300, 0)-Step(300, 300), &H606060, BF
  Picture1.Line (600, 0)-Step(300, 300), vbBlack, BF
  Picture1.Picture = Picture1.Image

End Sub

Private Sub Picture1_Click()
  Picture1.PaintPicture Picture1, 0, 0, 900, 900, 0, 0, 900, 900, vbDstInvert
End Sub
Author
2 Jun 2005 4:18 AM
jm7
Thanks Larry,

Now that is truly efficient - one line of CODE!  Did the trick.

I will try and investigate the other RASTER OPCODES.

Is there some other very efficient coding that can also adjust Contrast and
brightness of a picture in a PictureBox?

Thanks again,

John
Author
2 Jun 2005 6:51 AM
Mike D Sutton
> Now that is truly efficient - one line of CODE!  Did the trick.
> I will try and investigate the other RASTER OPCODES.

The InvertRect() API call would be more efficient then the Binary ROP suggestion if you're looking for speed:

'***
Private Declare Function InvertRect Lib "User32.dll" ( _
    ByVal hDC As Long, ByRef lpRect As RectAPI) As Long
Private Declare Function GetClientRect Lib "User32.dll" ( _
    ByVal hWnd As Long, ByRef lpRect As RectAPI) As Long

Private Type RectAPI
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Private Sub Picture1_Click()
    Dim ClientArea As RectAPI ' Invert picture on click
    Call GetClientRect(Picture1.hWnd, ClientArea)
    Call InvertRect(Picture1.hDC, ClientArea)
End Sub
'***

> Is there some other very efficient coding that can also adjust Contrast and
> brightness of a picture in a PictureBox?

The SetColorAdjustment() API call will perform contrast adjustment for you when blitting to a DC but it's only supported
in WinNT based OS, so you would need to develop your own solution for 9x/Me if you support those.  The "Edge detection /
Edge snap / Filter kernel" demo on my site has brightness/contrast adjustment functions built into it's DIB class should
you prefer not to write your own solution and is OS unspecific.
Hope this helps,

    Mike


- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Author
2 Jun 2005 7:44 AM
Mike D Sutton
> The InvertRect() API call would be more efficient then the Binary ROP suggestion if you're looking for speed:

Sorry, that should be _ternary_ ROP with StretchBlt(), a binary ROP would be used by setting the DrawMode and using a
white brush, but both would likely be slower than InvertRect().
Cheers,

    Mike


- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Author
2 Jun 2005 11:31 AM
jm7
Thanks Mike,

I will only be needing this contrast control in WIN XP or WIN 2000 so I
guess The SetColorAdjustment() API call will be sufficient.
Can you show  me how this would be coded simply for a Picturebox control or
please point me to some sample code?

Thanks in advance,

John

> The SetColorAdjustment() API call will perform contrast adjustment for you
when blitting to a DC but it's only supported
> in WinNT based OS, so you would need to develop your own solution for
9x/Me if you support those.  The "Edge detection /
> Edge snap / Filter kernel" demo on my site has brightness/contrast
adjustment functions built into it's DIB class should
Author
2 Jun 2005 1:40 PM
Mike D Sutton
> I will only be needing this contrast control in WIN XP or WIN 2000 so I
> guess The SetColorAdjustment() API call will be sufficient.
> Can you show  me how this would be coded simply for a Picturebox control
> or
> please point me to some sample code?

I've got a demo of it somewhere round here, I'll post it to the site as soon
as I find it and clean it up a little.
If you want to have a hack at it yourself then have a look here for the
documentation:
http://msdn.microsoft.com/library/en-us/gdi/colors_5cs4.asp
Hope this helps,

    Mike


- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Author
4 Jun 2005 12:31 PM
Mike D Sutton
Ok, demo's uploaded - You'll find it on the files page under "Colour adjustment demo".
Hope this helps,

    Mike


- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Author
2 Jun 2005 1:06 AM
Randy Birch
This will do the entire image ... and re-invert it back to normal if pressed
again:

Option Explicit

Private Type BITMAP
   bmType As Long
   bmWidth As Long
   bmHeight As Long
   bmWidthBytes As Long
   bmPlanes As Integer
   bmBitsPixel As Integer
   bmBits As Long
End Type

Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal
hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long,
ByVal dwCount As Long, lpBits As Any) As Long
Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long,
ByVal dwCount As Long, lpBits As Any) As Long

Private Sub Command1_Click()

   Dim PicBits() As Byte
   Dim PicInfo As BITMAP
   Dim cnt As Long

   GetObject Picture1.Image, Len(PicInfo), PicInfo

   ReDim PicBits(1 To (PicInfo.bmWidth * PicInfo.bmHeight) * 4) As Byte

   GetBitmapBits Picture1.Image, UBound(PicBits), PicBits(1)

   For cnt = 1 To UBound(PicBits)
      PicBits(cnt) = 255 Xor (PicBits(cnt))
   Next cnt

   SetBitmapBits Picture1.Image, UBound(PicBits), PicBits(1)

   Picture1.Refresh

End Sub

--

Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/
----------------------------------------------------------------------------
Read. Decide. Sign the petition to Microsoft.
http://classicvb.org/petition/
----------------------------------------------------------------------------



Show quoteHide quote
"jm7" <x@ht> wrote in message news:OLvEhQwZFHA.2412@TK2MSFTNGP10.phx.gbl...
:I am looking for some simple code in VB6 that willl invert a greyscale
: image.  ie:  Black to white, white to black  and all the other shades in
: between.  Only needs to INVERT the portion of  the IMAGE that is visible
on
: the screen.
:
: Thanks,
:
:
Author
2 Jun 2005 4:20 AM
jm7
Thanks Randy,

Haven't tried your code yet but the only liner in LSF's sample was too
brilliant.

I am hoping there is just as easy a way of doing brightness and contrast.

John