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.
I used this method in my ucShellBrowse UserControl to store icons for menus and buttons, I included this tool attached to the 2nd post and there were so many downloads of the tool that I wrote around The_trick's code that I wanted to post this as an independent code sample for all the people who might not come across it in that project.
![Name: bmpenc.jpg
Views: 10
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. 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 :)
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.
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.
I used this method in my ucShellBrowse UserControl to store icons for menus and buttons, I included this tool attached to the 2nd post and there were so many downloads of the tool that I wrote around The_trick's code that I wanted to post this as an independent code sample for all the people who might not come across it in that project.
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. 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 :)
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.