locked
GraphicsWindow.GetPixel generates an exception RRS feed

  • Question

  • Hello everyone,

    I'm trying to run a Small Basic program on a "Windows 10 Home Single Language" (a computer of one of my Thai students).

    I get the following error: "Exception has been thrown by the target of an invocation".

    On other systems (Windows 7, Windows 10...), the same program doesn't generate any error.

    Would anyone have an idea on the cause of this problem and, maybe, on a way to fix it?

    Thanks. :-)

    J.


    Monday, August 21, 2017 1:57 PM

Answers

  • col = LDGraphicsWindow.GetPixel(x,y)    would use a different approach, plz try that.

    I suppose, you did'nt change your Graphics settings (from default settings):

    Here are my (default) settings for eg. 'Intel HD Graphics', but should be similar for others too:

    RightClick Desktop background -> ContextMenu ->

    -> 'Graphics Properties...' -> opens  'Display settings' (CtrlPanel applet) -> Default settings

    or

    -> Menu item 'Graphics Options' (with following Subnodes):

    > Screen Adjustment:    Keep Display scaling ✓           !!!
    > Shortcut Keys:           On/Off however
    > Traysymbol:               On/Off  however
    > Resolution/Dimensions:  your current Width x Height (eg. 1366 x 768) ✓
    > Rotation:                   0 degrees ✓                              !!!
    > Profile:                      some video options

    The menu items (like 'Screen Adjustment' etc) are free translated from german, perhaps they are named different for your OS language.

    TextWindow.WriteLine("dpiX: "+ LDUtilities.DPIX)  ' should be same as for Y below !
    TextWindow.WriteLine("dpiY: "+ LDUtilities.DPIY)  ' should be same as for X above !
    GraphicsWindow.Left = 0
    GraphicsWindow.Top = 0
    TextWindow.WriteLine("moffX: "+ LDGraphicsWindow.MouseXOffset)  ' x-Pos of the GW client area from left (8 for GW left border/frame)
    TextWindow.WriteLine("moffY: "+ LDGraphicsWindow.MouseYOffset)  ' y-pos of the GW client area from top (30 for titlebar and GW border)


    Saturday, August 26, 2017 12:21 PM
    Answerer
  • Hi

    Try Downloading The LITDEV Extension Here = https://gallery.technet.microsoft.com/Small-Basic-LitDev-75827dc5

    And Use 

    colourofpixel = LDGraphicsWindow.GetPixel(x,y) 'This is to find the colour of pixel

    Thanks,


    Saturday, August 26, 2017 3:10 PM

All replies

  • You should post some code behind (or the special part), which causes the exception !

    Monday, August 21, 2017 9:17 PM
    Answerer
  • Tuesday, August 22, 2017 2:42 PM
  • Thanks.

    I worked on the problem for a few hours and here is what I can tell you.

    My program is the following:

    GraphicsWindow.Width = 0.8 * Desktop.Width
    GraphicsWindow.Height = 0.8 * Desktop.Height
    
    GraphicsWindow.DrawRectangle(1,1,GraphicsWindow.Width-1,GraphicsWindow.Height-1)
    GraphicsWindow.DrawText(0,0,"GraphicsWindow.Width = " + GraphicsWindow.Width) ' = 1440
    GraphicsWindow.DrawText(0,20,"GraphicsWindow.Height = " + GraphicsWindow.Height) ' = 897
    
    For i = 1 To GraphicsWindow.Width
      GraphicsWindow.SetPixel(i,100,"black")
    EndFor
    
    For i = 1 To 759 ' Changing 759 into 760 will generate an exception.
      GraphicsWindow.GetPixel(i,50)
    EndFor

    When I run it, I get this (knowing that the screen resolution of the tablet is 2160x1440):

    And the error message is:

       at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
       at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
       at System.Delegate.DynamicInvokeImpl(Object[] args)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.ProcessQueue()
       at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at Microsoft.SmallBasic.Library.Internal.SmallBasicApplication.<>c__DisplayClass1.<.cctor>b__0()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

    Any idea?

    Thanks in advance for any help you are able to provide. :-)

    J.


    Wednesday, August 23, 2017 3:45 PM
  • I guess it's about your screen resolution. Similar problem there was some time ago, look here if this thread can help you:

    mouse-object-bug

    small-basic-mouse-position

    related to 'LitDev' commands:

    LDGraphicsWindow.MouseXOffset
    LDGraphicsWindow.MouseXScale
    LDGraphicsWindow.MouseYOffset
    LDGraphicsWindow.MouseYScale

    LDUtilities.DPIX
    LDUtilities.DPIY

    Also found 1 usage example with these commands from Tryhest (ID: FKF800-0) on challenge-of-the-month-may-2017

    Wednesday, August 23, 2017 5:11 PM
    Answerer
  • Thanks for your answer.

    I tried all of the 22 possible screen resolutions proposed by that tablet:
    2160x1440
    2048x1152
    1920x1440
    1620x1200
    1920x1080
    1856x1392
    1792x1344
    1680x1050
    1600x1200
    1600x900
    1440x900
    1400x1050
    1366x768
    1280x1024
    1280x960
    1280x800
    1280x768
    1280x720
    1280x600
    1152x864
    1024x768
    800x600

    My program generated the same error message ("Exception has been thrown by the target of an invocation") each time.

    So... any other idea on how to fix the problem?

    J.


    Thursday, August 24, 2017 10:11 AM
  • Please ensure:

    * All systems (Win7, Win10) esp. the one 'Win10 Home Single Language' are running the same SmallBasic version?? (eg. SB 1.2)

    * The used decimal separator (like in  0.8 * Desktop.Width)  is your system default  (point vs. comma) ??

    * Ensure about your GraphicsWindow.Width!  You can show it by eg.

    GraphicsWindow.Title = "W = "+ GraphicsWindow.Width +" | DtW = "+ Desktop.Width     ' before the 'For'  loops

    * Note that x/y coordinates in the GraphicsWindow range from:

    x ..  0 to GraphicsWindow.Width-1

    y ..  0 to GraphicsWindow.Height-1

     ... like used in GetPixel(x,y)  and  SetPixel(x,y, color)

    * ?? Something unclear about dimensions:

    you say: screen resolution (Desktop.Width x .Height) of the tablet = 2160x1440

    GraphicsWindow.Width = 0.8 * Desktop.Width  =  0.8 * 2160  = 1728  (NOT 1440)

    GraphicsWindow.Height = 0.8 * Desktop.Height  = 0.8 * 1440 =  1152  (NOT 897)

    Generally it would be better (and shorter) to use variables, like this:

    gw = 0.8 * Desktop.Width
    gh = 0.8 * Desktop.Height
    GraphicsWindow.Width = gw
    GraphicsWindow.Height = gh
    
    GraphicsWindow.DrawRectangle(1,1, gw-1,gh-1)
    GraphicsWindow.DrawText(0,0, "GraphicsWindow.Width = "+ gw)
    GraphicsWindow.DrawText(0,20, "GraphicsWindow.Height = "+ gh)
    GraphicsWindow.Title = "W = "+ gw +" | DtW = "+ Desktop.Width
    
    For i = 1 To gw-1
      GraphicsWindow.SetPixel(i,100, "Black")
    EndFor
    
    For i = 1 To Math.Min(gw-1, 760) ' To ??  Changing 759 into 760 will generate an exception.
      GraphicsWindow.GetPixel(i,50)
    EndFor




    Thursday, August 24, 2017 1:14 PM
    Answerer
  • Thank you very much for your help.

    Yes, I'm running Small Basic 1.2 on all systems.

    Here is what I get with your program:

    J.

    Friday, August 25, 2017 12:59 AM
  • Hmmm,  same Exception message like above, and does'nt like the GraphicsWindow.GetPixel(760,50) ?!

    col = GraphicsWindow.GetPixel(760,50)  ' should return #000000

    Reduced to a minimum and only using integers:

    gw = Math.Floor(0.8 * Desktop.Width)  ' 1024.0
    gh = Math.Floor(0.8 * Desktop.Height) ' 614.4
    
    GraphicsWindow.Top = 0
    GraphicsWindow.Left = 0
    GraphicsWindow.Width = gw
    GraphicsWindow.Height = gh
    GraphicsWindow.Title = "WxH = "+ gw +"x"+ gh +" | DtW = "+ Desktop.Width  ' 1024.0 ; 1280
    
    'For i = 1 To Math.Min(gw - 1, 760)  ' To ??  Changing 759 into 760 will generate an exception.
    '  col = GraphicsWindow.GetPixel(i, 50)
    ''  TextWindow.WriteLine(i +": "+ col)
    'EndFor
    TextWindow.WriteLine(GraphicsWindow.GetPixel(760, 50))
    If this does'nt work either, i dont know ...



    Friday, August 25, 2017 11:57 AM
    Answerer
  • Thank you Pappa Lapub.

    Unfortunately, it doesn't work either. Below 759, no problem. Above, it generates an exception.

    I suppose the problem comes from Small Basic's GraphicsWindow. Whatever the numbers I feed GraphicsWindow.Width and GraphicsWindow.Height with are, the window will always occupy the totality of the screen.

    For example, if I set the screen resolution of the tablet to 1280x768 and I explicitly ask Small Basic for a half-screen window...

    GraphicsWindow.Width = 640
    GraphicsWindow.Height = 384

    ...I will get a full screen window.

    And if, just after this, I ask Small Basic to tell me the size of my window...

    GraphicsWindow.DrawText(0,0,"GraphicsWindow.Width = " + GraphicsWindow.Width)
    GraphicsWindow.DrawText(0,20,"GraphicsWindow.Height = " + GraphicsWindow.Height)

    ...the answer will be 1280 x 705.

    Functions GraphicsWindow.Left and GraphicsWindow.Top don't work either. The window will always start from (0,0).

    Any idea?

    J.


    PS: I've just uninstalled SB / rebooted the system / reinstalled SB (1.2). It doesn't change anything.
    Saturday, August 26, 2017 8:37 AM
  • Hi ,

    Try Upgrading Your Small Basic To 1.3, Its Available On The Window Store Here = https://www.microsoft.com/en-us/store/p/small-basic/9pfqkjljpb1r 

    This Can Be Downloaded Only If You Have The Following System Requirements

    Thanks,

    Saturday, August 26, 2017 8:56 AM
  • Thanks. I can download it but nothing happens when I click the "Launch" button.
    (In the Task Manager, I can see "SB.exe" appear and disappear immediately. "Run as administrator" doesn't help either.)

    J.

    Saturday, August 26, 2017 10:11 AM
  • Hi,

    Try Reinstalling The Small Basic 1.2

    Thanks,

    Saturday, August 26, 2017 10:26 AM
  • It didn't work either. (See the "PS" in my previous message.)

    J.

    Saturday, August 26, 2017 10:30 AM
  • GraphicsWindow.Top =0.8* Desktop.Height 
    GraphicsWindow.Width =0.8* Desktop.Width
    TextWindow.WriteLine(GraphicsWindow.Top)
    TextWindow.WriteLine(GraphicsWindow.Width)

    Hi ,

    Does The Following Code Causes Exception

    Thanks,

    Saturday, August 26, 2017 10:35 AM
  • No. Only
    GraphicsWindow.GetPixel(x,y)
    with x >= 760 causes an exception.

    Saturday, August 26, 2017 11:53 AM
  • col = LDGraphicsWindow.GetPixel(x,y)    would use a different approach, plz try that.

    I suppose, you did'nt change your Graphics settings (from default settings):

    Here are my (default) settings for eg. 'Intel HD Graphics', but should be similar for others too:

    RightClick Desktop background -> ContextMenu ->

    -> 'Graphics Properties...' -> opens  'Display settings' (CtrlPanel applet) -> Default settings

    or

    -> Menu item 'Graphics Options' (with following Subnodes):

    > Screen Adjustment:    Keep Display scaling ✓           !!!
    > Shortcut Keys:           On/Off however
    > Traysymbol:               On/Off  however
    > Resolution/Dimensions:  your current Width x Height (eg. 1366 x 768) ✓
    > Rotation:                   0 degrees ✓                              !!!
    > Profile:                      some video options

    The menu items (like 'Screen Adjustment' etc) are free translated from german, perhaps they are named different for your OS language.

    TextWindow.WriteLine("dpiX: "+ LDUtilities.DPIX)  ' should be same as for Y below !
    TextWindow.WriteLine("dpiY: "+ LDUtilities.DPIY)  ' should be same as for X above !
    GraphicsWindow.Left = 0
    GraphicsWindow.Top = 0
    TextWindow.WriteLine("moffX: "+ LDGraphicsWindow.MouseXOffset)  ' x-Pos of the GW client area from left (8 for GW left border/frame)
    TextWindow.WriteLine("moffY: "+ LDGraphicsWindow.MouseYOffset)  ' y-pos of the GW client area from top (30 for titlebar and GW border)


    Saturday, August 26, 2017 12:21 PM
    Answerer
  • Hi

    Try Downloading The LITDEV Extension Here = https://gallery.technet.microsoft.com/Small-Basic-LitDev-75827dc5

    And Use 

    colourofpixel = LDGraphicsWindow.GetPixel(x,y) 'This is to find the colour of pixel

    Thanks,


    Saturday, August 26, 2017 3:10 PM
  • To answer Pappa Lapub:

    Even in Thai, the Graphics options seem to be normal.

    I'm not sure I understood your explanations concerning DPIX, DPIY and LDGraphicsWindow.MouseX/YOffset, but here is what I get:

    dpiX: 96
    dpiY: 96
    moffX: 0
    moffY: 23

    To answer Pappa Lapub and RoshanPriyaKumar:

    LDGraphicsWindow.GetPixel does not generate an exception and my program works (almost) normally using it: http://tinyurl.com/yaz4vh5q. There is (still) a problem with the display moving suddenly to the right and down with no apparent reason... (Again, this problem only appears on the tablet of one of my students.)

    So, if the problem of the exception generated by GraphicsWindow.GetPixel is not solved, at least I can now get around it.

    Thanks a lot for your help.

    J.

    Saturday, August 26, 2017 4:49 PM