Create NSImageRep from CGImage

Daniel Peñalba 1 Reputation point
2022-03-08T21:27:03.177+00:00

hi there! I’m trying to pinvoke the AppKit from C#. Any help would be appreciated.

I’m trying to create a new NSBitmapImageRep from a CGImage using the following constructor:
https://developer.apple.com/documentation/appkit/nsbitmapimagerep/1395423-init

I’m trying the following C# code with no luck:

var nsBitmapImageRep = objc_getClass("NSBitmapImageRep");

 var nsBitmapImage = objc_msgSend_retIntPtr_IntPtr(
     nsBitmapImageRep,
     GetSelector("initWithCGImage:"),
     cgImage);

I’m not sure if this is the correct way to construct a new object …

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,301 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 27,106 Reputation points Microsoft Vendor
    2022-03-09T08:12:15.637+00:00

    Hello,

    If you are using AppKit and you want to create a new NSBitmapImageRep from a CGImage using the constructor in your Xamarin.Mac project, you could refer to the following code:

    var image = new NSImage("setting.png");// I create a NSImage to get the CGImage  
    var nsBitmapImageRep = new NSBitmapImageRep(image.CGImage);// this parameter is your CGImage   
    

    You also could check the API at https://learn.microsoft.com/en-us/dotnet/api/appkit.nsbitmapimagerep?view=xamarin-mac-sdk-14

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments