Simple Pictures Example

Simple Pictures Example
  1import win32com.client
  2
  3picture_file_to_load = r"d:\picture1.jpg"
  4picture_file_to_save = r"d:\picture1saved.jpg"
  5
  6ad_type_binary = 1
  7ad_save_create_over_write = 2
  8
  9the_dialog = impact.gui.make_dialog("Pictures")
 10
 11opts1 = impact.gui.create_dialog_picture_options()
 12
 13# the width and height of the image within the picture control
 14opts1.width = 128
 15opts1.height = 128
 16
 17opts1.command_panel_position = ipDialogPictureCommandsPosition.dpcpRightTop
 18
 19# ensure that any image selected is automatically resized to a specific width and height
 20opts1.auto_resize = ipDialogPictureAutoResize.dparScaleToFixed
 21opts1.resize_width = 128
 22opts1.resize_height = 128
 23
 24# add a picture field with a label
 25pic1 = the_dialog.fields.add_picture(picture_file_to_load, "Picture 1", opts1)
 26
 27opts2 = impact.gui.create_dialog_picture_options()
 28
 29# a larger picture control
 30opts2.width = 480
 31opts2.height = 300
 32
 33# command panel options
 34opts2.command_panel_always_visible = True
 35opts2.command_panel_position = ipDialogPictureCommandsPosition.dpcpTop
 36
 37# hide individual commands
 38opts2.hide_command(ipDialogPictureCommand.dpcCopy)
 39opts2.hide_command(ipDialogPictureCommand.dpcPaste)
 40
 41# if no file formats are specified then all raster image formats are allowed
 42# otherwise you can specify the individual image formats to allow
 43opts2.allow_file_format(ipFileFormat.ffBitmap)
 44opts2.allow_file_format(ipFileFormat.ffPNG)
 45opts2.allow_file_format(ipFileFormat.ffJPG)
 46
 47# ensure that any image never exceeds a specific width and height - but any image
 48# smaller than this will be left at its original size
 49opts2.auto_resize = ipDialogPictureAutoResize.dparShrinkToMaximum
 50opts2.resize_width = 1024
 51opts2.resize_height = 1024
 52
 53# load a file into an array (but could also be a db value or image stored somewhere else)
 54stream = win32com.client.Dispatch("ADODB.Stream")
 55
 56stream.open()
 57stream.type = ad_type_binary
 58stream.load_from_file(picture_file_to_load)
 59
 60bytes = stream.read
 61
 62# add a picture field without a label means the image will be left justified in the dialog
 63pic2 = the_dialog.fields.add_picture(bytes, "", opts2)
 64
 65pic2.hint = "Select a nice picture to show here"
 66
 67opts3 = impact.gui.create_dialog_picture_options()
 68
 69opts3.width = 16
 70opts3.height = 16
 71
 72# hide commands panel
 73opts3.command_panel_position = ipDialogPictureCommandsPosition.dpcpNone
 74
 75pic3 = the_dialog.fields.add_picture(picture_file_to_load, "Picture 3", opts3)
 76
 77# to make a picture control read only set enabled to False
 78pic3.enabled = False
 79
 80# the buttons
 81the_dialog.add_button(ipDialogButtonType.dbtClose)
 82
 83# show the dialog
 84the_dialog.show_modal(None)
 85
 86# retrieve an image and save it to a local file
 87# the pic1 options specified that all images would be resized to 128x128
 88
 89if pic1.picture_has_image:
 90
 91    # we need the picture BLOB value as a VARIANT array of bytes
 92    the_dialog.fields.bytes_as_variant = False
 93
 94    bytes = pic1.value
 95
 96    stream = win32com.client.Dispatch("ADODB.Stream")
 97
 98    stream.open()
 99    stream.type = ad_type_binary
100
101    stream.write(bytes)
102    stream.save_to_file(picture_file_to_save, ad_save_create_over_write)
103
104else:
105    impact.gui.show_message(ipShowMessageType.smtError, "Picture1 has no image assigned", None)