Page 1 of 1

From wxMemoryDC to wxGraphicsContext with a small issue

Posted: Tue Apr 17, 2018 8:10 pm
by bertolino
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

Re: From wxMemoryDC to wxGraphicsContext with a small issue

Posted: Tue Apr 17, 2018 8:25 pm
by doublemax
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.

Re: From wxMemoryDC to wxGraphicsContext with a small issue

Posted: Tue Apr 17, 2018 8:43 pm
by bertolino
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

Re: From wxMemoryDC to wxGraphicsContext with a small issue

Posted: Tue Apr 17, 2018 9:00 pm
by doublemax
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

Re: From wxMemoryDC to wxGraphicsContext with a small issue

Posted: Tue Apr 17, 2018 9:02 pm
by bertolino
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

Re: From wxMemoryDC to wxGraphicsContext with a small issue

Posted: Tue Apr 17, 2018 9:04 pm
by bertolino
Our posts crossed.
Thanks Doublemax for your help.
Regards,

Pascal

Re: From wxMemoryDC to wxGraphicsContext with a small issue

Posted: Tue Apr 17, 2018 9:08 pm
by doublemax
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.

Re: From wxMemoryDC to wxGraphicsContext with a small issue

Posted: Tue Apr 17, 2018 9:17 pm
by bertolino
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

Re: From wxMemoryDC to wxGraphicsContext with a small issue

Posted: Wed Apr 18, 2018 7:03 am
by bertolino
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