Results 1 to 17 of 17

Thread: ImageControl Brightness propertie on Excel Worksheet

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2016
    Posts
    106

    ImageControl Brightness propertie on Excel Worksheet

    Hi,

    i want get and change the Brightness propertie of picture on ImageControl on Excel Worksheet.
    i am not want use setpixel or getpixel simple change of colors, but the properties of colors of picture.
    or using color constants Declarations.

    i know here not is forum of Microsoft Office but it is more graphics programming hardcore and VB programmers more advanced knowledge about this.

    please patience.

    thank you.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jun 2016
    Posts
    106

    Re: ImageControl Brightness propertie on Excel Worksheet

    Hi, Arnoutdv,

    you are welcome.

  3. #3
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: ImageControl Brightness propertie on Excel Worksheet

    There is no property to change the brightness of an image, or a specific color in an image.
    You have to parse all pixels, retrieve the color, update the color and put it back in the image.
    Only 256 and less color images have a color palette which can be updated. Other color depth have a color per X,Y.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2016
    Posts
    106

    Re: ImageControl Brightness propertie on Excel Worksheet

    Hi Arnoutdv , i am not want change the brightness, i want check the brightness, get the value of brightness and use it to compare in the code below.


    Like this:

    Code:
    Private Const WHITENESS   As Int32 = &HFF0062 
    Private Const BLACKNESS   As Int32 = &H42
    Code:
      For X As Integer = 0 To (Bs.Width - 1)
        For Y As Integer = 0 To (Bs.Height - 1)
          C = Bs.GetPixel(X, Y)
          If C.GetBrightness() < 0.5 Then
    Here a simple code of picture in worksheet (not in picturebox) I need in ImageControl(picturebox)

    Code:
    =========================
    Set myDocument = Worksheets(1) 
    myDocument.Shapes(1).PictureFormat.Brightness = 0.3
    =========================
    Last edited by xman2000; May 18th, 2017 at 09:05 AM.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: ImageControl Brightness propertie on Excel Worksheet

    Thread moved from the 'VB6' forum to the 'Office Development/VBA' forum (while VBA and VB6 have some similarities, they are not the same thing - especially when it comes to controls etc)

  6. #6
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: ImageControl Brightness propertie on Excel Worksheet

    Sorry don't have clue what this is about, I don't do Excel programming

    But let's get started:
    You have an Excel Shape object, which has a property PictureFormat.Brightness

    You also have some code:
    Code:
    C = Bs.GetPixel(X, Y)
    If C.GetBrightness() < 0.5 ...
    What kind of object is C?
    It seems to return the color (that's what GetPixel does).
    But where does .GetBrightness on a color come from?

    Is this a working project, or did you modify some samples found on the internet?

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2016
    Posts
    106

    Re: ImageControl Brightness propertie on Excel Worksheet

    Hi Arnoutdv,

    i am want adapt some vectorizations samples, but the problem is some details.
    a simple pictrue in worksheet get the brightness but not have a handle.
    a picture in ImageControl have a handle but not simple get a brightness in simple way i am not know.

    i need get a value of brightness of picture in picturebox, but i am not understand how create a brightness constant.
    it is a other edge contour algorithm

    "Dim C As Color"


    Code:
      Dim C As Color
      Dim X1, Y1 As Integer
      
    
      Dim G As Graphics
      G = Graphics.FromImage(picDest.Image)
      G.Clear(Color.FromArgb(255, 255, 0, 0))

  8. #8
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: ImageControl Brightness propertie on Excel Worksheet

    What version of Excel and VBA are you using?

    My version of Excel, 2010, does not support
    Code:
    Dim C As Color
    Dim G As Graphics
    Color and Graphics are not valid objects, unless you are using a .Net variant of VBA

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2016
    Posts
    106

    Re: ImageControl Brightness propertie on Excel Worksheet

    I use Excel 2013 Pro original version but i use GDi32 Getpixel to make a color, i put here the original code sample from internet i am want adapt his.

    problem is get color value of the brightness of a picture in imageBox.
    I am know maybe use a simple picture in worksheet to get the brightness but it is not a solution.

    is possible get brightness combining or compare colors black and withe values ?

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2016
    Posts
    106

    Re: ImageControl Brightness propertie on Excel Worksheet

    example of Getpixel in Excel vba

    Code:
    Sub Picture1_Click()
        Dim pLocation As POINT
        Dim lColour As Long
    
        Dim lDC As Variant
        lDC = GetWindowDC(0)
        Call GetCursorPos(pLocation)
        lColour = GetPixel(lDC, pLocation.x, pLocation.y)
        Range("a1").Interior.Color = lColour
    End Sub

    Declarations

    Code:
    #If VBA7 Then
        Private Declare PtrSafe Function GetPixel Lib "gdi32" (ByVal hdc As LongPtr, ByVal x As Long, ByVal y As Long) As Long
        Private Declare PtrSafe Function GetCursorPos Lib "user32" (ByRef lpPoint As POINT) As LongPtr
        Private Declare PtrSafe Function GetWindowDC Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
    #Else
        Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long,     ByVal y As Long) As Long
        Private Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As POINT) As Long
        Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
    #End If
    Private Type POINT
        x As Long
        y As Long
    End Type

  11. #11
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: ImageControl Brightness propertie on Excel Worksheet

    This code works for you?
    Code:
    Dim C As Color
    Dim G As Graphics
    Dim X1, Y1 As Integer
      
    G = Graphics.FromImage(picDest.Image)
    G.Clear(Color.FromArgb(255, 255, 0, 0))
    And further you are mixing all kind of code samples it seems.
    VB6, VBA, VBA7 and VB.Net

    I really can't help you any further because my Excel does not support VB.Net

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jun 2016
    Posts
    106

    Re: ImageControl Brightness propertie on Excel Worksheet

    Hi Arnoutdv ,

    i am not using vb.net, only vba, adapting vb to vba Excel

    i am not mixing the sample codes, only making differents new projects.
    but vba not equal vb or vbnet and i need adapt.

    i am not need you adapt vbNet to Excel, only get or combine colors to get brightness constant of a picture on picturebox wiht GDI32 GextPixel.

    i am found a way to set de brightness wiht a value of input of user, but not get the value from picture.

    http://vbnet.mvps.org/index.html?cod.../setpixelv.htm
    "http://vbnet.mvps.org/index.html?code/bitmap/setpixelv.htm"

    Code:
    
    
    Private Sub Command1_Click()
    
      'variables for brightness, colour calculation, positioning
       Dim Brightness As Single
       Dim NewColour As Long
       Dim pixHdc As Long   
       Dim x As Long, y As Long
       Dim r As Integer, g As Integer, b As Integer
       
      'change the brightness to a percent
       Brightness = CSng(Val(Text1.Text) / 100)
       
       pixHdc = Picture1.hDC
       
      'run a loop through the picture to change every pixel
       For x = 0 To Picture1.ScaleWidth
       
          For y = 0 To Picture1.ScaleHeight
          
            'get the current colour value
             NewColour = GetPixel(pixHdc , x, y)
          
            'extract the R,G,B values from the long returned by GetPixel
             r = (NewColour Mod 256)
             b = (Int(NewColour \ 65536))
             g = ((NewColour - (b * 65536) - r) \ 256)
          
            'change the RGB settings to their appropriate brightness
             r = r * Brightness
             b = b * Brightness
             g = g * Brightness
          
            'make sure the new variables aren't too high or too low
             If r > 255 Then r = 255
             If r < 0 Then r = 0
             If b > 255 Then b = 255
             If b < 0 Then b = 0
             If g > 255 Then g = 255
             If g < 0 Then g = 0
          
            'set the new pixel
             SetPixelV pixHdc, x, y, RGB(r, g, b)
          
         'continue through the loop
          Next y
          
         'refresh the picture box
         '(a nice visual progress effect)
          Picture1.Refresh
       
       Next x
       
      'final picture refresh
       Picture1.Refresh
       
    End Sub
    Code:
    Add a picture box (Picture1, a command button (Command1) and a textbox to the form. Set the picture box ScaleMode to 3-Pixels, and AutoRedraw to True. Add the following code to the form:
    Option Explicit
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
    ' Some pages may also contain other copyrights by the author.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Distribution: You can freely use this code in your own
    '               applications, but you may not reproduce 
    '               or publish this code on any web site,
    '               online service, or distribute as source 
    '               on any media without express permission.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Private Declare Function SetPixelV Lib "gdi32" _
      (ByVal hDC As Long, _
       ByVal x As Long, _
       ByVal y As Long, _
       ByVal crColor As Long) As Byte
       
    Private Declare Function GetPixel Lib "gdi32" _
      (ByVal hDC As Long, _
       ByVal x As Long, _
       ByVal y As Long) As Long

  13. #13
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: ImageControl Brightness propertie on Excel Worksheet

    How are you using the following code, posted by you in post #7, if you don't use VB.NET?
    Code:
    Dim C As Color
      Dim X1, Y1 As Integer
      
      Dim G As Graphics
      G = Graphics.FromImage(picDest.Image)
      G.Clear(Color.FromArgb(255, 255, 0, 0))
    i am found a way to set de brightness wiht a value of input of user, but not get the value from picture.
    You are not really setting the brightness, but just increasing the R,G,B values.

    What you really need is converting from RGB to HSV (or HLS)
    Then update the brightness (lightness) and convert it back to RGB

    Explanation:
    http://www.rapidtables.com/convert/color/rgb-to-hsv.htm

    Some source samples:
    http://www.xtremevbtalk.com/tech-dis...b-hsv-rgb.html
    http://www.gsericksen.com/2014/03/ex...or-conversion/
    http://www.vb-helper.com/howto_rgb_to_hls.html
    Last edited by Arnoutdv; May 19th, 2017 at 02:53 AM.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jun 2016
    Posts
    106

    Re: ImageControl Brightness propertie on Excel Worksheet

    Hi, Arnoutdv ,
    Sorry, I did not explain it correctly.

    i am need make somethings in VBA Excel and i am not if is possible or how to make.
    i am need translate some codes of VB and VBnet to VBA.

    i am not using vbNet in Excel.
    the code VBNet is the original code, i am I'm trying to translate it, Because they are the best and easiest codes I could find.

    i am need translate this code to VBA Excel:

    Code:
    Dim C As Color
      Dim X1, Y1 As Integer
      
      Dim G As Graphics
      G = Graphics.FromImage(picDest.Image)
      G.Clear(Color.FromArgb(255, 255, 0, 0))
    and i need get or adjust the brightness propertie in a ImageControl in Worksheet

    Code:
    C = Bs.GetPixel(X, Y)
    If C.GetBrightness() < 0.5 ...
    the Color.FromArgb i am will work after.

    edit:
    i think it maybe the solution workaround to Brightness

    https://www.experts-exchange.com/que...-a-bitmap.html
    "
    https://www.experts-exchange.com/questions/10027489/Calculating-'brightness'-of-a-bitmap.html"

    Code:
    Private Function GetBrightness(picX As PictureBox, nSamples As Integer, nX As Integer, nY As Integer) As Single
        Dim I As Integer
        Dim nRGB As Long
        Dim nRed As Integer
        Dim nGreen As Integer
        Dim nBlue As Integer
        Dim nGray As Integer
        Dim nRedSum As Long
        Dim nGreenSum As Long
        Dim nBlueSum As Long
        Dim nSum As Long
        
        For I = 1 To nSamples
            nRGB = GetPixel(picX.hdc, Int(Rnd() * nX), Int(Rnd() * nY))
            nRedSum = nRedSum + Int(nRGB / &H10000 And &HFF)
            nGreenSum = nGreenSum + Int(nRGB / &H100 And &HFF)
            nBlueSum = nBlueSum + Int(nRGB And &HFF)
        Next I
        nSum = (0.3 * nRedSum + 0.59 * nGreenSum + 0.11 * nBlueSum) / nSamples
        GetBrightness = 100 * nSum / 255
    End Function
    
    I posted my sample to  '-email removed by moderator-'  which address you gave in an earlier note.
    
    My address is '-email removed by moderator-'
    Last edited by dday9; May 19th, 2017 at 03:15 PM. Reason: Removed Emails

  15. #15
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: ImageControl Brightness propertie on Excel Worksheet

    For the last time, you can not just convert the Graphics from the VB.Net library to VB6 code.
    There is no one to one translation, because it's a huge .Net library and there is no VBA/VB6 equivalent.

    You are collecting all kind of code samples from the internet without investigating how thinks work and how to adapt them for your situation.

    I don't know how to help you anymore. So I'm out of this thread.

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Jun 2016
    Posts
    106

    Re: ImageControl Brightness propertie on Excel Worksheet

    Quote Originally Posted by Arnoutdv View Post
    For the last time, you can not just convert the Graphics from the VB.Net library to VB6 code.
    There is no one to one translation, because it's a huge .Net library and there is no VBA/VB6 equivalent.

    You are collecting all kind of code samples from the internet without investigating how thinks work and how to adapt them for your situation.

    I don't know how to help you anymore. So I'm out of this thread.

    Hi, Arnoutdv,
    What I'm trying to do is what you said now,What I'm trying to do is what you said now:
    "investigating how thinks work and how to adapt them for your situation."

    but i am not a expert and my English is poor, I can not express myself fully or Or upload my full project sample file.

    thank you very very mutch for your help.

  17. #17
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: ImageControl Brightness propertie on Excel Worksheet

    I've removed the emails that were included to prevent spammers from picking them up.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width