Platforms to show: All Mac Windows Linux Cross-Platform

/MacCocoa/NSImage/Open Image using NSImage


Required plugins for this example: MBS MacBase Plugin, MBS MacCG Plugin, MBS MacCF Plugin, MBS Main Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCocoa/NSImage/Open Image using NSImage

This example is the version from Mon, 15th Sep 2019.

Project "Open Image using NSImage.xojo_binary_project"
MyFileTypes
Filetype text
Filetype all
End MyFileTypes
Class Window1 Inherits Window
EventHandler Sub DropObject(obj As DragItem, action As Integer) if obj.FolderItemAvailable then process obj.FolderItem end if End EventHandler
EventHandler Sub Open() me.AcceptFileDrop "all" End EventHandler
Protected Sub process(f as folderitem) dim p as Picture p=f.OpenAsNSImage if p<>Nil then Backdrop=p MaxWidth=p.Width MaxHeight=p.Height else MsgBox "Could not read picture from "+f.Name end if End Sub
End Class
MenuBar MenuBar1
MenuItem UntitledMenu1 = ""
MenuItem FileMenu = "&File"
MenuItem FileQuit = "Quit"
MenuItem UntitledMenu5 = ""
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu0 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "Clear"
MenuItem UntitledMenu4 = ""
MenuItem UntitledMenu3 = ""
MenuItem UntitledMenu2 = ""
End MenuBar
Class App Inherits Application
EventHandler Sub Open() if TargetMachO=false then MsgBox "This example needs a MachO target running on Mac OS X." quit end if End EventHandler
End Class
Module Module1
Function OpenAsNSimage(extends file as folderitem) As picture // this one is resolution indepenend! dim width as integer dim height as integer // load image from file dim c as new NSImageMBS(file) // is that image valid? if c.isValid then Width = c.MaximumPixelWidth height = c.MaximumPixelHeight // We want maximum size of pixels! c.ScalesWhenResized = True c.SetSize width,height // create a drawing buffer to draw inside dim pic as new Picture(width, height, 32) dim g as CGBitmapContextMBS = CGBitmapContextMBS.CreateWithPicture(pic) if g.Handle<>0 then // valid? // now draw the image inside. // you could scale or even apply transparency... if c.DrawIntoCGContextAtRect(g.Handle, 0, 0, c.width, c.height, 0,0,c.width,c.height,2,1.0) then // make a RB Picture from it Return pic end if end if end if End Function
End Module
End Project

The items on this page are in the following plugins: MBS MacCocoa Plugin.


The biggest plugin in space...