# Graphics Handle always null

**URL:** <https://forum.xojo.com/t/graphics-handle-always-null/28889>\
**Category:** macOS\
**Created:** [December 28, 2015, 4:35pm UTC](https://forum.xojo.com/t/graphics-handle-always-null/28889 "2015-12-28T16:35:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrick\_Morel](https://forum.xojo.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@Patrick\_Morel](https://forum.xojo.com/u/Patrick_Morel)\
**Post date:** [December 28, 2015, 4:35pm UTC](https://forum.xojo.com/t/graphics-handle-always-null/28889/1 "2015-12-28T16:35:22Z")

</div>

Hello,  
I need to get a Graphics Handle of either HandleTypeCGrafPtr or HandleTypeCGContextRef Type.  
In both cases, I get a null Handle  
I need it, for I want to draw a part of a NSTextView into a Graphics (Canvas or a Graphics to be printed)

`Dim Pic as New Picture (300, 300, 24)
Dim g as Graphics = Pic.Graphics
Dim iHandle as integer = g.Handle (Graphics.HandleTypeCGrafPtr)`

Same for

```auto
Dim iHandle as integer = g.Handle (Graphics.HandleTypeCGContextRef)
```

Is it a bug of Xojo ?  
Is there another way to get a Mac Graphics Handle ?  
TIA

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [December 28, 2015, 5:47pm UTC](https://forum.xojo.com/t/graphics-handle-always-null/28889/2 "2015-12-28T17:47:27Z")

</div>

AFAIK there is no Graphics.Handle for a picture. Graphis.Handle is only non-zero within the Paint event of controls and windows, most notably of course is the Canvas’s Paint event:

`Sub Paint(g As Graphics, areas() As REALbasic.Rect)
   Dim handle As Integer = g.Handle(Graphics.HandleTypeCGContextRef)
   ...
End Sub`

---

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [December 28, 2015, 6:00pm UTC](https://forum.xojo.com/t/graphics-handle-always-null/28889/3 "2015-12-28T18:00:22Z")

</div>

You can always create a fresh image context in the size you need, draw the entire view hierarchy (or a rectangle of it) of your control into it and then draw the context into an image, which in turn you can convert into a Xojo picture.

Creating the context and drawing the view hierachy takes some time; I would recommend to do this conversion outside your canvas draw event and rather attach the result as an image as described to avoid flicker. But maybe drawing the controls view hierarchy into the canvas paint graphics object is fast enough though as the event provides you with the current context already.

Do you know which Core Graphics methods to call?

EDIT: To be a bit more verbose: No, it’s no Xojo error. A graphics context doesn’t exist outside of a paint event, unless you create one. In case you can read German, I have a short description on my blog: [http://xojoblog.me/2015/07/10/ios-grafiken-fuer-fortgeschrittene-teil-i-iosgraphics-aka-cgcontext/](http://xojoblog.me/2015/07/10/ios-grafiken-fuer-fortgeschrittene-teil-i-iosgraphics-aka-cgcontext/)

---

<div class="post-metadata">

**Author:** ![Will\_Shank](https://forum.xojo.com/letter_avatar_proxy/v4/letter/w/f475e1/32.png) [@Will\_Shank](https://forum.xojo.com/u/Will_Shank)\
**Post date:** [December 28, 2015, 7:08pm UTC](https://forum.xojo.com/t/graphics-handle-always-null/28889/4 "2015-12-28T19:08:22Z")

</div>

Works for me in an older Xojo on 10.10

HandleTypeCGContextRef works for Cocoa  
HandleTypeCGrafPtr works for Carbon

---

<div class="post-metadata">

**Author:** ![Joe\_Ranieri](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@Joe\_Ranieri](https://forum.xojo.com/u/Joe_Ranieri)\
**Post date:** [December 29, 2015, 8:06am UTC](https://forum.xojo.com/t/graphics-handle-always-null/28889/5 "2015-12-29T08:06:10Z")

</div>

[quote=238143:@Eli Ott]AFAIK there is no Graphics.Handle for a picture. Graphis.Handle is only non-zero within the Paint event of controls and windows, most notably of course is the Canvas’s Paint event:

`Sub Paint(g As Graphics, areas() As REALbasic.Rect)
   Dim handle As Integer = g.Handle(Graphics.HandleTypeCGContextRef)
   ...
End Sub`[/quote]

A Picture that has a non-Nil Graphics object should have a valid CGContext handle. The only place you definitely won’t get a handle back is using a control’s Graphics object outside of the Paint event (which is deprecated anyways).

---

<div class="post-metadata">

**Author:** ![Patrick\_Morel](https://forum.xojo.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@Patrick\_Morel](https://forum.xojo.com/u/Patrick_Morel)\
**Post date:** [December 31, 2015, 2:43am UTC](https://forum.xojo.com/t/graphics-handle-always-null/28889/6 "2015-12-31T02:43:40Z")

</div>

Well, it seems that it is possible, the following code works :

```
[code]Dim Pic as New Picture (300, 300)

```

Dim iHandle as integer = Pic.Graphics.Handle(Pic.Graphics.HandleTypeCGContextRef)  
Dim gPtr as Ptr = Ptr (iHandle)[/code]

I can’t use a paint event here because my NStextView instance is not intended to be displayed :

`' Instance a new NSTextView, given a NSRect : aRect
dim pNewTextView as Ptr = initWithFrame (allocFromString ("NSTextView"), aRect)
' Load RTF in it
Dim data as Ptr = dataWithBytes (ClassFromString("NSData"), RtfString, LenB(RtfString))
dim range as NSRange
replaceCharactersInRange (pNSTextView, range, data)
' Get a bitmap and draw in it
dim pNSBitmapImageRep as Ptr = bitmapImageRepForCachingDisplayInRect (pNewTextView, aRect)
CacheDisplayInRectToBitmapImageRep (pNewTextView, aRect, pNSBitmapImageRep)
' Conversion into a CGImage
dim pCGImage As Ptr = CGImage (pNSBitmapImageRep)
' We draw the CGImage into Pic
Dim Pic as New Picture (aRect.Width, aRect.Height )
Dim iHandle as integer = Pic.Graphics.Handle(Pic.Graphics.HandleTypeCGContextRef)
Dim gPtr as Ptr = Ptr (iHandle)
Dim cgr as CGRect ' ==> Convert here aRect (= NSRect) to cgr (= CGRect)
CGContextDrawImage (gPtr, cgr, pCGImage)
ReleasePtr (pNewTextView)`

I can then scale and draw Pic anywhere, in a Canvas or in some clipped region on a printer graphics …

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 6:07pm UTC](https://forum.xojo.com/t/graphics-handle-always-null/28889/7 "2020-10-29T18:07:19Z")

</div>


