!============================================================
! Копирование фото из clipboard в файл
!============================================================
  Program
  Include('FreeImCl.inc'),Once
  Include('SaClipCl.inc'),Once

  Map
    INCLUDE('SaWApi.inc','Prototypes'),ONCE
  End

JpegQuality Long(75)
FileName    string(128)
TovarName   string(128)

W    WINDOW('Сохранение фото из буфера обмена в файл JPEG'),AT(,,291,100),FONT('MS Sans Serif',8,,FONT:regular), |
         CENTER,ICON('Copy.ico'),SYSTEM,GRAY
       STRING(@s128),AT(17,10,261,10),USE(TovarName)
       STRING(@s128),AT(17,22,261,10),USE(FileName)
       PROMPT('&Качество:'),AT(27,43),USE(?JPEGQualityPrompt)
       SPIN(@n3),AT(80,43,36,12),USE(JpegQuality),LEFT(2),RANGE(0,100),STEP(5)
       BUTTON('Сохранить'),AT(50,70,191,14),USE(?GetImageButton)
     END


fi      FreeImageClass      !need an instance of the freeimage class and
cb      SaClipboardClass    !an instance of the clipboard class

hMem UNSIGNED,Auto
  Code

  FileName = command('fname')
  TovarName = command('tname')

  if FileName='' or TovarName=''
     return
  .

  Open(W)

  Accept

    Case Field()
    Of ?GetImageButton
      If Event() = EVENT:Accepted
        !--------------------------------------------------------
        ! first try to open the clipboard
        !--------------------------------------------------------
        If cb.OpenClipboard(w) = True

          !--------------------------------------------------------
          ! If there's an image on the clipboard that the clipboard class
          ! knows how to manage it will make a copy of it and store the
          ! handle to the global memory in the hMem variable.  Otherwise
          ! hMem will equal zero on failure.
          !--------------------------------------------------------
          hMem = cb.GetClipboardCopy(SA_CF_DIB)

          If hMem <> 0

            !--------------------------------------------------------
            ! Now that you have a copy of the image from the clipboard
            ! try to load it into the FreeImage object.  Once it's in the
            ! FreeImage object you can manipulate it and save it to a file
            ! in any of the supported image formats.
            !--------------------------------------------------------
            If fi.iImage.Load(hMem, SA_GlobalSize(hMem)) = True

              !--------------------------------------------------------
              ! The image was loaded into the FreeImage object.
              ! Change the color depth to 24 bits per pixel if necessary.
              !--------------------------------------------------------
              If fi.Iimage.GetBPP() <> 24 Then
                fi.Iimage.ConvertColorDepth(FI_24BIT,,)
              End

              !--------------------------------------------------------
              ! Before saving the image you can set supported save options.
              ! The FreeImage documentation lists the options supported by
              ! each image format.  
              !--------------------------------------------------------
              fi.Iimage.SetSaveOption(JpegQuality) !jpegs support 0 to 100 quality
              !fi.Iimage.SetSaveOption(JPEG_QUALITYSUPERB) !jpegs support 0 to 100 quality

              !--------------------------------------------------------
              ! Finally call the SaveAs method to convert the DIB to a JPEG.
              !--------------------------------------------------------
              If fi.iImage.SaveAs(FileName,FIF_JPEG) = True
                !Message('Saved the image to the file named: "'&fi.iImage.GetFilename()&'"','Clarion FreeImage')
                break
              Else
                Message('Не могу сохранить фото','Ошибка')
              End
            End
            SA_GlobalFree(hMem) !You must free the memory when done.
          Else
            Message('Нет фото товара в буфере обмена!','Ошибка',Icon:Exclamation)
          End
          cb.CloseClipboard()

        Else
          Message('Не могу получить доступ к буферу обмена!','Ошибка',Icon:Exclamation)
        End !open clipboard sucessful

      End !Event
    End  !Field
  End !Accept

  Close(W)
  Return
