Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS Images Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Picture/Picture To String
This example is the version from Sun, 17th Mar 2012.
Project "Picture To String.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action()
out.Backdrop=nil
// some picture
dim p as Picture = LogoMBS(500)
// compress
dim s as string = PictureToJPEGStringMBS(p, 75)
JPEGSize.text=Str(lenb(s))
// decompress
dim q as Picture = JPEGStringToPictureMBS(s)
out.Backdrop=q
if q<>Nil then
me.Caption="Works"
end if
End EventHandler
End Control
Control PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action()
out.Backdrop=nil
// some picture
dim p as Picture = LogoMBS(500)
// compress
dim s as string = PictureToJPEGString(p, 75)
JPEGSize.text=Str(lenb(s))
// decompress
dim q as Picture = StringToPicture(s)
out.Backdrop=q
if q<>Nil then
me.Caption="Works"
end if
End EventHandler
End Control
Control out Inherits Canvas
ControlInstance out Inherits Canvas
End Control
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control PushButton3 Inherits PushButton
ControlInstance PushButton3 Inherits PushButton
EventHandler Sub Action()
out.Backdrop=nil
// some picture
dim p as Picture = LogoMBS(500)
// compress
dim s as string = PictureToTIFFStringMBS(p)
tiffsize.text=Str(lenb(s))
// decompress
dim q as Picture = TIFFStringToPictureMBS(s)
out.Backdrop=q
if q<>Nil then
me.Caption="Works"
end if
End EventHandler
End Control
Control PushButton4 Inherits PushButton
ControlInstance PushButton4 Inherits PushButton
EventHandler Sub Action()
out.Backdrop=nil
// some picture
dim p as Picture = LogoMBS(500)
// compress
dim s as string = PictureToTiffString(p)
tiffsize.text=Str(lenb(s))
// decompress
dim q as Picture = StringToPicture(s)
out.Backdrop=q
if q<>Nil then
me.Caption="Works"
end if
End EventHandler
End Control
Control StaticText2 Inherits Label
ControlInstance StaticText2 Inherits Label
End Control
Control StaticText3 Inherits Label
ControlInstance StaticText3 Inherits Label
End Control
Control PushButton5 Inherits PushButton
ControlInstance PushButton5 Inherits PushButton
EventHandler Sub Action()
out.Backdrop=nil
// some picture
dim p as Picture = LogoMBS(500)
// compress
dim s as string = PictureToPNGString(p)
pngsize.text=Str(lenb(s))
// decompress
dim q as Picture = StringToPicture(s)
out.Backdrop=q
if q<>Nil then
me.Caption="Works"
end if
End EventHandler
End Control
Control PushButton6 Inherits PushButton
ControlInstance PushButton6 Inherits PushButton
EventHandler Sub Action()
const gamma = 0.0 // default gamma
out.Backdrop=nil
// some picture
dim p as Picture = LogoMBS(500)
// compress
dim s as string = PictureToPNGStringMBS(p, gamma)
pngsize.text=Str(lenb(s))
// decompress
dim q as Picture = PNGStringToPictureMBS(s, gamma)
out.Backdrop=q
if q<>Nil then
me.Caption="Works"
end if
End EventHandler
End Control
Control JPEGSize Inherits Label
ControlInstance JPEGSize Inherits Label
End Control
Control TiffSize Inherits Label
ControlInstance TiffSize Inherits Label
End Control
Control PNGSize Inherits Label
ControlInstance PNGSize Inherits Label
End Control
Control StaticText4 Inherits Label
ControlInstance StaticText4 Inherits Label
End Control
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
Module Util
Function PictureToJPEGString(p as Picture, q as integer) As string
dim t as FolderItem = GetTemporaryFolderItem
t.SaveAsJPEG p
dim b as BinaryStream = t.OpenAsBinaryFile(False)
if b<>Nil then
Return b.Read(B.Length)
end if
Exception r as runtimeexception
if DebugBuild then
MsgBox r.message
end if
End Function
Function PictureToPNGString(p as Picture) As string
dim t as FolderItem = GetTemporaryFolderItem
t.SaveAsPicture p, FolderItem.SaveAsPNG
dim b as BinaryStream = t.OpenAsBinaryFile(False)
if b<>Nil then
Return b.Read(B.Length)
end if
Exception r as runtimeexception
if DebugBuild then
MsgBox r.message
end if
End Function
Function PictureToTIFFStringMBS(p as Picture) As string
// very easy function
// does not set compression, icc profile, mask, etc.
dim t as new TiffPictureMBS
if t.CreateString(1000000) then
t.Pict=p
if t.WriteRGB then
t.Close
Return t.OutputBuffer
end if
end if
Exception r as runtimeexception
if DebugBuild then
MsgBox r.message
end if
End Function
Function PictureToTiffString(p as Picture) As string
dim t as FolderItem = GetTemporaryFolderItem
t.SaveAsPicture p, FolderItem.SaveAsTIFF
dim b as BinaryStream = t.OpenAsBinaryFile(False)
if b<>Nil then
Return b.Read(B.Length)
end if
Exception r as runtimeexception
if DebugBuild then
MsgBox r.message
end if
End Function
Function StringToPicture(s as string) As Picture
if lenb(s)=0 then Return nil
dim t as FolderItem = GetTemporaryFolderItem
dim b as BinaryStream = t.CreateBinaryFile("")
if b<>Nil then
b.Write s
b.Close
Return t.OpenAsPicture
end if
Exception r as runtimeexception
if DebugBuild then
MsgBox r.message
end if
End Function
End Module
End Project
See also:
- /Picture/Picture Blending test
- /Picture/Picture blending/blend with mask
- /Picture/Picture Combine
- /Picture/Picture Combine Test
- /Picture/Picture Copy Fast Test
- /Picture/Picture Difference
- /Picture/Picture Matrix/matrix with plugin
- /Picture/Picture MinMax/mmx_demo
- /Picture/Picture Scale/Picture.ScaleMBS
- /Picture/Picture to Binary Data Test
The items on this page are in the following plugins: MBS Picture Plugin.