Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1463

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

$
0
0
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: 32
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 Images
 
Attached Files

Viewing all articles
Browse latest Browse all 1463

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>