Results 1 to 24 of 24

Thread: [VB6] Encode binary data to bitmap for use in a PictureBox

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,651

    [VB6] Encode binary data to bitmap for use in a PictureBox

    If you've ever tried to include more than a very small amount of binary data on a Form or UserControl without loading the data from an external file, you certainly noticed it's extremely difficult to accomplish. There's limits to how much data can be stored in string constants, trying to store in controls like a ListBox as the ListItems runs into huge problems when it's more than a tiny file... and just all sorts complications with various ways.

    But thanks to an idea by LaVolpe and implementing code from The_trick, there's a simple and effective way to get binary data onto your form. It's possible to encode binary data as a bitmap, and set that bitmap as the Picture in a standard PictureBox.

    With a custom tool developed around The_trick's code I used this method in my ucShellBrowse UserControl to store icons for menus and buttons right in the control itself, having previously relied on a bunch of images in a res file everyone had to painstakingly merge or recreate for their own projects, and attached my tool to the 2nd post. Since then there's been so many downloads of it I was convinced this should get its own CodeBank entry for all those who might not see it. So I created the Decoder example to go with it, and here we are:

    Name:  bmpenc.jpg
Views: 1626
Size:  42.5 KB
    There's two projects in this sample. An Encoder demo, and a Decoder demo. The demo includes a preprocessed sound-- the Encode directory contains TestSound.wav, and the Decode project includes the processed bitmap, Enc-TestSound.wav.bmp, set as a Picture and ready to be decoded. It's decoded to memory and played from there, no need for writing it back to disk yet. You can use the Encoder for other files, and the Decoder also has a function to write it back to file. There's a sample icon that includes the original and processed, but it's not set to a PictureBox yet. Note that the .bmp files are intermediates; once you've set it as a picture on your form, they're no longer needed- you don't need to include them with the project, that would defeat the whole point Also the demos show the PictureBox controls to provide visual feedback, you can set Visible = False in regular projects, being invisible has no effect on decoding.

    To test it on your own file, enter the directory and name in the Encode demo, and click process. That generates the bitmap. Then in the Decode demo, in the IDE set it as the Picture for the PictureBox on the bottom. Then click Decode to file- a Save dialog will come up, and you save it as its original type. That file will come out identical to the input file you started with.



    This project has no external dependencies. It should run on any version of Windows.
    Thanks again to LaVolpe and The_trick. The_trick wrote the central encode/decode function, all I did was build a demo around it.
    Attached Files Attached Files
    Last edited by fafalone; Jan 21st, 2021 at 09:17 PM. Reason: grammar be is difficultest

  2. #2
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    673

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    If the encryption function is added, it is a picture hiding device

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,651

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    Sure you could use it that way. It stores arbitrary binary data... plain text, complex formats, encrypted data, all the same.

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    Or just store the BLOB as a custom resource and be all done.

  5. #5
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    Why not to store it in a property of an UserControl? The Propertybag object can handle binary data.

  6. #6
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    so, it would be possible to store a png picture, and at runtime render those png pictures into the picture itself using gdialphablend.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,651

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    @dilettante - That would require taking the .res file along with it. That's a fine approach for a regular project, but for distributing an uncompiled UserControl like I use this for, it adds the .res dependency, and then any project you're adding it to, you'd either need to start with the UC from the control, or copy the custom resource into the projects.

    @Eduardo- Wouldn't you have to manually edit the file containing the propertybag defaults to get it to always be available? I tried a whole bunch of ways to store it as properties before settling on this.


    @baka - This gives you a byte array containing the file, so yes you can do whatever you would normally do with a png file.

  8. #8
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    u can create a usercontrol, a picturebox, where u "load" png using API. store the converted 32-bit into the picturebox
    and when u run, the picturebox read the bitmap into a memory hdc and render using gdialphablend into the picturebox or even into the parent hwnd.

    so this method could be used for that.

  9. #9
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    This is my approach.
    Attached Files Attached Files

  10. #10
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    506

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    Quote Originally Posted by Eduardo- View Post
    This is my approach.
    in your example it only works if there is only one usercontrol in the others the data is deleted.
    you could make it multi files

    a greeting

  11. #11
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    Quote Originally Posted by yokesee View Post
    in your example it only works if there is only one usercontrol in the others the data is deleted.
    you could make it multi files

    a greeting
    I just tested with two FileStorage controls on the same form, having loaded two different files, and it worked perfectly.

    Code:
    Private Sub Command2_Click()
        FileStorage1.SaveFile "D:\" & FileStorage1.FileName
        FileStorage2.SaveFile "D:\" & FileStorage2.FileName
    End Sub

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,651

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    The FileStorage control is saving the file in Form1.frx, making it persist with the project. And then the data is associated with the control, so it can't be removed, adding several more files as dependencies.

    The scenario I used my method for, I needed the data to be saved in the UserControl.ctx, to persist with the UserControl itself, to be available to every project the UC was loaded in, since the data is used by the control itself, in my case to display images (with unsupported transparency, and displayed by API menus and not on the control); and didn't want dependencies for other controls/files.
    Last edited by fafalone; Jan 26th, 2021 at 07:34 PM.

  13. #13
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    506

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    Quote Originally Posted by Eduardo- View Post
    I just tested with two FileStorage controls on the same form, having loaded two different files, and it worked perfectly.

    Code:
    Private Sub Command2_Click()
        FileStorage1.SaveFile "D:\" & FileStorage1.FileName
        FileStorage2.SaveFile "D:\" & FileStorage2.FileName
    End Sub
    the problem was that it did not save the project before compiling and the control data was lost.

    a greeting

  14. #14
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    Quote Originally Posted by yokesee View Post
    the problem was that it did not save the project before compiling and the control data was lost.

    a greeting
    It seems to be a VB6 bug that do not fire WriteProperties (after a PropertyChanged was issued) before compiling in the UserControl when that UserControl is in the same project. I was not aware of that. Another workaround is to close the form before compiling.

  15. #15
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    Quote Originally Posted by fafalone View Post
    The FileStorage control is saving the file in Form1.frx, making it persist with the project. And then the data is associated with the control, so it can't be removed, adding several more files as dependencies.

    The scenario I used my method for, I needed the data to be saved in the UserControl.ctx, to persist with the UserControl itself, to be available to every project the UC was loaded in, since the data is used by the control itself, in my case to display images (with unsupported transparency, and displayed by API menus and not on the control); and didn't want dependencies for other controls/files.
    Ah OK. I misunderstood your goal since in your sample the data was saved on the *.frx file.

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,651

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    The important part is not having any additional dependencies; just the object that uses the data having it within its own file.

    Your method or something like dilettante suggested is definitely the easier option if you're not operating under that limit. I admit it's a rather narrow range of scenarios this one is useful for, but for the specific situation of wanting a Form or UserControl with the data embedded within it and without an additional control, this seems like the most practical way for larger files.

  17. #17
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    Quote Originally Posted by fafalone View Post
    The important part is not having any additional dependencies; just the object that uses the data having it within its own file.

    Your method or something like dilettante suggested is definitely the easier option if you're not operating under that limit. I admit it's a rather narrow range of scenarios this one is useful for, but for the specific situation of wanting a Form or UserControl with the data embedded within it and without an additional control, this seems like the most practical way for larger files.
    You can also store the file in just the *.ctl file itself as code. It would take perhaps many lines (or even more than one procedure), but it is possible.

  18. #18

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,651

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    There's limits on that:

    The amount of code that can be loaded into a form, class, or standard module is limited to 65,534 lines. A single line of code can consist of up to 1023 bytes. Up to 256 blank spaces can precede the actual text on a single line, and no more than twenty-four line-continuation characters ( _) can be included in a single logical line.
    The data segment (that is, the data defined in the Declarations section) of the VBA module of any form or module in Visual Basic can be up to 64K. This data segment contains the following data:
    Local variables declared with Static.
    Module-level variables other than arrays and variable-length strings.
    4 bytes for each module-level array and variable-length string.
    Plus, when it's more than a very small amount of data, having thousands or tens of thousands of lines of binary data is not ideal for editing.
    Last edited by fafalone; Jan 27th, 2021 at 02:43 AM.

  19. #19
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    When you say "A single line of code" is a line of text or a line of code (that can be splitted on several lines of text using ' _')?

    Anyway, my guess is that with that method can be stored files like up to 1MB or more, but I would have to test.
    (unfortunately I'm busy right now, so may be on another time).

    Quote Originally Posted by fafalone View Post
    Plus, when it's more than a very small amount of data, having thousands or tens of thousands of lines of binary data is not ideal for editing.
    Yep. It will appear full of "garbage".

  20. #20

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,651

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    You can't exceed 65k physical lines.

    So theoretically you can store (65k - actual code lines) * 1,024 bytes in blocks of strings up to 15kb each (it says 24 concatenations per line, but using a string constant to the full 1023 bytes, I get 'statement too complex' trying to add additional lines); but the practical issues of having to scroll around that many lines of garbage, all the concatenations... it's possible but I still think the bitmaps are a better method once you get past a few kb. Plus storing as strings you'd have to re-encode anyway, storing directly as bytes it'd take up even more space to write it as code....
    Last edited by fafalone; Jan 27th, 2021 at 04:23 AM.

  21. #21
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    Quote Originally Posted by fafalone View Post
    You can't exceed 65k physical lines.
    That is for a procedure or a module?

    Quote Originally Posted by fafalone View Post
    So theoretically you can store (65k - actual code lines) * 1,024 bytes in blocks of strings up to 15kb each (it says 24 concatenations per line, but using a string constant to the full 1023 bytes, I get 'statement too complex' trying to add additional lines); but the practical issues of having to scroll around that many lines of garbage, all the concatenations... it's possible but I still think the bitmaps are a better method once you get past a few kb. Plus storing as strings you'd have to re-encode anyway, storing directly as bytes it'd take up even more space to write it as code....
    Then the result is several megabytes.

  22. #22

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,651

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    The 65k total physical limit is for the module.

    Yep you could store a few MB if really determined to do it that way, but I like the bitmaps for more than a few kb.

  23. #23
    Hyperactive Member
    Join Date
    Dec 2008
    Location
    Argentina
    Posts
    439

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    Many years ago I had done something similar, but I saved the image as png with gdip +, I did it to compress data without dependency, just for fun
    leandroascierto.com Visual Basic 6 projects

  24. #24
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: [VB6] Encode binary data to bitmap for use in a PictureBox

    Quote Originally Posted by fafalone View Post
    The 65k total physical limit is for the module.

    Yep you could store a few MB if really determined to do it that way, but I like the bitmaps for more than a few kb.
    I understand, it seems a good balance between avoiding dependencies and avoiding "garbage" code.

Posting Permissions

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



Click Here to Expand Forum to Full Width