Opened 2 years ago

Closed 17 months ago

#18627 closed defect (fixed)

Cannot create bitmaps of views on OSX/Catalina

Reported by: dkulp Owned by:
Priority: normal Milestone: 3.1.4
Component: wxOSX Version: 3.1.3
Keywords: Cc:
Blocked By: Blocking:
Patch: yes


While trying to track down an issue for why wxSmith doesn't work in CodeBlocks on OSX, I've managed to track down the first issue to wxWindowDCImpl::DoGetAsBitmap no longer works on OSX. The problem seems to be that the line:

if ( [rep respondsToSelector:@selector(CGImage)] )

returns false and it then goes into the block below it which has a "TODO" in it. It returns the default (all black) wxBitmap. Thus, it does not look we can grab the window contents on OSX anymore.

Change History (9)

comment:1 Changed 2 years ago by vadz

I'm not 100% sure but I think Stefan said in the past that this just wasn't supported any more. If you, or anybody else, knows of any way to work around this, please let us know.

If not, perhaps we could at least implement some API allowing to get a bitmap of a native control? E.g. in wxMSW we could use WM_PRINT for this and maybe Cocoa views can be rendered onto memory bitmaps too somehow?

comment:2 Changed 2 years ago by vadz

Hmm, looking at the comment in the code, it seems that CGWindowListCreateImage() could be used to implement this. Could anyone try making this work?

comment:3 Changed 20 months ago by dkulp

  • Cc dan@… added

This seems to work to get the image:

-            // TODO for 10.4 in case we can support this for osx_cocoa
+            NSBitmapImageRep *nsbitmap = [view bitmapImageRepForCachingDisplayInRect:[view bounds]];
+            [view cacheDisplayInRect:[view bounds] toBitmapImageRep:nsbitmap];
+            NSImage *result = [[NSImage alloc] initWithSize:[view bounds].size];
+            [result addRepresentation:nsbitmap];
+            bitmap.Create(result);

Running into different issues in CodeBlocks now, but at least the window images appear correctly.

comment:4 Changed 20 months ago by dkulp

  • Cc dan@… removed

comment:5 Changed 20 months ago by vadz

  • Milestone set to 3.1.4
  • Patch set
  • Status changed from new to confirmed

Thanks for this! I'm ready to commit it (although I wonder if result shouldn't be released here?), but would really appreciate a confirmation from Stefan that this doesn't suffer from some fatal flaw that I have no idea about.

comment:6 Changed 20 months ago by dkulp

  • Cc dan@… added

Adding the release for the result still allows it to work so you are likely correct.

comment:7 Changed 20 months ago by dkulp

  • Cc dan@… removed

Any chance of getting this committed so the CodeBlocks folks can possibly pick it up?

comment:8 Changed 20 months ago by vadz

I'd really like to add a unit test for this to make sure this works as expected. And this will have to wait until my next session under macOS, which is still some time off as I have a few more things to finish first. If anybody could add such test and make a PR, so that it's checked in the CI builds, it would, of course, help a lot.

comment:9 Changed 17 months ago by vadz

  • Resolution set to fixed
  • Status changed from confirmed to closed

It looks like this has been implemented (differently) by Stefan in ced68e3efaeb0a70e39e3e7b5c2b8ed5f382919d and so hopefully already works now. Please reopen if it still doesn't, TIA!

Note: See TracTickets for help on using tickets.