Var imageData As String
Var bs As BinaryStream
Var f As FolderItem
If ImageWell1.Image <> Nil Then
' Get a temporary file to save the image to
If Picture.IsExportFormatSupported(Picture.Formats.JPEG) Then
f = SpecialFolder.Temporary.Child("TempImage.jpg")
' Save the image out to the file
ImageWell1.Image.Save(f, Picture.Formats.JPEG)
End If
' Open the file as a BinaryStream and read the data in
bs = BinaryStream.Open(f, False)
If bs <> Nil Then
imageData = bs.Read(bs.Length)
bs.Close
End If
' delete the temporary file if it exists
If f.Exists Then
f.Delete
End If
End If
I need to adapt this code, my image comes from a pdf file. When I execute this code I have an exception on the save line:
UnsupportedOperationException Error: Saving a multi-representation Picture is not supported
Var p As Picture
Var tmp As FolderItem
p = Picture.Open(file)
ImageViewer1.Image = p
If Picture.IsExportFormatSupported(Picture.Formats.JPEG) Then
tmp = SpecialFolder.Temporary.Child("TempImage.jpg")
'tmp = SpecialFolder.Desktop.Child("TEST.jpg")
' Save the image out to the file
Try
p.Save(tmp, Picture.Formats.JPEG)
Catch error As UnsupportedOperationException
MessageBox("UnsupportedOperationException Error: " + error.Message)
End Try
End If
Var pic As Picture
'pic = p.BestRepresentation(100, 100, 32.0)
pic = p.ImageAt(0)
If Picture.IsExportFormatSupported(Picture.Formats.JPEG) Then
Var tmp As FolderItem = SpecialFolder.Temporary.Child("FirstPage.jpg")
Try
pic.Save(tmp, Picture.Formats.JPEG)
MessageBox("PDF first page saved as JPEG in: " + tmp.NativePath)
Catch error As UnsupportedOperationException
MessageBox("Save failed: " + error.Message)
End Try
Else
MessageBox("JPEG export is not supported on this platform.")
End If
I made this test, and imageViewer show a valid image as expected.
Var p As Picture
p = Picture.Open(file)
ImageViewer1.Image = p.BestRepresentation(100, 100, 32.0)
I also made this test, and got ant OutOfBoundsException p.image.Count return 0 in the console…
This isn’t quite true. It’s more accurate to say “Xojo’s image support mirrors that of the underlying operating system,” because macOS actually does support directly loading a PDF. The following code, run on macOS, loads a PDF into a Picture object and images it onto another Picture object:
dim p as picture
dim f as FolderItem
f=FolderItem.ShowOpenFileDialog("")
p=picture.Open(f)
dim o as picture
o=new picture(p.Width, p.Height)
o.Graphics.DrawPicture p, 0, 0
break
Var origin As Picture
origin = Picture.Open(file)
If origin= Nil Then
MessageBox("Error: Failed to open PDF. PDF to Picture is likely unsupported on this platform.")
Return
End If
Var destination as Picture
destination = New Picture(origin.Width, origin.Height)
destination.Graphics.DrawPicture (origin, 0, 0)
ImageViewer1.Image = destination
If Picture.IsExportFormatSupported(Picture.Formats.JPEG) Then
'Var tmp As FolderItem = SpecialFolder.Temporary.Child("tmp_img.jpg")
Var tmp As FolderItem = SpecialFolder.Desktop.Child("tmp_img.jpg")
Try
destination.Save(tmp, Picture.Formats.JPEG)
MessageBox("PDF first page saved as JPEG in: " + tmp.NativePath)
Catch error As UnsupportedOperationException
MessageBox("Save failed: " + error.Message)
End Try
Else
MessageBox("JPEG export is not supported on this platform.")
End If