Picture

From Xojo Documentation
Jump to: navigation, search

Class (inherits from Object)

A Picture object contains a picture.

Properties
DebuggerMask HorizontalResolution RGBSurface ReadOnlyProperty.png
Depth ReadOnlyProperty.png ImageCount Transparent
Graphics ReadOnlyProperty.png IndexedImage Type ReadOnlyProperty.png
HasAlphaChannel ReadOnlyProperty.png Mask VerticalResolution
Height ReadOnlyProperty.png Objects Width ReadOnlyProperty.png


Methods
ApplyMask CopyMask Save
BestRepresentation CopyOSHandle
CopyColorChannels GetData


Shared Methods
FromData IsImportFormatSupported
IsExportFormatSupported Open


Constructors

Picture.Constructor(width as Integer, height as Integer, bitmaps() As Picture)


Picture.Constructor(width as Integer, height as Integer)


Picture.Constructor(width as Integer, height as Integer, Depth as Integer)


Notes

NOTE: Console applications only support JPEG and PNG image types.

Image Sets and HiDPI

When adding Pictures to your projects, you create Image Sets which can contain the picture at various sizes for use with HiDPI displays.

Also refer to HiDPI Support for additional information.

Support for Transparency

Beginning with version 2011r4, Picture objects support alpha channels (transparency) directly, without the need for a mask.

Pictures with alpha channels are supported on macOS, Windows , Linux, console applications, and Web. On platforms without support for alpha channels, a PlatformNotSupported exception is raised when trying to create the Picture.

Support for the alpha channel is now built into the color functions: RGB, HSV, CMY, and &c. These functions have an optional fourth parameter that specifies the degree of transparency of the color. Here are the modified functions:

Function Parameters
RGB red as Integer
green as Integer
blue as Integer
alpha as Integer = 0
HSV hue as Double
saturation as Double
value as Double
alpha as Integer = 0
CMY cyan as Double
magenta as Double
yellow as Double
alpha as Integer = 0
&c RRGGBBAA, where the hex digits specify the amounts of red, green, blue, and transparency in the color. For the transparency parameter, 00 is opaque and FF is fully transparent.

In the Picture class, support for alpha channels means that the use of masks is no longer required. This streamlines the process and reduces memory support. To take advantage of this feature, you can create new Picture objects with alpha channel support and convert “old” picture objects to those with alpha channels.

Constructor

To create a Picture with an alpha channel, use the new constructor that omits the depth parameter:

Constructor(width As Integer, height As Integer)

When this constructor is used, the Picture instance defaults to transparent and can be returned to that state using Graphics.ClearRect().

A picture created without an alpha channel supplies the depth parameter:

Constructor(width As Integer, height As Integer, depth As Integer)

Updating "old" Pictures

Pictures loaded from disk, databases, project files, or Picture.FromData continue to return pictures with masks. This is required for legacy compatibility, but masked Pictures can be converted to a Picture with an alpha channel with the following code:

Function ConvertToAlphaPicture(input As Picture) As Picture
If input.hasAlphaChannel Then Return input

Dim result As New Picture(input.Width, input.Height)
result.HorizontalResolution = input.HorizontalResolution
result.VerticalResolution = input.VerticalResolution
result.Graphics.DrawPicture(input, 0, 0)
Return result
End Function

New Properties and Methods

HasAlphaChannel Property

HasAlphaChannel As Boolean

This property indicates whether or not the Picture has an alpha channel. For Pictures using masks, this returns False.

CopyMask Method

CopyMask As Picture

When used on a picture with an alpha channel, this takes the image's alpha channel and creates the equivalent mask. If used on a picture with a mask, it simply returns a copy of the current mask. In neither case does it alter the original image and the copied mask is not updated when any drawing happens on the original Picture.

ApplyMask Method

ApplyMask(theMask As Picture)

When used on a Picture with an alpha channel, this overwrites the Picture's alpha channel with the mask data. When used on a picture with a mask, it overwrites the current mask with a copy of the mask passed in.

Other Alpha Channel Notes

Pixel and RGBSurface have been updated to properly support alpha channels by taking them into account when rendering them. Additionally, Pixel properly reports back alpha channel information, if available.

Pictures with alpha channels cannot also use masks. Trying to do so will raise an UnsupportedOperationException at runtime. Reading this value always returns Nil for pictures with alpha channels. For pictures without alpha channels, the mask functionality continues to work normally.

Pictures with alpha channels cannot use the "Transparent" property and attempting to assign anything other than zero to it will raise an UnsupportedOperationException at runtime. For pictures without alpha channels, this property will continue to work.

On Mac and Linux, Pictures are stored internally with their alpha channels premultiplied into the color channels. Since premultiplication is a lossy operation, the color you draw may not be exactly what you get out when inspecting it with an RGBSurface. For example, drawing a &cFF00EECC rectangle will result in a different color if read from that pixel using RGBSurface.

Plugin Support

The color type has changed from being XRGB to ARGB. If you previously put garbage into the first byte, this is going to cause problems now that the framework uses this to store the alpha component.

On macOS, a Picture with an alpha channel is represented internally as a premultiplied ARGB buffer wrapped by a CGBitmapContext. This buffer can be accessed the same way as for masked images: REALLockPictureDescription and then CGBitmapContextGetData.


On Windows, a Picture with an alpha channel is represented internally as a premultiplied 32-bit BGRA buffer. This buffer can be accessed the same way as for masked images: REALLockPictureDescription and the raw bytes are returned in the data field.

On Linux, a Picture with an alpha channel is represented internally as a 32-bit premultiplied ARGB buffer wrapped by a cairo_t. This buffer can be accessed the same way as for masked images: REALLockPictureDescription and then cairo_image_surface_get_data( cairo_get_target( cairo_t * ) ).

On console and web apps, a Picture with an alpha channel is represented internally as a 32-bit ARGB buffer wrapped by a gdImagePtr. Note: the ARGB buffer is NOT premultiplied, and the maximum alpha value is 127 instead of 255. This buffer can be accessed the same way as for masked images: REALLockPictureDescription with pictureGDPtr (this will give you the raw buffer with an 11 byte header).

Creating a Picture

A Picture object is created by adding a picture to the project, by calling the Picture constructor or the OpenAsVectorPicture method of a FolderItem object. When you use the New operator, you must use the constructor. If you use the New operator but run out of memory, an OutOfMemoryException error will be raised. If you specify a depth of zero, a picture with no pixel map at all is created, but with a preinitialized Objects property. Use this option for creating vector graphics pictures via the set of Object2D subclasses.

Here is an example on how to create a picture and handle the OutOfMemoryException exception:

// 2000 x 2000 Pixel works, but 20000 x 20000 Pixel will raise exception
Dim width As integer = 2000
Dim height As integer = 2000

// creates new picture
Dim pic As New Picture(width, height, 32)

// fill with red
pic.Graphics.ForeColor = &cFF0000
pic.Graphics.FillRect(0, 0, 100, 100)

// and show picture in a window/canvas backdrop
Self.Backdrop = pic

Exception o As OutOfMemoryException
MsgBox("Picture dimensions too big." + EndOfLine + EndOfLine + Str(width * height * 4 / 1024 / 1024) + " MB")

Picture objects have no events.

Using the Transparent property with 'old' Pictures

The Transparent property works for pictures that lack an alpha channel only if making the color white transparent produces the desired effect. In the example shown below, the picture's interior is white but its border is black. Making the white "layer" transparent makes the white area transparent. Notice that only the interior is white, but the border is not.

No Transparency: Transparent=0.
Transparency: Transparent=1.

In many cases, you need to work with the Mask property. For example, if you have a picture, MyPicture, and a custom mask, MyMask, add to your project, the code:

MyPicture.Mask.Graphics.DrawPicture(MyMask, 0, 0)

will assign the custom mask to the Mask property of MyPicture.

Obviously, you should upgrade these pictures so that they support alpha channel information. Use the ConvertToAlphaPicture function from above to do this.

Class Constants

The Save method can save pictures in a variety of formats. You can use the BMP, JPEG and PNG formats on all platforms (but only JPEG and PNG in console apps).

You can also save in other formats as described below.

Meta-Formats

Meta-formats map to a various concrete formats based on the target, the data being saved, or other criteria.

Value Class Constant Compatibility Description
0 SaveAsMostCompatible MostCompatible Most widely-used format for the platform.

macOS = PICT (deprecated)
Win32 = BMP)

1 SaveAsMostComplete MostComplete Format most likely to retain all vector info.

macOS = PICT (deprecated)
Win32 = EMF)

2 SaveAsDefault Default DefaultVector or DefaultRaster, depending on picture data.

macOS = PICT (deprecated)
Win32 vector=EMF
Win32 raster= BMP

3 SaveAsDefaultVector DefaultVector Platform's standard vector format.

macOS = PICT (deprecated)
Win32 = EMF)

4 SaveAsDefaultRaster DefaultRaster Platform's standard raster format.

macOS = Raster PICT (deprecated)
Win32 = BMP)

macOS only Formats (valid through OS X 10.8.x)

Value Class Constant Format Description
100 SaveAsMacintoshPICT
Introduced 2008r4
PICT Macintosh PICTs are "type 2" PICTs that are saved with the full resolution of the image. Includes simple vector data.
NOTE: The PICT format has been deprecated by Apple; use JPEG or PNG on macOS instead.

Note that this value of 100 generates an UnsupportedFormatException. Use value 200 instead (there is no constant for this) if you must save as PICT format on macOS.

250 SaveAsMacintoshRasterPICT
Introduced 2008r4
RasterPICT Flattens all vector data to pixels.

Windows and Cross-Platform Formats

Xojo 2016r3 and earlier require GDI+ to be enabled in the project for Windows to use file formats other than BMP.

Value Class Constant Format Description
150 SaveAsPNG PNG Portable Network Graphics.
151 SaveAsJPEG JPEG Joint Photographics Expert Group.
301 SaveAsWindowsEMF EMF Extended Metafile format (newer vector format).
350 SaveAsWindowsBMP BMP Windows bitmap format.

Platform-Specific Formats

Be sure to check for UnsupportedFormatException on all platforms where these are used.

Xojo 2016r3 and earlier require GDI+ to be enabled in the project for Windows to use file formats other than BMP.

Value Class Constant Format Description
402 SaveAsGIF GIF Graphics Interchange Format.
403 SaveAsTIFF TIFF Tag Image File Format.

Cross-Platform Formats

These formats are cross-platform: SaveAsPNG, SaveAsJPEG and SaveAsWindowsBMP.

Unrecognized formats or formats not supported for the built target will result in an UnsupportedFormatException. The Message property of the exception will contain additional information about what went wrong.

JPEG Quality Constants

If you are using the JPEG format, then there is an optional parameter for the quality of the JPEG. You specify the quality using the following class constants.

Constant Value
QualityDefault 1
QualityHigh 80
QualityLow 25
QualityMax 100
QualityMedium 50
QualityMin 0

See Also

RGBSurface object; Graphics class.

Personal tools
Namespaces

Variants
Actions
Main
Content
Starting Out
Dig Deeper
More Help
Toolbox
About