• Put PNG into Windows clipboard

    From Alexandru@21:1/5 to All on Thu Apr 6 14:47:25 2023
    I would like to:
    1. copy the content of an window into an image,
    2. replace the white pixels in the image with transparent ones,
    3. place the image into the Windows clipboard

    The issue is at point 3.
    Currently I can realize point 3 only with bitmap format:

    ::twapi::open_clipboard
    ::twapi::empty_clipboard
    # First 14 bytes are bitmapfileheader - get rid of this
    ::twapi::write_clipboard 8 [string range\
    [binary decode base64 [$imgname data -format bmp]] 14 end]
    ::twapi::close_clipboard

    But bitmaps does not support transparent pixels, right?
    So I guess I must use PNG format.
    But how can I place PNG format into the clipboard?

    Many thanks
    Alexandru

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alexandru@21:1/5 to [email protected] on Thu Apr 6 17:05:36 2023
    [email protected] schrieb am Freitag, 7. April 2023 um 01:43:24 UTC+2:
    May be this help you : https://wiki.tcl-lang.org/page/Copy+image+to+and+from+the+Windows+clipboard.
    Saludos,

    Alejandro

    Thanks, but the example seems to do the same thing as I did until now: The procedure Img2Clipboard puts BMP format into the clipboard, not PNG.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From [email protected]@21:1/5 to All on Thu Apr 6 16:43:21 2023
    May be this help you : https://wiki.tcl-lang.org/page/Copy+image+to+and+from+the+Windows+clipboard.
    Saludos,

    Alejandro

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christian Gollwitzer@21:1/5 to All on Fri Apr 7 08:43:14 2023
    Am 07.04.23 um 02:05 schrieb Alexandru:
    [email protected] schrieb am Freitag, 7. April 2023 um 01:43:24 UTC+2:
    May be this help you : https://wiki.tcl-lang.org/page/Copy+image+to+and+from+the+Windows+clipboard.
    Saludos,

    Alejandro

    Thanks, but the example seems to do the same thing as I did until now: The procedure Img2Clipboard puts BMP format into the clipboard, not PNG.

    The bottom example handles PNG, but it is the otehr way round (get PNG
    from clipboard)

    Christian

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alexandru@21:1/5 to Harald Oehlmann on Fri Apr 7 02:43:03 2023
    Harald Oehlmann schrieb am Freitag, 7. April 2023 um 11:39:06 UTC+2:
    Am 06.04.2023 um 23:47 schrieb Alexandru:
    I would like to:
    1. copy the content of an window into an image,
    2. replace the white pixels in the image with transparent ones,
    3. place the image into the Windows clipboard

    The issue is at point 3.
    Currently I can realize point 3 only with bitmap format:

    ::twapi::open_clipboard
    ::twapi::empty_clipboard
    # First 14 bytes are bitmapfileheader - get rid of this ::twapi::write_clipboard 8 [string range\
    [binary decode base64 [$imgname data -format bmp]] 14 end] ::twapi::close_clipboard

    But bitmaps does not support transparent pixels, right?
    So I guess I must use PNG format.
    But how can I place PNG format into the clipboard?

    Many thanks
    Alexandru
    In BMP, you don't have an alpha channel, but you have a transparent
    color (like in gif). If this is ok for you, use it.
    It is quite uncommon to paste png via Windows clipbord. The BMP& friends
    is used. There might be alpha channels there to.

    Take care,
    Harald

    Thanks. How can I define tranparent color?
    Currently my procedure does this:

    proc ImageColorSetTransparent {img {color #ffffff}} {
    set y 0
    foreach row [$img data] {
    set x 0
    foreach pixel $row {
    if {$color==$pixel} {
    $img transparency set $x $y 1
    }
    incr x
    }
    incr y
    }
    }

    How can I adapt it to make BMP transparent?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Harald Oehlmann@21:1/5 to All on Fri Apr 7 11:39:04 2023
    Am 06.04.2023 um 23:47 schrieb Alexandru:
    I would like to:
    1. copy the content of an window into an image,
    2. replace the white pixels in the image with transparent ones,
    3. place the image into the Windows clipboard

    The issue is at point 3.
    Currently I can realize point 3 only with bitmap format:

    ::twapi::open_clipboard
    ::twapi::empty_clipboard
    # First 14 bytes are bitmapfileheader - get rid of this
    ::twapi::write_clipboard 8 [string range\
    [binary decode base64 [$imgname data -format bmp]] 14 end]
    ::twapi::close_clipboard

    But bitmaps does not support transparent pixels, right?
    So I guess I must use PNG format.
    But how can I place PNG format into the clipboard?

    Many thanks
    Alexandru

    In BMP, you don't have an alpha channel, but you have a transparent
    color (like in gif). If this is ok for you, use it.
    It is quite uncommon to paste png via Windows clipbord. The BMP& friends
    is used. There might be alpha channels there to.

    Take care,
    Harald

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alexandru@21:1/5 to Christian Gollwitzer on Fri Apr 7 02:41:26 2023
    Christian Gollwitzer schrieb am Freitag, 7. April 2023 um 08:43:19 UTC+2:
    Am 07.04.23 um 02:05 schrieb Alexandru:
    [email protected] schrieb am Freitag, 7. April 2023 um 01:43:24 UTC+2:
    May be this help you : https://wiki.tcl-lang.org/page/Copy+image+to+and+from+the+Windows+clipboard.
    Saludos,

    Alejandro

    Thanks, but the example seems to do the same thing as I did until now: The procedure Img2Clipboard puts BMP format into the clipboard, not PNG.
    The bottom example handles PNG, but it is the otehr way round (get PNG
    from clipboard)

    Christian
    Yes, I realize this too.
    I tried to reverse the procedure using this
    ::twapi::write_clipboard 49406 [$imgname data -format png]
    but nothing happens, not even an error.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Harald Oehlmann@21:1/5 to All on Fri Apr 7 12:00:26 2023
    Am 07.04.2023 um 11:43 schrieb Alexandru:
    Harald Oehlmann schrieb am Freitag, 7. April 2023 um 11:39:06 UTC+2:
    Am 06.04.2023 um 23:47 schrieb Alexandru:
    I would like to:
    1. copy the content of an window into an image,
    2. replace the white pixels in the image with transparent ones,
    3. place the image into the Windows clipboard

    The issue is at point 3.
    Currently I can realize point 3 only with bitmap format:

    ::twapi::open_clipboard
    ::twapi::empty_clipboard
    # First 14 bytes are bitmapfileheader - get rid of this
    ::twapi::write_clipboard 8 [string range\
    [binary decode base64 [$imgname data -format bmp]] 14 end]
    ::twapi::close_clipboard

    But bitmaps does not support transparent pixels, right?
    So I guess I must use PNG format.
    But how can I place PNG format into the clipboard?

    Many thanks
    Alexandru
    In BMP, you don't have an alpha channel, but you have a transparent
    color (like in gif). If this is ok for you, use it.
    It is quite uncommon to paste png via Windows clipbord. The BMP& friends
    is used. There might be alpha channels there to.

    Take care,
    Harald

    Thanks. How can I define tranparent color?
    Currently my procedure does this:

    proc ImageColorSetTransparent {img {color #ffffff}} {
    set y 0
    foreach row [$img data] {
    set x 0
    foreach pixel $row {
    if {$color==$pixel} {
    $img transparency set $x $y 1
    }
    incr x
    }
    incr y
    }
    }

    How can I adapt it to make BMP transparent?

    The bmp header has the color index of the transparent color.
    That should just work

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alexandru@21:1/5 to Harald Oehlmann on Fri Apr 7 03:44:47 2023
    Harald Oehlmann schrieb am Freitag, 7. April 2023 um 12:00:30 UTC+2:
    Am 07.04.2023 um 11:43 schrieb Alexandru:
    Harald Oehlmann schrieb am Freitag, 7. April 2023 um 11:39:06 UTC+2:
    Am 06.04.2023 um 23:47 schrieb Alexandru:
    I would like to:
    1. copy the content of an window into an image,
    2. replace the white pixels in the image with transparent ones,
    3. place the image into the Windows clipboard

    The issue is at point 3.
    Currently I can realize point 3 only with bitmap format:

    ::twapi::open_clipboard
    ::twapi::empty_clipboard
    # First 14 bytes are bitmapfileheader - get rid of this
    ::twapi::write_clipboard 8 [string range\
    [binary decode base64 [$imgname data -format bmp]] 14 end]
    ::twapi::close_clipboard

    But bitmaps does not support transparent pixels, right?
    So I guess I must use PNG format.
    But how can I place PNG format into the clipboard?

    Many thanks
    Alexandru
    In BMP, you don't have an alpha channel, but you have a transparent
    color (like in gif). If this is ok for you, use it.
    It is quite uncommon to paste png via Windows clipbord. The BMP& friends >> is used. There might be alpha channels there to.

    Take care,
    Harald

    Thanks. How can I define tranparent color?
    Currently my procedure does this:

    proc ImageColorSetTransparent {img {color #ffffff}} {
    set y 0
    foreach row [$img data] {
    set x 0
    foreach pixel $row {
    if {$color==$pixel} {
    $img transparency set $x $y 1
    }
    incr x
    }
    incr y
    }
    }

    How can I adapt it to make BMP transparent?
    The bmp header has the color index of the transparent color.
    That should just work
    Here are both procedures, one that creates an image, one that puts it into the clipboard as BMP and also saves it to an PNG file.

    The PNG file is transparent, the BMP in the clipboard is not.

    proc Image2Clipboard {imgname} {
    ::twapi::open_clipboard
    ::twapi::empty_clipboard
    ImageColorSetTransparent $imgname
    # First 14 bytes are bitmapfileheader - get rid of this
    ::twapi::write_clipboard 8 [string range\
    [binary decode base64 [$imgname data -format bmp]] 14 end]
    ::twapi::close_clipboard
    }

    proc ImageColorSetTransparent {img {color #ffffff}} {
    set y 0
    foreach row [$img data] {
    set x 0
    foreach pixel $row {
    if {$color==$pixel} {
    $img transparency set $x $y 1
    }
    incr x
    }
    incr y
    }
    $img write temp.png -format "png -alpha 0.0"
    }

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Obermeier@21:1/5 to All on Sat Apr 8 22:43:24 2023
    Am 07.04.2023 um 12:44 schrieb Alexandru:
    Harald Oehlmann schrieb am Freitag, 7. April 2023 um 12:00:30 UTC+2:
    Am 07.04.2023 um 11:43 schrieb Alexandru:
    Harald Oehlmann schrieb am Freitag, 7. April 2023 um 11:39:06 UTC+2:
    Am 06.04.2023 um 23:47 schrieb Alexandru:
    I would like to:
    1. copy the content of an window into an image,
    2. replace the white pixels in the image with transparent ones,
    3. place the image into the Windows clipboard

    The issue is at point 3.
    Currently I can realize point 3 only with bitmap format:

    ::twapi::open_clipboard
    ::twapi::empty_clipboard
    # First 14 bytes are bitmapfileheader - get rid of this
    ::twapi::write_clipboard 8 [string range\
    [binary decode base64 [$imgname data -format bmp]] 14 end]
    ::twapi::close_clipboard

    But bitmaps does not support transparent pixels, right?
    So I guess I must use PNG format.
    But how can I place PNG format into the clipboard?

    Many thanks
    Alexandru
    In BMP, you don't have an alpha channel, but you have a transparent
    color (like in gif). If this is ok for you, use it.
    It is quite uncommon to paste png via Windows clipbord. The BMP& friends >>>> is used. There might be alpha channels there to.

    Take care,
    Harald

    Thanks. How can I define tranparent color?
    Currently my procedure does this:

    proc ImageColorSetTransparent {img {color #ffffff}} {
    set y 0
    foreach row [$img data] {
    set x 0
    foreach pixel $row {
    if {$color==$pixel} {
    $img transparency set $x $y 1
    }
    incr x
    }
    incr y
    }
    }

    How can I adapt it to make BMP transparent?
    The bmp header has the color index of the transparent color.
    That should just work
    Here are both procedures, one that creates an image, one that puts it into the clipboard as BMP and also saves it to an PNG file.

    The PNG file is transparent, the BMP in the clipboard is not.

    proc Image2Clipboard {imgname} {
    ::twapi::open_clipboard
    ::twapi::empty_clipboard
    ImageColorSetTransparent $imgname
    # First 14 bytes are bitmapfileheader - get rid of this
    ::twapi::write_clipboard 8 [string range\
    [binary decode base64 [$imgname data -format bmp]] 14 end]
    ::twapi::close_clipboard
    }

    proc ImageColorSetTransparent {img {color #ffffff}} {
    set y 0
    foreach row [$img data] {
    set x 0
    foreach pixel $row {
    if {$color==$pixel} {
    $img transparency set $x $y 1
    }
    incr x
    }
    incr y
    }
    $img write temp.png -format "png -alpha 0.0"
    }

    The following script copies and pastes images with alpha channel.
    The reference PNG image with alpha channel can be downloaded here: https://www.tcl3d.org/bawt/download/Preview/vegetation.png

    set useImg false
    set f "vegetation.png"

    package require twapi
    package require Tk
    puts -nonewline "Using Tcl [info patch], Tk [package version Tk]"
    if { $useImg } {
    package require Img
    puts -nonewline ", Img [package version Img]"
    }
    puts ""

    set CF_PNG 49460

    proc Clipboard2Img {} {
    twapi::open_clipboard

    # Assume clipboard content is in format CF_PNG
    set retVal [catch {twapi::read_clipboard $::CF_PNG} clipData]
    if { $retVal != 0 } {
    error "Invalid or no content in clipboard"
    }

    set phImg [image create photo]
    $phImg put $clipData -format png

    twapi::close_clipboard
    return $phImg
    }

    proc Img2Clipboard { phImg } {
    twapi::open_clipboard
    twapi::empty_clipboard
    set retVal [catch {package present Img} versionStr]
    if { $retVal == 0 } {
    twapi::write_clipboard $::CF_PNG [binary decode base64 [$phImg data -format png]]
    } else {
    twapi::write_clipboard $::CF_PNG [$phImg data -format png]
    }
    twapi::close_clipboard
    }

    label .l1 -background yellow
    label .l2 -background lightgreen
    pack .l1 .l2

    puts "Load file $f"
    set ph1 [image create photo -file $f -format "png"]
    puts "Put image into clipboard"
    Img2Clipboard $ph1
    .l1 configure -image $ph1
    update

    puts "Get image from clipboard"
    set ph2 [Clipboard2Img]
    .l2 configure -image $ph2
    update

    bind . <Escape> exit

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alexandru@21:1/5 to Paul Obermeier on Tue Apr 11 07:39:45 2023
    Paul Obermeier schrieb am Samstag, 8. April 2023 um 22:43:27 UTC+2:
    Am 07.04.2023 um 12:44 schrieb Alexandru:
    Harald Oehlmann schrieb am Freitag, 7. April 2023 um 12:00:30 UTC+2:
    Am 07.04.2023 um 11:43 schrieb Alexandru:
    Harald Oehlmann schrieb am Freitag, 7. April 2023 um 11:39:06 UTC+2: >>>> Am 06.04.2023 um 23:47 schrieb Alexandru:
    I would like to:
    1. copy the content of an window into an image,
    2. replace the white pixels in the image with transparent ones,
    3. place the image into the Windows clipboard

    The issue is at point 3.
    Currently I can realize point 3 only with bitmap format:

    ::twapi::open_clipboard
    ::twapi::empty_clipboard
    # First 14 bytes are bitmapfileheader - get rid of this
    ::twapi::write_clipboard 8 [string range\
    [binary decode base64 [$imgname data -format bmp]] 14 end]
    ::twapi::close_clipboard

    But bitmaps does not support transparent pixels, right?
    So I guess I must use PNG format.
    But how can I place PNG format into the clipboard?

    Many thanks
    Alexandru
    In BMP, you don't have an alpha channel, but you have a transparent >>>> color (like in gif). If this is ok for you, use it.
    It is quite uncommon to paste png via Windows clipbord. The BMP& friends
    is used. There might be alpha channels there to.

    Take care,
    Harald

    Thanks. How can I define tranparent color?
    Currently my procedure does this:

    proc ImageColorSetTransparent {img {color #ffffff}} {
    set y 0
    foreach row [$img data] {
    set x 0
    foreach pixel $row {
    if {$color==$pixel} {
    $img transparency set $x $y 1
    }
    incr x
    }
    incr y
    }
    }

    How can I adapt it to make BMP transparent?
    The bmp header has the color index of the transparent color.
    That should just work
    Here are both procedures, one that creates an image, one that puts it into the clipboard as BMP and also saves it to an PNG file.

    The PNG file is transparent, the BMP in the clipboard is not.

    proc Image2Clipboard {imgname} {
    ::twapi::open_clipboard
    ::twapi::empty_clipboard
    ImageColorSetTransparent $imgname
    # First 14 bytes are bitmapfileheader - get rid of this ::twapi::write_clipboard 8 [string range\
    [binary decode base64 [$imgname data -format bmp]] 14 end] ::twapi::close_clipboard
    }

    proc ImageColorSetTransparent {img {color #ffffff}} {
    set y 0
    foreach row [$img data] {
    set x 0
    foreach pixel $row {
    if {$color==$pixel} {
    $img transparency set $x $y 1
    }
    incr x
    }
    incr y
    }
    $img write temp.png -format "png -alpha 0.0"
    }
    The following script copies and pastes images with alpha channel.
    The reference PNG image with alpha channel can be downloaded here: https://www.tcl3d.org/bawt/download/Preview/vegetation.png

    set useImg false
    set f "vegetation.png"

    package require twapi
    package require Tk
    puts -nonewline "Using Tcl [info patch], Tk [package version Tk]"
    if { $useImg } {
    package require Img
    puts -nonewline ", Img [package version Img]"
    }
    puts ""

    set CF_PNG 49460

    proc Clipboard2Img {} {
    twapi::open_clipboard

    # Assume clipboard content is in format CF_PNG
    set retVal [catch {twapi::read_clipboard $::CF_PNG} clipData]
    if { $retVal != 0 } {
    error "Invalid or no content in clipboard"
    }

    set phImg [image create photo]
    $phImg put $clipData -format png

    twapi::close_clipboard
    return $phImg
    }

    proc Img2Clipboard { phImg } {
    twapi::open_clipboard
    twapi::empty_clipboard
    set retVal [catch {package present Img} versionStr]
    if { $retVal == 0 } {
    twapi::write_clipboard $::CF_PNG [binary decode base64 [$phImg data -format png]]
    } else {
    twapi::write_clipboard $::CF_PNG [$phImg data -format png]
    }
    twapi::close_clipboard
    }

    label .l1 -background yellow
    label .l2 -background lightgreen
    pack .l1 .l2

    puts "Load file $f"
    set ph1 [image create photo -file $f -format "png"]
    puts "Put image into clipboard"
    Img2Clipboard $ph1
    .l1 configure -image $ph1
    update

    puts "Get image from clipboard"
    set ph2 [Clipboard2Img]
    .l2 configure -image $ph2
    update

    bind . <Escape> exit

    Hi Paul,

    Many thanks, your code is a step forward but though it works inside Tcl environment, it does not work with Windows. It's like the format in the clipboard is not understandable for Windows. After puting the image into the clipboard, I try to paste it into
    PowerPoint and nothing happens. It's like the clipboard were empty, which is not.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Obermeier@21:1/5 to All on Tue Apr 11 19:15:24 2023
    Am 11.04.2023 um 16:39 schrieb Alexandru:
    Paul Obermeier schrieb am Samstag, 8. April 2023 um 22:43:27 UTC+2:
    Am 07.04.2023 um 12:44 schrieb Alexandru:
    Harald Oehlmann schrieb am Freitag, 7. April 2023 um 12:00:30 UTC+2:
    Am 07.04.2023 um 11:43 schrieb Alexandru:
    Harald Oehlmann schrieb am Freitag, 7. April 2023 um 11:39:06 UTC+2: >>>>>> Am 06.04.2023 um 23:47 schrieb Alexandru:
    I would like to:
    1. copy the content of an window into an image,
    2. replace the white pixels in the image with transparent ones,
    3. place the image into the Windows clipboard

    The issue is at point 3.
    Currently I can realize point 3 only with bitmap format:

    ::twapi::open_clipboard
    ::twapi::empty_clipboard
    # First 14 bytes are bitmapfileheader - get rid of this
    ::twapi::write_clipboard 8 [string range\
    [binary decode base64 [$imgname data -format bmp]] 14 end]
    ::twapi::close_clipboard

    But bitmaps does not support transparent pixels, right?
    So I guess I must use PNG format.
    But how can I place PNG format into the clipboard?

    Many thanks
    Alexandru
    In BMP, you don't have an alpha channel, but you have a transparent >>>>>> color (like in gif). If this is ok for you, use it.
    It is quite uncommon to paste png via Windows clipbord. The BMP& friends >>>>>> is used. There might be alpha channels there to.

    Take care,
    Harald

    Thanks. How can I define tranparent color?
    Currently my procedure does this:

    proc ImageColorSetTransparent {img {color #ffffff}} {
    set y 0
    foreach row [$img data] {
    set x 0
    foreach pixel $row {
    if {$color==$pixel} {
    $img transparency set $x $y 1
    }
    incr x
    }
    incr y
    }
    }

    How can I adapt it to make BMP transparent?
    The bmp header has the color index of the transparent color.
    That should just work
    Here are both procedures, one that creates an image, one that puts it into the clipboard as BMP and also saves it to an PNG file.

    The PNG file is transparent, the BMP in the clipboard is not.

    proc Image2Clipboard {imgname} {
    ::twapi::open_clipboard
    ::twapi::empty_clipboard
    ImageColorSetTransparent $imgname
    # First 14 bytes are bitmapfileheader - get rid of this
    ::twapi::write_clipboard 8 [string range\
    [binary decode base64 [$imgname data -format bmp]] 14 end]
    ::twapi::close_clipboard
    }

    proc ImageColorSetTransparent {img {color #ffffff}} {
    set y 0
    foreach row [$img data] {
    set x 0
    foreach pixel $row {
    if {$color==$pixel} {
    $img transparency set $x $y 1
    }
    incr x
    }
    incr y
    }
    $img write temp.png -format "png -alpha 0.0"
    }
    The following script copies and pastes images with alpha channel.
    The reference PNG image with alpha channel can be downloaded here: https://www.tcl3d.org/bawt/download/Preview/vegetation.png

    set useImg false
    set f "vegetation.png"

    package require twapi
    package require Tk
    puts -nonewline "Using Tcl [info patch], Tk [package version Tk]"
    if { $useImg } {
    package require Img
    puts -nonewline ", Img [package version Img]"
    }
    puts ""

    set CF_PNG 49460

    proc Clipboard2Img {} {
    twapi::open_clipboard

    # Assume clipboard content is in format CF_PNG
    set retVal [catch {twapi::read_clipboard $::CF_PNG} clipData]
    if { $retVal != 0 } {
    error "Invalid or no content in clipboard"
    }

    set phImg [image create photo]
    $phImg put $clipData -format png

    twapi::close_clipboard
    return $phImg
    }

    proc Img2Clipboard { phImg } {
    twapi::open_clipboard
    twapi::empty_clipboard
    set retVal [catch {package present Img} versionStr]
    if { $retVal == 0 } {
    twapi::write_clipboard $::CF_PNG [binary decode base64 [$phImg data -format png]]
    } else {
    twapi::write_clipboard $::CF_PNG [$phImg data -format png]
    }
    twapi::close_clipboard
    }

    label .l1 -background yellow
    label .l2 -background lightgreen
    pack .l1 .l2

    puts "Load file $f"
    set ph1 [image create photo -file $f -format "png"]
    puts "Put image into clipboard"
    Img2Clipboard $ph1
    .l1 configure -image $ph1
    update

    puts "Get image from clipboard"
    set ph2 [Clipboard2Img]
    .l2 configure -image $ph2
    update

    bind . <Escape> exit

    Hi Paul,

    Many thanks, your code is a step forward but though it works inside Tcl environment, it does not work with Windows. It's like the format in the clipboard is not understandable for Windows. After puting the image into the clipboard, I try to paste it
    into PowerPoint and nothing happens. It's like the clipboard were empty, which is not.

    I played around with different format numbers and left the wrong one in the script.
    The PNG format number is:
    set CF_PNG 49494

    Paul

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alexandru@21:1/5 to Paul Obermeier on Tue Apr 11 12:24:06 2023
    Paul Obermeier schrieb am Dienstag, 11. April 2023 um 19:15:28 UTC+2:
    Am 11.04.2023 um 16:39 schrieb Alexandru:
    Paul Obermeier schrieb am Samstag, 8. April 2023 um 22:43:27 UTC+2:
    Am 07.04.2023 um 12:44 schrieb Alexandru:
    Harald Oehlmann schrieb am Freitag, 7. April 2023 um 12:00:30 UTC+2: >>>> Am 07.04.2023 um 11:43 schrieb Alexandru:
    Harald Oehlmann schrieb am Freitag, 7. April 2023 um 11:39:06 UTC+2: >>>>>> Am 06.04.2023 um 23:47 schrieb Alexandru:
    I would like to:
    1. copy the content of an window into an image,
    2. replace the white pixels in the image with transparent ones, >>>>>>> 3. place the image into the Windows clipboard

    The issue is at point 3.
    Currently I can realize point 3 only with bitmap format:

    ::twapi::open_clipboard
    ::twapi::empty_clipboard
    # First 14 bytes are bitmapfileheader - get rid of this
    ::twapi::write_clipboard 8 [string range\
    [binary decode base64 [$imgname data -format bmp]] 14 end]
    ::twapi::close_clipboard

    But bitmaps does not support transparent pixels, right?
    So I guess I must use PNG format.
    But how can I place PNG format into the clipboard?

    Many thanks
    Alexandru
    In BMP, you don't have an alpha channel, but you have a transparent >>>>>> color (like in gif). If this is ok for you, use it.
    It is quite uncommon to paste png via Windows clipbord. The BMP& friends
    is used. There might be alpha channels there to.

    Take care,
    Harald

    Thanks. How can I define tranparent color?
    Currently my procedure does this:

    proc ImageColorSetTransparent {img {color #ffffff}} {
    set y 0
    foreach row [$img data] {
    set x 0
    foreach pixel $row {
    if {$color==$pixel} {
    $img transparency set $x $y 1
    }
    incr x
    }
    incr y
    }
    }

    How can I adapt it to make BMP transparent?
    The bmp header has the color index of the transparent color.
    That should just work
    Here are both procedures, one that creates an image, one that puts it into the clipboard as BMP and also saves it to an PNG file.

    The PNG file is transparent, the BMP in the clipboard is not.

    proc Image2Clipboard {imgname} {
    ::twapi::open_clipboard
    ::twapi::empty_clipboard
    ImageColorSetTransparent $imgname
    # First 14 bytes are bitmapfileheader - get rid of this
    ::twapi::write_clipboard 8 [string range\
    [binary decode base64 [$imgname data -format bmp]] 14 end]
    ::twapi::close_clipboard
    }

    proc ImageColorSetTransparent {img {color #ffffff}} {
    set y 0
    foreach row [$img data] {
    set x 0
    foreach pixel $row {
    if {$color==$pixel} {
    $img transparency set $x $y 1
    }
    incr x
    }
    incr y
    }
    $img write temp.png -format "png -alpha 0.0"
    }
    The following script copies and pastes images with alpha channel.
    The reference PNG image with alpha channel can be downloaded here: https://www.tcl3d.org/bawt/download/Preview/vegetation.png

    set useImg false
    set f "vegetation.png"

    package require twapi
    package require Tk
    puts -nonewline "Using Tcl [info patch], Tk [package version Tk]"
    if { $useImg } {
    package require Img
    puts -nonewline ", Img [package version Img]"
    }
    puts ""

    set CF_PNG 49460

    proc Clipboard2Img {} {
    twapi::open_clipboard

    # Assume clipboard content is in format CF_PNG
    set retVal [catch {twapi::read_clipboard $::CF_PNG} clipData]
    if { $retVal != 0 } {
    error "Invalid or no content in clipboard"
    }

    set phImg [image create photo]
    $phImg put $clipData -format png

    twapi::close_clipboard
    return $phImg
    }

    proc Img2Clipboard { phImg } {
    twapi::open_clipboard
    twapi::empty_clipboard
    set retVal [catch {package present Img} versionStr]
    if { $retVal == 0 } {
    twapi::write_clipboard $::CF_PNG [binary decode base64 [$phImg data -format png]]
    } else {
    twapi::write_clipboard $::CF_PNG [$phImg data -format png]
    }
    twapi::close_clipboard
    }

    label .l1 -background yellow
    label .l2 -background lightgreen
    pack .l1 .l2

    puts "Load file $f"
    set ph1 [image create photo -file $f -format "png"]
    puts "Put image into clipboard"
    Img2Clipboard $ph1
    .l1 configure -image $ph1
    update

    puts "Get image from clipboard"
    set ph2 [Clipboard2Img]
    .l2 configure -image $ph2
    update

    bind . <Escape> exit

    Hi Paul,

    Many thanks, your code is a step forward but though it works inside Tcl environment, it does not work with Windows. It's like the format in the clipboard is not understandable for Windows. After puting the image into the clipboard, I try to paste it
    into PowerPoint and nothing happens. It's like the clipboard were empty, which is not.
    I played around with different format numbers and left the wrong one in the script.
    The PNG format number is:
    set CF_PNG 49494

    Paul

    Hi Paul, I tried again with CF_PNG=49494 and still the same result.
    Where do you get those numbers from? I couldn't find anything in the oficial Microsoft documentation.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From [email protected]@21:1/5 to All on Tue Apr 11 16:19:51 2023
    Hi Alexandru,

    It seems that the Windows clipboard has a long story of problems with transparency support .
    I guess a practical approach for you could be use a program to view the clipboard's content such as InsideClipboard of www.nirsoft.net.
    For example, if i copy a PNG file from MSPaint , InsideClipboard show me this :


    3 CF_METAFILEPICT Metafile 0 3
    8 CF_DIB Memory 20.480 4
    49163 Embed Source Storage 0 1
    49166 Object Descriptor Memory 174 2

    ( the columns are Format ID, Format Name, Handle Type, Size and Index respectively)

    In other hand other program, Free Clipboard Viewer, show more formats, previews, etc but not the format code.

    Saludos,

    Alejandro

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alexandru@21:1/5 to [email protected] on Tue Apr 11 22:33:22 2023
    [email protected] schrieb am Mittwoch, 12. April 2023 um 01:19:54 UTC+2:
    Hi Alexandru,

    It seems that the Windows clipboard has a long story of problems with transparency support .
    I guess a practical approach for you could be use a program to view the clipboard's content such as InsideClipboard of www.nirsoft.net.
    For example, if i copy a PNG file from MSPaint , InsideClipboard show me this :


    3 CF_METAFILEPICT Metafile 0 3
    8 CF_DIB Memory 20.480 4
    49163 Embed Source Storage 0 1
    49166 Object Descriptor Memory 174 2

    ( the columns are Format ID, Format Name, Handle Type, Size and Index respectively)

    In other hand other program, Free Clipboard Viewer, show more formats, previews, etc but not the format code.

    Saludos,

    Alejandro

    Thanks Alejandro,

    I will definitely look into that.

    Paul also sent me a partial solution that works with most Windows programs, but not with IrfanView. It all depends on that CL_FORMAT, so Paul wrote a function that find out what the current PNG format is (it can change after a computer restart):

    proc ClipcoardFormatFind {fmtName} {
    for {set fmtNum 1} {$fmtNum<100000} {incr fmtNum} {
    set retVal [catch {twapi::get_registered_clipboard_format_name $fmtNum} name]
    if {$retVal==0} {
    if {$name eq $fmtName} {
    return $fmtNum
    }
    }
    }
    return -1
    }

    Now I must find out, how I can make it work also with IrfanView.
    The clipboard inspection programs you proposed should help.

    Regards
    Alexandru

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From saitology9@21:1/5 to Paul Obermeier on Thu Apr 13 13:40:23 2023
    On 4/11/2023 1:15 PM, Paul Obermeier wrote:

    I played around with different format numbers and left the wrong one in
    the script.
    The PNG format number is:
    set CF_PNG 49494

    Paul



    I can confirm that the latest version you posted on the wiki works fine!
    I think I am going to copy it and maybe use it soon.

    Thanks!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)