dcsimg
Results 1 to 32 of 32

Thread: Copy/Paste Images w/Transparency

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,633

    Question Copy/Paste Images w/Transparency

    This might belong in Gaming and Graphics, so if it moves it won't bother me.

    Local guy asked me if I have some "minimal code, no 3rd party dependencies" to perform a couple of simple tricks. I figure most of the "code" required would be defining structs and function calls and such but I haven't dug into it myself. I suggested he join here if he's really doing much VB6 programming.


    1.) Basically, he wants to be able to "load" non-animated GIF and PNG images from disk and byte arrays within a VB6 program and copy them to the Clipboard. He wants this to work in MS Paint to paste preserving any transparency or translucency of the original image data. Maybe CF_DIBV5 format?

    Windows 10 Paint would be all he wanted, but Win7 support would be useful to him too if it happens MS Paint is a lot different there. If MS Paint can't do this at all then he'd like to do it in Word. Weird choice, but there you go.


    2.) As a bonus he'd like to be able to paste the same clipboard data in a VB6 program and render that into a PictureBox or Form with transparency.


    3.) Bonus bonus would be to do all of this using StdPicture objects to hold images, but the inherent limitations there would probably require something like LaVolpe's extender tricks. And LoadPicture would only handle GIFs at best as far as I know.


    So is anyone currently bored enough to play with this? Or is there already something in the CodeBank that my searching has failed me on? Keep in mind that the Clipboard aspect is the most important part of it.

    Things are slow lately so I thought people might be up for a minor challenge.

  2. #2
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,341

    Re: Copy/Paste Images w/Transparency

    I've got a demo that does transparent copy/paste/drag/drop with CF_PNG, and further down the thread in my post about Explorer drag images, there's a beta version of the class for reading CF_DIB and CF_DIBV5 to the drag/drop IDataObjet, which can be dropped into the clipboard functionality of the dragformats project since the IDataObject handling is the same between drag/drop and copy/paste (and simply OleGet/SetClipboard to pass it that way).
    (Edit: I thought it had writing the DIB formats but apparently not, let me look but it's the same principles as PNG)

    If you count the TLB as a 3rd party dependency obviously you can sub in direct vtable calls for IDataObject.

  3. #3
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    268

    Re: Copy/Paste Images w/Transparency

    I found these when I searched for Copy PNG to Clipboard with Transparency:

    Quote Originally Posted by Nyerguds
    Copying From and To Clipboard loses image transparency

    The Windows clipboard, by default, does not support transparency, but you can put content on the clipboard in many types together to make sure most applications find some type in it that they can use. Sadly, the most common type (which Windows itself seems to use) is a really dirty and unreliable one. I wrote a big ̶r̶a̶n̶t̶ explanation about that here.

    I'll assume you have read through that before continuing with my answer here, because it contains the background information required for the next part.

    Now, the cleanest way of putting an image on the clipboard with transparency support is a PNG stream, but it won't guarantee that all applications can paste it. Gimp supports PNG paste, and apparently so do the newer MS Office programs, but Google Chrome, for example, doesn't, and will only accept the messy DIB type detailed in the answer I linked to. On the other hand, Gimp will not accept DIB as having transparency, because its creators actually followed the format's specifications, and realized that the format was unreliable (as clearly demonstrated by that question I linked).

    Because of the DIB mess, sadly, the best thing to do is simply to put it in there in as many generally-supported types as you can, including PNG, DIB and the normal Image.

    PNG and DIB are both put on the clipboard in the same way: by putting them in the DataObject as MemoryStream, and then giving the clipboard the "copy" instruction when actually putting it on.

    Read more...
    Quote Originally Posted by Nyerguds
    Text erased from screenshot after using Clipboard.GetImage() on Windows 10?

    I've been researching the windows clipboard, and I think I know what is going on. Bear with me, this is a pretty long explanation, and it took a lot of research to get enough information on the DIB format that's at the core of this mess.

    By default, the clipboard doesn't support transparency at all, for legacy reasons; back when the clipboard was first designed there was no such thing as alpha channels. As you may know, though, multiple formats can be put on the clipboard together, to give programs reading the clipboard a wider range of possibilities of getting data out, and it seems that in recent Windows versions, the image apparently also gets put on the clipboard in DIB (Device Independent Bitmap) format. Now, the .Net framework v3.5 normally takes the standard non-transparent "Image" format version from the clipboard, so it never gives transparency, but it seems the 4.5 one might actually get this DIB one instead.

    The DIB, as it exists on the clipboard, is technically a 32 bits per pixel RGB image. Note, RGB, not ARGB. This means that there are four bytes per pixel, but while the red, green and blue bytes are filled in normally, the fourth byte is actually undefined, and should not be counted on to actually mean "alpha". It's just there to get the data nicely aligned to a multiple of four bytes.

    Read more...
    Quote Originally Posted by Tor Lillqvist
    Copying image with transparency gives black background

    Note that as far as I know, the only officially documented DIB format with per-pixel-alpha is one with a BITMAPV4HEADER or BITMAPV5HEADER, compression BI_BITFIELDS and then an appropriate bV4AlphaMask or bV5AlphaMask. Unfortunately, though, it seems that if you explicitly (in a test program that fills in all fields "manually" without relying on any other code) create a .bmp file that contains such a DIB, it won't display correctly in Firefox, Explorer, or IE7. It will load correctly into GIMP and PixelFormer (a small commercial bitmap graphics editor), but not into Photoshop Elements 5.

    I am not 100% sure if I tested this, I was hacking like crazy last weekend on copy/paste of images between GTK+ and other apps on Windows, but I think I saw that none of the apps I tested with managed to paste from the clipboard correctly a DIB in the above BITMAPV5HEADER 32-bit BI_BITFIELDS with alpha format, though.

    I think it would be great if Firefox would offer images on the clipboard also in the unambiguous PNG format. (Simply a PNG image stored in the clipboard as the named clipboard format "PNG".) For instance Word and Powerpoint offer images in that format, and GTK+ on Windows will in the future, too.

  4. #4
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,341

    Re: Copy/Paste Images w/Transparency

    Does someone have code to load pictures into memory and the BITMAPV5 struct? Because then you just point an IDataObject that way and call OleSetClipboard.

    I'm losing transparency reading CF_DIBV5 from Firefox too. (Or maybe GDI+'s HBITMAP conversion?)

    Edit: mspaint in Win7 doesn't support CF_PNG so that's going to be the only option.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,633

    Re: Copy/Paste Images w/Transparency

    I'm starting to think this guy who asked me about this had already looked into it himself. Maybe he thought the question was some sort of hilarious prank?

  6. #6
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,341

    Re: Copy/Paste Images w/Transparency

    Well I don't know how much of a prank it is considering how much fun it was working on it

    I checked and mspaint on Win10 doesn't support CF_PNG.... that 'Paint 3D' app does tho. And I think Word does on both.

    In any case, I bet one of the graphics guys has something to load a file into a BITMAPv5 struct, if we can find that everything else is already good to go besides the bonus bonus of StdPicture.

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,451

    Re: Copy/Paste Images w/Transparency

    FYI. I think you can find BmpV5 information from project that run alpha-AVIs since clipboard is used in retrieving frames, if I recall correctly.

    The question would remain is if Paint will look for BmpV5 (if first loaded into clipboard), or default to auto-appended formats. Does Windows put simpler Bmp formats on clipboard automatically when Bmpv5 is placed?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,633

    Re: Copy/Paste Images w/Transparency

    I wouldn't think a TLB would give most programmers heartburn as long as the source and "make" instructions using standard tools are available. Sometimes you can end up with a lengthy chain of includes/imports though. The more somebody tries to pile into a single TLB the more aggravating they can become when anything requires updating.

    Pretty rare to need them at runtime anyway unless the program uses them to translate enum item names to values or look up the value names for enums to populate lists and such. That would mostly be developer utilities.


    So add multiple formats? DIBv5, PNG stream - probably just the raw bytes of a PNG file, right? Also a flat DIB (no alpha)? DIB vs. BMP clipboard formats?

    Different versions of different Office programs probably vary somewhat in what they will copy or paste too, hmm? I don't see any clear discussion of that in any Microsoft docs, KBs, etc.

    I suppose once there is some "pattern" code handling the more common needs one can always add special cases as needed.


    This feels like an example of the kind of thing that makes it hard to take the "Well, I'll just move to Linux" crowd seriously. Seems like even finding the breadcrumbs to follow could be pretty hard, much less dealing with the quirks of various distributions and applications that may use entirely different GUI shells and libraries.

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,633

    Re: Copy/Paste Images w/Transparency

    I keep expecting a new member to come in and blow us away with some amazingly trivial solution, like the punchline to a joke.

  10. #10
    Hyperactive Member
    Join Date
    Feb 2019
    Posts
    443

    Re: Copy/Paste Images w/Transparency

    Sounds like your friend doesn't know about Inkscape. Youtube has a lot of tutorials.

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,633

    Re: Copy/Paste Images w/Transparency

    I don't know much more than I put into post #1 though. I don't know him very well, we were just in a breakout group on VB6 at a "programming expo" thing at the local community college and he added it to a list of challenges the group compiled. Sadly there were only 7 people interested in VB6 enough to join us this year out of maybe 400 programmers. Last year there were more like 12. None of them are members here as far as I can tell, at least nobody showed any recognition when I asked.

    I wish I'd written down the entire list. If I had it I'd post them here for those who wanted some things to play with in their spare time.

    It was tough sitting through the presentations this time. Almost everything was about JavaScript and Python this year.


    I'm not sure where Inkscape fits in. I think Paint and Office were just mentioned because most people have them.

  12. #12
    New Member
    Join Date
    Sep 2017
    Posts
    12

    Re: Copy/Paste Images w/Transparency

    Quote Originally Posted by dilettante View Post
    Almost everything was about JavaScript and Python this year.
    Those two are currently at the peak of their popularity. No doubt when quantum programming languages becomes the norm, they'll fade away just like VB6 (and eventually, VB.NET).

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,633

    Re: Copy/Paste Images w/Transparency

    Sadly I think these kinds of local, cheap, face-to-face programmer-oriented events ($10 tickets to get in to cover use of the facilities) are just about extinct. If not for two motivated instructors there we probably wouldn't have ours either. Both guys are on the verge of retiring so we'll be lucky to get another one.

  14. #14
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,341

    Re: Copy/Paste Images w/Transparency

    Quote Originally Posted by dilettante View Post
    I wouldn't think a TLB would give most programmers heartburn as long as the source and "make" instructions using standard tools are available. Sometimes you can end up with a lengthy chain of includes/imports though. The more somebody tries to pile into a single TLB the more aggravating they can become when anything requires updating.

    Pretty rare to need them at runtime anyway unless the program uses them to translate enum item names to values or look up the value names for enums to populate lists and such. That would mostly be developer utilities.


    So add multiple formats? DIBv5, PNG stream - probably just the raw bytes of a PNG file, right? Also a flat DIB (no alpha)? DIB vs. BMP clipboard formats?

    Different versions of different Office programs probably vary somewhat in what they will copy or paste too, hmm? I don't see any clear discussion of that in any Microsoft docs, KBs, etc.

    I suppose once there is some "pattern" code handling the more common needs one can always add special cases as needed.


    This feels like an example of the kind of thing that makes it hard to take the "Well, I'll just move to Linux" crowd seriously. Seems like even finding the breadcrumbs to follow could be pretty hard, much less dealing with the quirks of various distributions and applications that may use entirely different GUI shells and libraries.
    Oh I'm very sensitive to those issues, so full source is right there, no other files needed to compile with MKTYPLIB. And compile shortcut you just have to adjust to your local MKTYPLIB path. Same deal for the 3 other VB6-compatible typelibs I know of that would have IDataObject, minus the compile shortcut, but you don't need any custom arguments so it's a triviality anyway. But again not too hard to just use v-table calls instead if that was too much, plenty of examples of that here.

    Re: Formats... yeah you could as many of them as needed. CF_PNG is just the whole file put into memory yeah. But only that and CF_DIBV5 will support transparency.

  15. #15
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,579

    Re: Copy/Paste Images w/Transparency

    Just tested CF_PNG = RegisterClipboardFormat("PNG") w/ MS Paint -- does *not* work. WinWord pastes ok.

    Edit: CF_DIBV5 is *not* supported both in MS Paint and WinWord.

    CF_DIB with "hacked" transparency (biCompression = BI_BITFIELDS) does *not* render transparent in both MS Paint and WinWord.

    I cannot find a single instance when MS Paint supports transparentcy of any kind -- image loading, clipboard operation, image writing -- nada! So this seems hopeless IMO.

    I'm using my AlphaBlendImage repo for tests in commit 1aaed79 added GdipSetClipboardDib and GdipSetClipboardDibV5. Note that the GdipLoadPicture function already implements the "bonuses" -- can load StdPicture with alpha-transparent image as an hIcon w/ PNG data.

    cheers,
    </wqw>

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,633

    Re: Copy/Paste Images w/Transparency

    Perhaps the MS Paint support was the punchline/red herring then. Even MS Word might not work until recent versions > 2003?

  17. #17
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,801

    Re: Copy/Paste Images w/Transparency

    I'm not sure if I'm on the same track of this thread or not, but I've certainly got a couple of related questions. (Dil, I'll start my own thread, if you'd like.)

    First, let it be said that I don't mess with images a great deal, but I do occasionally. Also, I'm not the VB6 expert with them, but I do dabble (as shown by this codebank entry).

    But, here are my related questions:

    ------------

    #1: I certainly wouldn't mind some VB6 code that placed either a PNG or TGA type file into the clipboard (including any alpha channel that was found).

    ------------

    #2: I wouldn't mind an explanation of the difference between an alpha channel and a transparent background. And admittedly, I possibly just haven't looked into that one enough. Or, maybe it's the software I'm using to open these file types. Typically, I use an old version of Paint-Shop-Pro (either version 5 or version 7) to open these things. I've got the later Corel version, but I hate the interface. And I don't have PhotoShop. I sometimes use Gimp or even Tanner's PhotoDemon, but most typically my old PSP versions.

    And, for some PNG files, they open and clearly have an alpha channel that I can pull into the PSP mask, and edited if I like. However, for other PNG files, they open and have a transparent background (in some areas, like a mask), but they don't have any alpha channel. I've never known the difference, and that's my question #2.

    I can guess that maybe one has an alpha channel with intermediate values (0 < value < 255) and the other has an alpha channel with only 0 or 255 values. But I haven't tested that theory. If that's not it, then how do these PNG files have a transparency mask that's not in the alpha channel?
    Last edited by Elroy; Nov 11th, 2019 at 09:03 AM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  18. #18
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,451

    Re: Copy/Paste Images w/Transparency

    Elroy. You can place anything on the clipboard. The question is will the expected target that will be pasting it be able to recognize it. Each clipboard item has a format (CF_xxxx) or a custom one that is registered. The target has the right to examine the clipboard for formats it can handle, by either preference or order that the format(s) were added to the clipboard. Bottom line. If the target doesn't want it, isn't coded to work with the format, then it won't use it.

    Regarding transparency. There are a couple basic types:
    1. Alpha channel. Each pixel has its own level of transparency/opacity
    -- I call this type: complex transparency
    2. Mask. A specific color or palette index is made 100% transparent. Typically, the alpha channel is not used as alpha
    -- I call this type: simple transparency
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  19. #19
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,801

    Re: Copy/Paste Images w/Transparency

    Quote Originally Posted by LaVolpe View Post
    Elroy. You can place anything on the clipboard. The question is will the expected target that will be pasting it be able to recognize it. Each clipboard item has a format (CF_xxxx) or a custom one that is registered. The target has the right to examine the clipboard for formats it can handle, by either preference or order that the format(s) were added to the clipboard. Bottom line. If the target doesn't want it, isn't coded to work with the format, then it won't use it.
    Hi LaVolpe,

    Yeah, I'm mostly aware. In fact, it's my understanding of the clipboard that you can have multiple clipboard formats simultaneously in the clipboard (and apparently arranged in some hierarchy of preference), with each format representing the item in slightly different ways. For example, Unicode text, RTF text, and ASCII text.

    But, I just don't know how, from VB6, to get a PNG (or TGA, either with alpha channel info) into the clipboard. And, even further, possibly get a standard BMP image in there for the same image as an alternative (again, from VB6).

    Quote Originally Posted by LaVolpe View Post
    Regarding transparency. There are a couple basic types:
    1. Alpha channel. Each pixel has its own level of transparency/opacity
    -- I call this type: complex transparency
    2. Mask. A specific color or palette index is made 100% transparent. Typically, the alpha channel is not used as alpha
    -- I call this type: simple transparency
    Ahh, yes. I've been doing a bit of research on PNG files since my post #17. Apparently, a PNG (and possibly a TGA as well) allows for a specific color value to be "tagged" as the transparency color (much like a GIF, but for full True-Color (24 bit color)). And, as your #1, alternatively, you can have a translucency (or maybe better said as "opacity") value per pixel (i.e., the alpha channel).

    There must be a flag-bit somewhere in PNG files that says whether or not the "tagged" transparency color is being used. Also, this "tagged" transparency color must be ignored if the PNG is an RGBA type file. Is that your understanding as well?
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  20. #20
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,801

    Re: Copy/Paste Images w/Transparency

    Also, just as something further, if I could get the "Simple" vs "Complex" transparencies figured out, I'd love to develop a Simple-->to-->Complex conversion tool for PNG and TGA files in VB6.

    EDIT1: I do appreciate that a Complex-->to-->Simple conversion wouldn't be possible (unless we just decided to take each non-zero alpha channel value and consider it to be a mask). (Also, we'd have to decide on the transparency color.) So, I'm not really interested in doing this direction.

    ------

    EDIT2: Since this is really getting "off topic", I'll start another thread on this aspect. It does seem that my question about getting a PNG with transparency into the clipboard from VB6 is related though.
    Last edited by Elroy; Nov 11th, 2019 at 11:17 AM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  21. #21

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,633

    Re: Copy/Paste Images w/Transparency

    I took a whack at this, but since I only used CF_BITMAP and CF_PNG it doesn't help the MS Paint scenario.

    So here it loads a PNG and copies it, then it can paste to itself in either format:

    Name:  sshot1.png
Views: 73
Size:  7.8 KB

    Name:  sshot2.png
Views: 72
Size:  13.2 KB

    I used WIA 2.0 to create a bitmap StdPicture and VB's Clipboard object for that format. I used an AlphaPic class and CustClip class for the PNG format. Those were just approaches I had on hand so they were quick to try.

    Use the Edit menu to get the sample PNG to the clipboard, then right-click where you want to paste and choose a format.

    Code:
    Private Sub mnuEditCopy_Click()
        Dim F As Integer
        Dim Buffer() As Byte
        Dim ImageProcess As WIA.ImageProcess
        Dim Picture As StdPicture
    
        F = FreeFile(0)
        Open "sample.png" For Binary Access Read As #F
        ReDim Buffer(LOF(F) - 1)
        Get #F, , Buffer
        Close #F
    
        With CustClip
            .OpenClipboard
            .Clear
            .SetData Buffer, cfPng
            .CloseClipboard
            With New WIA.Vector
                .BinaryData = Buffer
                Set ImageProcess = New WIA.ImageProcess
                ImageProcess.Filters.Add ImageProcess.FilterInfos("Convert").FilterID
                ImageProcess.Filters.Item(1).Properties("FormatID").Value = wiaFormatBMP
                Set Picture = ImageProcess.Apply(.ImageFile).FileData.Picture
            End With
            Clipboard.SetData Picture, vbCFBitmap
        End With
        mnuEditCopy.Enabled = False
        mnuPopupeditPastebmp.Enabled = True
        mnuPopupeditPastepng.Enabled = True
    End Sub
    Attached Files Attached Files

  22. #22
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,579

    Re: Copy/Paste Images w/Transparency

    PNG, TGA, TIFF, GIF, BMP -- these are all bitmap *file* formats. In memory we have device-dependant bitmaps -- these are fast and opaque blobs supported only by current GDI driver, and DIBs -- these are portable and not so fast to render. There are also the GDI+ formats (a large number of them) which are similar to Cairo internal formats -- modern, versatile and designed with enought thought process. DIBs are also somewhat intermingled with BMP files format which introduces complex legacy that is a nuisance to support.

    For Clipboard obviously we have to use something portable to be able to copy/paste between different applications, so DIBs are the most logical choice. Using *file* formats (like PNG or TGA) is a bit desparate and this can only be a result of some big trauma, a glaring shortcoming of DIBs. . . exactly what image transparency is for DIBs.

    There is *no* official way to store alpha channel in DIBs. There is semi-official 32 bit pre-multiplied alpha DIBs that work with AlphaBlend API but these have quirks and are hard to defferentiate against 32-bit RGB images w/ no alpha (for instance the ones from Print Screen capture).

    I'm not aware a portable way to store GDI+ images in the system clipboard (even less for Cairo). So we have this tower of babel where PhotoShop is placing it's native PSDs on the clipboard and does not care about other imaging applications and so on. Messy as real life as usual :-))

    cheers,
    </wqw>

  23. #23

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,633

    Re: Copy/Paste Images w/Transparency

    It looks like DIBv5 does explicitly support optional alpha transparency. At least as a container. I'm not sure StretchDIBits() can render that channel though.

  24. #24
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,451

    Re: Copy/Paste Images w/Transparency

    @dilettante, a rather long thread (still 1 page) can be found here
    http://www.vbforums.com/showthread.p...CF_DIB-format)

    That thread talks a lot about the stuff discussed in your thread. I don't know if it will shed any light?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  25. #25
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,385

    Re: Copy/Paste Images w/Transparency

    I agree with wqweto - I don't think Paint supports transparency at all. I tried copying & pasting a PNG from PhotoDemon into Paint, but transparency was not preserved. Tried directly opening a transparent PNG in and transparency was not preserved.

    I tried copying & pasting a transparent PNG from PhotoDemon to both Inkscape & Word 2016 and transparency was preserved in both cases, so it might be worth a look at what Tanner is doing in Photodemon.

  26. #26
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,385

    Re: Copy/Paste Images w/Transparency

    Found this interesting comment in the PhotoDemon pdClipboardMain.cls ClipboardCopy function:

    Code:
            'By default, we offer all supported formats
            If (cFormat = pdcf_All) Then
            
                m_Clipboard.SetClipboardData_DelayedRendering CF_PD_DIB
                m_Clipboard.SetClipboardData_DelayedRendering m_Clipboard.AddClipboardFormat("PNG")
                m_Clipboard.SetClipboardData_DelayedRendering CF_BITMAP
                m_Clipboard.SetClipboardData_DelayedRendering CF_DIBV5
                
                'So an interesting story about CF_DIB.  PD is perfectly capable of rendering CF_DIB images
                ' to the clipboard, but at present, we choose not to enable this.  Why?  Microsoft Paint will
                ' preferentially accept a CF_DIB image over any other format on the clipboard.  This would be
                ' okay if Paint recognized alpha channels, but because it doesn't, we would need to downsample
                ' PD's 32-bpp images to 24-bpp.  However, this would incorrectly generate a 24-bpp copy for
                ' software that *does* recognize 32-bpp CF_DIB entries.  To minimize the risk of a screwed-up
                ' end result, PD only registers CF_BITMAP and CF_DIBv5 formats to the clipboard (plus PNG,
                ' obviously).  It's assumed that any program clever enough to recognize CF_DIBv5 also has a
                ' contingency plan for alpha bytes, whereas the same can't be said for CF_DIB, unfortunately.
                '
                'As always, PNG is the preferred interchange format for images.  If you use a program that
                ' doesn't copy/paste PNG-format data, get them to fix their software!

  27. #27

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,633

    Re: Copy/Paste Images w/Transparency

    Well, I brought this up because this guy was hinting to me that there is at least one solution in VB6 to place a PNG from file bytes onto the clipboard, then paste it into Windows 10's MS Paint (and also into at least later versions of Word).

    I didn't have time to mess with it so I thought I'd post it as a question here for bored people to chew on. I was also hoping he'd join here and maybe chime in with something surprising for us.


    But plinking around a bit and reading responses here, it doesn't seem to be a VB6 issue as much as a "transparent PNG paste in MS Paint" issue to me now. Or (wild supposition here) is it possible there is some WIC clipboard format MS Paint knows about now?


    As far as the comment block above (post #26) goes, I'm not sure Microsoft will update MS Paint. They had planned to get rid of it in more recent updates to Windows 10 and they seem to want people to move to their "Paint 3D" instead. That program does paste CF_PNG, at least the version my quick and dirty demo above copies. Maybe he has Paint and Paint 3D confused or misspoke?

    I see I should change that "Paste as BMP" menu caption to "Paste as Bitmap" though so it isn't lying (see screenshot above).


    Thanks for the input though. Lots of interesting info, but I don't need the capability for anything myself.

    If I did I'd probably just go with CF_PNG and update CustClip.cls to just do copy PNG from byte array, paste PNG to byte array... and keep it independent of any rendering or other code except for a test whether a CF_PNG is on the clipboard. There isn't even much to gain by a class, so a static BAS module would do fine.

  28. #28
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,385

    Re: Copy/Paste Images w/Transparency

    Quote Originally Posted by dilettante View Post
    Well, I brought this up because this guy was hinting to me that there is at least one solution in VB6 to place a PNG from file bytes onto the clipboard, then paste it into Windows 10's MS Paint (and also into at least later versions of Word).
    Well if it's a puzzle, it's a tricky one. Maybe there is a secret clipboard format, but I'd be surprised because I don't even see any tools for handling transparency in paint (like no way to set an alpha channel, or draw in a transparent "colour" or anything).

    I found this interesting tidbit on StackOverflow re: transparency in the WinXP version of paint though:

    "The Windows XP version of MS Paint supports transparency. And users of Windows 7 Professional, Enterprise, or Ultimate can use the XP Mode feature of Windows 7 to run Windows XP in a window, run the old Paint there, and set the transparency of an image file"

    So we don't have XP mode anymore, but maybe there's some trick to get Win10 Paint to behave like XP Paint?

    Quote Originally Posted by dilettante View Post
    As far as the comment block above (post #26) goes, I'm not sure Microsoft will update MS Paint. They had planned to get rid of it in more recent updates to Windows 10 and they seem to want people to move to their "Paint 3D" instead. That program does paste CF_PNG, at least the version my quick and dirty demo above copies.
    I don't expect we'll convince MS to do much of anything either.

    Quote Originally Posted by dilettante View Post
    Maybe he has Paint and Paint 3D confused or misspoke?
    That's a possibility...there is also an "Edit with Paint 3D" button in vanilla Paint, so maybe that's a piece of the puzzle.

  29. #29

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,633

    Re: Copy/Paste Images w/Transparency

    Side jaunt:

    I stripped down the "copy as bitmap" logic since converting "to BMP format" in WIA shouldn't be necessary because I am using Clipboard.SetData with a StdPicture:

    Code:
    Private Sub mnuEditCopy_Click()
        Dim F As Integer
        Dim Buffer() As Byte
    
        F = FreeFile(0)
        Open "sample.png" For Binary Access Read As #F
        ReDim Buffer(LOF(F) - 1)
        Get #F, , Buffer
        Close #F
    
        With CustClip
            .OpenClipboard
            .Clear
            .SetData Buffer, cfPng
            .CloseClipboard
            With New WIA.Vector
                .BinaryData = Buffer
                Clipboard.SetData .Picture, vbCFBitmap
            End With
        End With
    
        mnuEditCopy.Enabled = False
        mnuPopupeditPastebmp.Enabled = True
        mnuPopupeditPastepng.Enabled = True
    End Sub
    For whatever reason it uses a white backdrop to flatten the image onto now instead of black:

    Name:  sshot1.png
Views: 50
Size:  7.8 KB

  30. #30
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,579

    Re: Copy/Paste Images w/Transparency

    The explicit RangeNumber parameter of 0 in FreeFile(0) is something new to me -- always glad to read MSDN about quirky legacy features of VB6 I'll never use :-))

    Btw, the past in "Past as PNG" is surely a typo.

    cheers,
    </wqw>

  31. #31

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,633

    Re: Copy/Paste Images w/Transparency

    Wow, what a typo. I missed the forest for the trees or something there.

    Also:

    The optional rangenumber argument is a Variant that specifies the range from which the next free file number is to be returned. Specify a 0 (default) to return a file number in the range 1 – 255, inclusive. Specify a 1 to return a file number in the range 256 – 511.
    file number

    Number used in the Open statement to open a file. Use file numbers in the range 1–255, inclusive, for files not accessible to other applications. Use file numbers in the range 256–511 for files accessible from other applications.
    Not everyone seems to have the MSDN CDs any more.
    Last edited by dilettante; Yesterday at 07:58 PM.

  32. #32
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,801

    Re: Copy/Paste Images w/Transparency

    file number

    Number used in the Open statement to open a file. Use file numbers in the range 1–255, inclusive, for files not accessible to other applications. Use file numbers in the range 256–511 for files accessible from other applications.
    I just saw that the other day, possibly for the first time. I'm hoping that's just a soft recommendation because I've never used the argument in FreeFile, and I'm opening files all the time that are also used by other applications. I can't even envision what that means, other than an organizational scheme.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width