Re: PictureBox and images

Tech-Archive recommends: Fix windows errors by optimizing your registry




"jack" <jack@xxxxxxxxxxx> wrote in message
news:O3WyYxhNFHA.2520@xxxxxxxxxxxxxxxxxxxxxxx
> Hi,
>
> I have 30 different jpeg images that I display in a picturebox based on
user
> input. I want to include these images inside the exe so the user does not
> have access to them. Is there a way to do this?


There are several ways. Ideally, you'd add all the JPEGs to a resource
file, which gets compiled into your EXE. Unfortunately, VB's resource
functions do not directly support JPEG, only bitmaps, icons, and cursors. To
keep the files as JPEG, you'd need to extract the resource and write it to a
temp file and then load that file. You could convert the JPEGs to BMPs, but
since BMPs are extremely larger than JPEGs, your EXE would be considerably
larger. This would have no impact whatsoever on performance though. For a
bitmap, the code would be something like this:

Set Picture1.Picture = LoadResPicture(Index, vbResBitmap)

You could make the BMPs significantly smaller by saving them as .RLE files.
An RLE file is really a compressed BMP, and VB fully supports these.
Depending on the image, an RLE file can be considerably smaller than the BMP
file. The problem there is that there aren't too many programs that support
saving a BMP as an RLE file. I don't even remember what I used to use for
this.

If you wanted to keep all the images as JPEGs in your resource file, you'd
have quite a bit more work. As stated, you'd have to extract the binary data
from the resource, write this to a file, and then LoadPicture that file. You
can use VB's LoadResData function to get the binary data. Unfortunately,
this function is limited to 64K, so all your JPEGs would have to be 64K or
smaller OR you'd have to get the binary data from multiple resources and
append it all together to make one file. You should be able to use Win32API
resource functions instead of LoadResData to overcome this, but I'm not
really familiar with those.

If all the JPEGs are the same width and height, you could add them to an
ImageList control. This would also cause them to be compiled into the EXE.
You would then assign the image to the PictureBox as follows:

Set Picture1.Picture = ImageList1.ListImages(IndexOrKey).Picture

This would also require that you distribute the Microsoft Windows Common
Controls OCX, but maybe you're using that anyway. Also, since an ImageList
does not allows its images to be of different sizes, they'd all have to be
the same size (in terms of width and height).

Finally, you could just have a control array of 30 Image controls and assign
each JPEG at design-time. Again, this would cause the images to get
compiled into your EXE. At runtime, assign the image from the appropriate
Image control to the PictureBox. This *would* affect performance however,
mostly during the loading of the form.

--
Mike
Microsoft MVP Visual Basic


.


Quantcast