screenshot code problem.

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 333
Joined: Wed Jan 20, 2010 1:15 pm

screenshot code problem.

Post by dkaip »

I make a mini screen-shot code. But put the rectangles not at start and end of mouse.
What i am wrong?
An some times background of all panel is altered. How to improve that? There is some wxwidget program to study?

Code: Select all

screenShotFrame::screenShotFrame(wxFrame *frame)
    : screenShot(frame)
{
    this->GetSize(&w,&h);
    wxCoord screenWidth, screenHeight;
    screenDC.GetSize(&screenWidth, &screenHeight);
    //this->SetSize(screenWidth,screenHeight);
    screenshot=wxBitmap(screenWidth, screenHeight,-1);
    memDC.SelectObject(screenshot);
    //Blit (in this case copy) the actual screen on the memory DC and thus the Bitmap
    memDC.Blit( 0, //Copy to this X coordinate
                0, //Copy to this Y coordinate
                screenWidth, //Copy this width
                screenHeight, //Copy this height
                &screenDC, //From where do we copy?
                0, //What's the X offset in the original DC?
                0  //What's the Y offset in the original DC?
              );
    //Select the Bitmap out of the memory DC by selecting a new
    //uninitialized Bitmap
    memDC.SelectObject(wxNullBitmap);
    image=screenshot.ConvertToImage();
    screenShotBitmap->SetBitmap(screenshot);
}

void screenShotFrame::screenCapturePart1( wxMouseEvent& event )
{
    x1=x2=y1=y2=0;
    wxPoint pt = wxGetMousePosition();
    x1= pt.x - this->GetScreenPosition().x;
    y1 = pt.y - this->GetScreenPosition().y;
}

void screenShotFrame::screenCapturePart2( wxMouseEvent& event )
{
    wxPoint pt = wxGetMousePosition();
    x2= pt.x - this->GetScreenPosition().x;
    y2 = pt.y - this->GetScreenPosition().y;
    DoPaint(this,*wxRED_PEN);
}

void screenShotFrame::DoPaint(wxWindow* wnd, const wxPen& linePen)
{
    wxSize s(wnd->GetClientSize());
    wxBufferedPaintDC dc(wnd);
    dc.SetPen(*wxRED_PEN);
    dc.SetBrush(*wxTRANSPARENT_BRUSH);
    dc.DrawRectangle(x1, y1, x2, y2);
}
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: screenshot code problem.

Post by doublemax »

Code: Select all

dc.DrawRectangle(x1, y1, x2, y2);
The parameters 3 and 4 are width and height, not absolute positions

Code: Select all

void screenShotFrame::screenCapturePart1( wxMouseEvent& event )
{
    x1=x2=y1=y2=0;
    wxPoint pt = wxGetMousePosition();
    x1= pt.x - this->GetScreenPosition().x;
    y1 = pt.y - this->GetScreenPosition().y;
}
If you would use event.GetPosition() you wouldn't have to do all this.
Use the source, Luke!
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 333
Joined: Wed Jan 20, 2010 1:15 pm

Re: screenshot code problem.

Post by dkaip »

Works just fine. A question please.
Why with old code the background is altered? With new code is just fine, without any modification. I don’t understand this behaviour.
Thanks.
Jim

Code: Select all

void screenShotFrame::screenCapturePart1( wxMouseEvent& event )
{
    x1=x2=y1=y2=0;
    //wxPoint pt = wxGetMousePosition();
//    x1= pt.x - this->GetScreenPosition().x;
//    y1 = pt.y - this->GetScreenPosition().y;
    pt1 = event.GetPosition();
}

void screenShotFrame::screenCapturePart2( wxMouseEvent& event )
{
    pt2 = event.GetPosition();
    dx=pt2.x-pt1.x;
    dy=pt2.y-pt1.y;
//    wxPoint pt = wxGetMousePosition();
//    x2= pt.x - this->GetScreenPosition().x;
//    y2 = pt.y - this->GetScreenPosition().y;
    DoPaint(this,*wxRED_PEN);
}

void screenShotFrame::DoPaint(wxWindow* wnd, const wxPen& linePen)
{
    wxSize s(wnd->GetClientSize());
    wxBufferedPaintDC dc(wnd);
    dc.SetPen(*wxRED_PEN);
    dc.SetBrush(*wxTRANSPARENT_BRUSH);
    //dc.DrawRectangle(x1, y1, x2-x1, y2-y1);
    dc.DrawRectangle(pt1.x,pt1.y,dx,dy);
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: screenshot code problem.

Post by doublemax »

I didn't run the code, so i don't really know what you meant with "altered background". Maybe there were some artifacts when the old code was trying to draw outside the window area?
Use the source, Luke!
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 333
Joined: Wed Jan 20, 2010 1:15 pm

Re: screenshot code problem.

Post by dkaip »

Sorry, wrong .., sometimes make problem with background. Maybe because i have two screens...
I am trying to figure out..
As i see when press mouse and release in same point make this every time. An some other times, i don’t know why. Code is very simple.
--------------
This happens every time that mouse leaves on vertical line that distinguishes the two screens.
Last edited by dkaip on Thu Oct 19, 2017 8:47 am, edited 2 times in total.
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 333
Joined: Wed Jan 20, 2010 1:15 pm

Re: screenshot code problem.

Post by dkaip »

I must send a picture before(1.png) and after (2.png) ...
This happens every time that mouse leaves on vertical line that distinguishes the two screens.
Attachments
2.png
2.png (8.47 KiB) Viewed 4339 times
1.png
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: screenshot code problem.

Post by doublemax »

I would need a working sample to try it out myself.

There are only two things that look a little suspicious:

Code: Select all

void screenShotFrame::DoPaint(wxWindow* wnd, const wxPen& linePen)
You're passing in a window pointer. Are you sure it's the correct one? How does the "real" paint event handler look like? In a paint event you should only draw onto the window that generated the event.

In the drawing code you're only drawing the lines, you're not erasing the background. Is that intended?
Use the source, Luke!
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 333
Joined: Wed Jan 20, 2010 1:15 pm

Re: screenshot code problem.

Post by dkaip »

Yes not erasing the background, because i want a rectangle's snapshot.
I send the file, but it linking against of topic libs of wxWidgets.
Attachments
screenShot.tar.gz
(4.23 KiB) Downloaded 111 times
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: screenshot code problem.

Post by doublemax »

I can't see how your code could ever have worked.

As there was a similar question a while ago, i decided to write a small class for grabbing a screen area. For simplicity it only uses the "fake" method that just grabs the whole screen and displays the bitmap full screen. I.e. if there are any "moving" parts on the "real" screen, you won't see that during the selection process. You'll just see a static bitmap. A proper solution is more effort, maybe i'll write that at a later time.

Sample code that shows how to use the class:

Code: Select all

void MyFrame::OnStartCapture(wxCommandEvent& WXUNUSED(event))
{
  ScreenSelectionDialog dlg(this);
  if( dlg.ShowModal() == wxID_OK )
  {
    // if you just want the selection rectangle and process the area yourself
    wxRect r = dlg.GetSelectionRect();
    
    // if you want the selected screen area as a bitmap
    ::wxInitAllImageHandlers();
    wxBitmap captured = dlg.GetCapturedArea();
    captured.SaveFile( "d:\\_captured.png", wxBITMAP_TYPE_PNG );
  }
}
Attachments
screen_selection_dialog.cpp
(3.99 KiB) Downloaded 121 times
screen_selection_dialog.h
(873 Bytes) Downloaded 105 times
Use the source, Luke!
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 333
Joined: Wed Jan 20, 2010 1:15 pm

Re: screenshot code problem.

Post by dkaip »

Thanks for reply.
CaptureMouse(); dont exist, so compiler say
../src/gtk/window.cpp(4918): assert ""window"" failed in DoCaptureMouse(): CaptureMouse() failed
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: screenshot code problem.

Post by doublemax »

That's not a compiler error, it's an assert at runtime. I work under Windows only, so i'm not sure why the code doesn't work under GTK. I can only assume that the CaptureMouse() in the dialog ctor is too early under GTK.

Try replacing the CaptureMouse() call with this:

Code: Select all

CallAfter([this] {
  CaptureMouse();
} );
Use the source, Luke!
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 333
Joined: Wed Jan 20, 2010 1:15 pm

Re: screenshot code problem.

Post by dkaip »

Compiler say..
../src/gtk/window.cpp(4918): assert ""window"" failed in DoCaptureMouse(): CaptureMouse() failed
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: screenshot code problem.

Post by doublemax »

I updated "screen_selection_dialog.cpp" in my original post. Try that version.
Use the source, Luke!
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 333
Joined: Wed Jan 20, 2010 1:15 pm

Re: screenshot code problem.

Post by dkaip »

I am truing to new version, but after click on screen stops at appbase.cpp line 1163 at wxTrap();
Have i something wrong?
---------------------------------------------
#0 0x5d9274 wxDefaultAssertHandler(file=..., line=1176, func=..., cond=..., msg=...) (../src/common/appbase.cpp:1163)
#1 ?? 0x00000000005d9be2 in wxOnAssert (file=0x767105 "../src/common/string.cpp", line=1176, func=0x7676d2 <wxString::FromAscii(char const*, unsigned long)::__FUNCTION__> "FromAscii", cond=0x7671ec "c < 0x80", msg=0x767150 L"Non-ASCII value passed to FromAscii().") (../src/common/appbase.cpp:1264)
#2 0x64e774 wxString::FromAscii(ascii=0xa5c8d6 <g_buf+22> "\224ΟΚΙΜΩΝ/screen_selection/screen_selection_dialog.cpp:105\n", len=64) (../src/common/string.cpp:1175)
#3 0x64e830 wxString::FromAscii(ascii=0xa5c8c0 <g_buf> "/home/a/MYDOC/bransh-ΔΟΚΙΜΩΝ/screen_selection/screen_selection_dialog.cpp:105\n") (../src/common/string.cpp:1187)
#4 0x680857 (anonymous namespace)::ReadLine(fp=0xd25c30, num=7, line=0x7fffffff6860) (../src/unix/stackwalk.cpp:225)
#5 0x680d3b wxStackWalker::InitFrames(this=0x7fffffffd3f0, arr=0x7fffffff6a60, n=42, addresses=0xa5c248 <wxStackWalker::ms_addresses+8>, syminfo=0xc8ac08) (../src/unix/stackwalk.cpp:320)
#6 0x68056e wxStackWalker::ProcessFrames(this=0x7fffffffd3f0, skip=1) (../src/unix/stackwalk.cpp:192)
#7 0x442a3e StackDump::ShowStackInDialog(this=0x7fffffffd3f0) (../src/gtk/utilsgtk.cpp:264)
#8 0x441a36 get_stackframe_callback(p=0x7fffffffd3f0) (../src/gtk/utilsgtk.cpp:323)
#9 0x515b50 gtk_assert_dialog_process_backtrace(dlg=0xc2c030) (../src/gtk/assertdlg_gtk.cpp:121)
#10 0x515bf2 gtk_assert_dialog_expander_callback(dlg=0xc2c030) (../src/gtk/assertdlg_gtk.cpp:146)
#11 0x7ffff5e6d3b8 g_closure_invoke() (/usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0:??)
#12 0x7ffff5e7ed3d ??() (/usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0:??)
#13 0x7ffff5e86a29 g_signal_emit_valist() (/usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0:??)
#14 0x7ffff5e86ce2 g_signal_emit() (/usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0:??)
#15 0x7ffff72964fc IA__gtk_widget_activate(widget=0xb78bb0) (/build/gtk+2.0-KsZSEA/gtk+2.0-2.24.23/gtk/gtkwidget.c:5041)
#16 ?? 0x00007ffff7126a6d in gtk_expander_button_release (widget=0xb78bb0, event=0xa58ca8 <wxDefaultAssertHandler(wxString const&, int, wxString const&, wxString const&, wxString const&)::s_bInAssert>) (/build/gtk+2.0-KsZSEA/gtk+2.0-2.24.23/gtk/gtkexpander.c:964)
#17 0x7ffff7187815 _gtk_marshal_BOOLEAN__BOXED(closure=0xae1150, return_value=0x7fffffffce70, n_param_values=<optimized out>, param_values=0x7fffffffcf20, invocation_hint=<optimized out>, marshal_data=0x7ffff7126a40 <gtk_expander_button_release>) (gtkmarshalers.c:86)
#18 0x7ffff5e6d3b8 g_closure_invoke() (/usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0:??)
#19 0x7ffff5e7eafb ??() (/usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0:??)
#20 0x7ffff5e866f9 g_signal_emit_valist() (/usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0:??)
#21 0x7ffff5e86ce2 g_signal_emit() (/usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0:??)
#22 0x7ffff7297684 gtk_widget_event_internal(widget=widget@entry=0xb78bb0, event=event@entry=0xc68df0) (/build/gtk+2.0-KsZSEA/gtk+2.0-2.24.23/gtk/gtkwidget.c:5010)
#23 0x7ffff7297959 IA__gtk_widget_event(widget=widget@entry=0xb78bb0, event=event@entry=0xc68df0) (/build/gtk+2.0-KsZSEA/gtk+2.0-2.24.23/gtk/gtkwidget.c:4807)
#24 0x7ffff7185fc4 IA__gtk_propagate_event(widget=0xb78bb0, event=0xc68df0) (/build/gtk+2.0-KsZSEA/gtk+2.0-2.24.23/gtk/gtkmain.c:2509)
#25 0x7ffff718637b IA__gtk_main_do_event(event=0xc68df0) (/build/gtk+2.0-KsZSEA/gtk+2.0-2.24.23/gtk/gtkmain.c:1699)
#26 0x7ffff6e003ec gdk_event_dispatch(source=<optimized out>, callback=<optimized out>, user_data=<optimized out>) (/build/gtk+2.0-KsZSEA/gtk+2.0-2.24.23/gdk/x11/gdkevents-x11.c:2425)
#27 0x7ffff5b9de04 g_main_context_dispatch() (/lib/x86_64-linux-gnu/libglib-2.0.so.0:??)
#28 0x7ffff5b9e048 ??() (/lib/x86_64-linux-gnu/libglib-2.0.so.0:??)
#29 0x7ffff5b9e30a g_main_loop_run() (/lib/x86_64-linux-gnu/libglib-2.0.so.0:??)
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: screenshot code problem.

Post by doublemax »

Sorry, like i said, i only work under Windows. I have no idea why it doesn't work under GTK.
Use the source, Luke!
Post Reply