From wxMemoryDC to wxGraphicsContext with a small issue

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.
Post Reply
bertolino
Knows some wx things
Knows some wx things
Posts: 48
Joined: Wed Aug 14, 2013 8:07 am
Location: France
Contact:

From wxMemoryDC to wxGraphicsContext with a small issue

Post by bertolino » Tue Apr 17, 2018 8:10 pm

Hello,

I recently changed my code that used wxMemoryDC to use wxGraphicsContext and get anti-aliasing support.
It works great and the change from wxMemoryDC to wxGraphicsContext is quite simple since the methods
are rather similar.
Everything was fine until I made the changes on text drawing:
My problem is that I don't find any SelectoObject method to use
the same approach than with wxMemoryDC.
Any idea?
Many thanks for your help.

Pascal

User avatar
doublemax
Moderator
Moderator
Posts: 14898
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: From wxMemoryDC to wxGraphicsContext with a small issue

Post by doublemax » Tue Apr 17, 2018 8:25 pm

I don't understand what you're looking for. wxGraphicsContext does have a DrawText method, but that's easy to find, so that's probably not what you mean.
Use the source, Luke!

bertolino
Knows some wx things
Knows some wx things
Posts: 48
Joined: Wed Aug 14, 2013 8:07 am
Location: France
Contact:

Re: From wxMemoryDC to wxGraphicsContext with a small issue

Post by bertolino » Tue Apr 17, 2018 8:43 pm

Sorry, I was not clear.
I want to draw some text over an image, so I use a bitmap for it.
With the wxMemoryDC dc, I used to attach the dc to the bitmap:

Code: Select all

wxBitmap myBitmap = wxBitmap(myWxImage, -1) ;
wxMemoryDC dc ;	
dc.SelectObject(myBitmap) ;
...
dc.DrawText(...) ;
...
myWxImage = myBitmap.ConvertToImage() ;
Everything seems the same with wxGraphicsContext but there is not SelectObjet method.
So I'm a bit lost. I guess it's not the same principle.
I hope I was more clear. Thank you Doublemax for your time.

Pascal

User avatar
doublemax
Moderator
Moderator
Posts: 14898
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: From wxMemoryDC to wxGraphicsContext with a small issue

Post by doublemax » Tue Apr 17, 2018 9:00 pm

There are several ways you can create a wxGraphicsContext.

In your case there are two versions you can use:

Create wxGraphicsContext to draw into a wxMemoryDC:
http://docs.wxwidgets.org/trunk/classwx ... 2dc0686e2e

Create wxGraphicsContext to draw into a wxImage:
http://docs.wxwidgets.org/trunk/classwx ... 2e8cda964c
Use the source, Luke!

bertolino
Knows some wx things
Knows some wx things
Posts: 48
Joined: Wed Aug 14, 2013 8:07 am
Location: France
Contact:

Re: From wxMemoryDC to wxGraphicsContext with a small issue

Post by bertolino » Tue Apr 17, 2018 9:02 pm

OOps, I have understood my error:
I must keep on using wxMemoryDC::SelectObject
and do all the rest with the wxGraphicsContext.
It works well like this.
Thanks,

Pascal

bertolino
Knows some wx things
Knows some wx things
Posts: 48
Joined: Wed Aug 14, 2013 8:07 am
Location: France
Contact:

Re: From wxMemoryDC to wxGraphicsContext with a small issue

Post by bertolino » Tue Apr 17, 2018 9:04 pm

Our posts crossed.
Thanks Doublemax for your help.
Regards,

Pascal

User avatar
doublemax
Moderator
Moderator
Posts: 14898
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: From wxMemoryDC to wxGraphicsContext with a small issue

Post by doublemax » Tue Apr 17, 2018 9:08 pm

Your code looks like you want to draw into a wxImage. In that case the second version might be better, because it saves you the conversion from wxImage to wxBitmap and back.
Use the source, Luke!

bertolino
Knows some wx things
Knows some wx things
Posts: 48
Joined: Wed Aug 14, 2013 8:07 am
Location: France
Contact:

Re: From wxMemoryDC to wxGraphicsContext with a small issue

Post by bertolino » Tue Apr 17, 2018 9:17 pm

You're right, it will be more simple and faster!
I'm going to try (tomorrow morning) and will let you know.
Many thanks for your advice and your very fast availability!

Pascal

bertolino
Knows some wx things
Knows some wx things
Posts: 48
Joined: Wed Aug 14, 2013 8:07 am
Location: France
Contact:

Re: From wxMemoryDC to wxGraphicsContext with a small issue

Post by bertolino » Wed Apr 18, 2018 7:03 am

Hi Doublemax,

So, as you proposed, I created my graphics context from the image and of course it works pretty well:

Code: Select all

wxGraphicsContext *gc = wxGraphicsContext::Create(myWxImage) ;
My comments about this version:
- The process is not faster (I'm on Windows)
- The code is more readable / lighter
- Don't forget to gc->Flush() when you have finished to use the gc in order to update the image.

Good day!
Pascal

Post Reply