Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Interface and Graphics > GDI+ brightness ???


Reply
 
Thread Tools Display Modes
  #1  
Old 09-29-2004, 01:59 PM
GregDuncan GregDuncan is offline
Regular
 
Join Date: Apr 2003
Location: Larn din Init
Posts: 70
Default GDI+ brightness ???


Does anyone know how to change the brightness of an image using GDI+ ?
Reply With Quote
  #2  
Old 09-29-2004, 07:06 PM
Jonny's Avatar
Jonny Jonny is offline
Senior Contributor
 
Join Date: Jun 2003
Location: Birmingham, England, UK
Posts: 821
Default Another Method

The principle of changing the brightness of an image is easy.
here is the pseudo to add 20 to the brightness...
Code:
For Each Pixel
    Get Pixel Color RGB
''''Add 20 to each colour in the pixel
    Pixel.R = Pixel.R + 20
    Pixel.G = Pixel.G + 20
    Pixel.B = Pixel.B + 20
''''Clip the values so they go no higher than 255
''''and no lower than 0
    If Pixel.R > 255 Then Pixel.R = 255
    If Pixel.G > 255 Then Pixel.G = 255
    If Pixel.B > 255 Then Pixel.B = 255
    If Pixel.R < 0 Then Pixel.R = 0
    If Pixel.G < 0 Then Pixel.G = 0
    If Pixel.B < 0 Then Pixel.B = 0
    
    Set New Pixel Color
Next Pixel
I have modified a blur image function that i posted earlier to change the brightness and have included the demo project in this post (see screenie). If you dont need to use GDI+ then feel free to use it.
If you have any questions please ask.
Attached Images
File Type: jpg brightness.jpg (18.2 KB, 5 views)
Attached Files
File Type: zip brightness.zip (6.3 KB, 24 views)
__________________
Regards
John, jlsd.co.uk
Reply With Quote
  #3  
Old 09-30-2004, 04:08 AM
GregDuncan GregDuncan is offline
Regular
 
Join Date: Apr 2003
Location: Larn din Init
Posts: 70
Default

Thanks for your reply.
While your code is very good I would like to find a GDI+ specif answer.
There are several ways to do what I want in GDI+ but I cant find any information or examples on it, only just enough to know that it is possible.
If anyone has any examples using matrices or applying effects then I would appreciate any info you can pass along.

Having said that I cant just sit here twiddling my thumbs so I gave your idea a go just to see if it would work.
Here is my code.
Public Function Lighten()
Dim retval As Long
Dim R As RECTL
Dim BMData As BitmapData
Dim bytecount As Long
Dim Pixels() As Byte
Dim Y As Integer
Dim X As Integer

R.Left = 0
R.Top = 0
R.Width = ImageWidth
R.Height = ImageHeight
'lock the bitmap
retval = GdipBitmapLockBits(m_Bitmap, R, ImageLockModeWrite, PixelFormat24bppRGB, BMData)

'setup values
bytecount = BMData.Width * BMData.Height
ReDim Pixels(bytecount - 1)
'copy data into our array for manipulation
CopyMemory Pixels(0), BMData.Scan0Ptr, bytecount
'change values
For Y = 0 To ImageHeight - 1
For X = 0 To ImageWidth - 1
Pixels(X + Y) = Pixels(X + Y) - 20
If Pixels(X + Y) < 0 Then Pixels(X + Y) = 0
Next
Next
'copy memory back
CopyMemory BMData.Scan0Ptr, Pixels(0), bytecount

retval = GdipBitmapUnlockBits(m_Bitmap, BMData)
End Function

Unfortunatly it doesnt work
It crashes on the 1st Copy memory line where Im trying to copy the pixel data into my byte array.

I havent used CopyMemory very often so if anyone can obviously see what im doing wrong then pls post a fix.

Cheers
Reply With Quote
  #4  
Old 09-30-2004, 04:57 AM
Jonny's Avatar
Jonny Jonny is offline
Senior Contributor
 
Join Date: Jun 2003
Location: Birmingham, England, UK
Posts: 821
Default

ok, i cant debug that code as i really need the project in front of me... but i am going to point you towards a GDI+ example, it does not include brightness but it might give you some ideas.
Scale, Rotate, Skew and Transform Images using GDI+
__________________
Regards
John, jlsd.co.uk
Reply With Quote
  #5  
Old 09-30-2004, 09:11 AM
OnErr0r's Avatar
OnErr0r OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
Default

That's a good start using LockBits.. but you have to remember that 24 bit BMP data is aligned on 4 byte boundries. So use a 2D array with the padded width and just access pixels up to the actual width. Instead of using RtlMoveMemory, you can pass VarPtr(my2DArray(0,0)) and use LockBits with these flags: ImageLockModeUserInputBuf Or ImageLockModeRead

Then your 2D array will be filled with the data.

One you've finished manipulating the pixels call LockBits again with ImageLockModeWrite Or ImageLockModeUserInputBuf to write the array.
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #6  
Old 09-30-2004, 09:16 AM
OnErr0r's Avatar
OnErr0r OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
Default

Btw, if you'll look at my gdiplus3.zip in the code library (specifically modQuantize.bas) you'll see this method in action. I use a 1D array, which is cumbersome (but I was trying to speed things along), which is why I suggest the 2D array.
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #7  
Old 09-30-2004, 12:40 PM
GregDuncan GregDuncan is offline
Regular
 
Join Date: Apr 2003
Location: Larn din Init
Posts: 70
Default

Thank you for your replies.
I have managed to succesfully apply a colour shift matrix using the GDI+ ImageAttributes functions and alter the brightness of my image.
I have included the function below for anyone else who might be interested.

Public Function AlterBrightness(ByVal howmuch As Single)
'applies a colour matrix to the image
'The ShiftInBrightness range is from -1 (complete blackness) to 1 (complete white)
'so to brighten an image 20% pass a value of 0.2, to darken it by 20% pass a value of -0.2
Dim retval As Long
Dim hAttributes As Long
Dim cmBrightness As ColorMatrix
Dim cmGrey As ColorMatrix

'create our transformation matrix
With cmBrightness
.m(0, 0) = 1
.m(1, 1) = 1
.m(2, 2) = 1
.m(3, 3) = 1
.m(0, 4) = howmuch
.m(1, 4) = howmuch
.m(2, 4) = howmuch
.m(4, 4) = 1
End With

'create an ImageAttribute which we will apply to our image
retval = GdipCreateImageAttributes(hAttributes)
'assign our matrix to the ImageAttribute
retval = GdipSetImageAttributesColorMatrix(hAttributes, ColorAdjustTypeDefault, True, cmBrightness, cmGrey, ColorMatrixFlagsDefault)

'overwrite our existing image rather than blend with it
retval = GdipSetCompositingMode(m_Graphic, CompositingModeSourceOver)

'the translation algorithem to use (InterpolationModeHighQualityBicubic produces the highest quality)
'not sure if this is necessary since documentation says it is used "when images are scaled or rotated"
retval = GdipSetInterpolationMode(m_Graphic, InterpolationModeHighQualityBicubic)

'Indicate that pixel centers have coordinates that are half way between integer values.
retval = GdipSetPixelOffsetMode(m_Graphic, PixelOffsetModeHighQuality)

'draw the image onto itself applying the matrix as it does so
retval = GdipDrawImageRectRectI(m_Graphic, m_Bitmap, 0, 0, ImageWidth, ImageHeight, 0, 0, ImageWidth, ImageHeight, UnitPixel, hAttributes)

'clean up
retval = GdipDisposeImageAttributes(hAttributes)

End Function
Reply With Quote
  #8  
Old 09-30-2004, 02:19 PM
GregDuncan GregDuncan is offline
Regular
 
Join Date: Apr 2003
Location: Larn din Init
Posts: 70
Default

The matrix to alter the contrast is supposed to be as follows

With cmContrast
.m(0, 0) = howmuch
.m(1, 1) = howmuch
.m(2, 2) = howmuch
.m(3, 3) = 1
.m(4, 4) = 1
End With

To halve the contrast you would set howmuch to be 0.5 and to double it you would use 2.

This does alter the image nicely if you use a value below 1 but i have found that taking it even fractionally above 1.0 causes my pictures to be ruined with globs of colour smeared all across them.

Perhaps it would be worthwhile trying other interpolation modes or something but all I needed was the brightness so im going to be stopping here.

If anyone continues this work then pls post your results.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->