I have made a user control that acts like a regular image control as I not only needed transparency I also need a handle because I use APIs to do things to the image that I can't do this using a regular image control. The user control shows a transparent image which I can change it's appearance using APIs but I was wondering if I can have two images in one control so all I would have to do is toggles between the two images. Both images would have to be transparent.
Can I have two images in one control and how would I implement a second image in a control?
Last edited by jmsrickland; Mar 24th, 2014 at 11:40 PM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Your example project is interesting but how would I add the images at design time like you would for a regular image control. I making a game where there are going to be about 100 controls on the Form each with a different image and each image will have an alternate image (one is shown as normal and the other shown as highlighted when it is clicked on).
What I was thinking would be that the UC image control would have two picture properties so when the control is drawn on the Form (no pictures at this time) then in the Properties box I would click on Picture1 and select the image from storage then I would click on Picture2 and select that image from storage. When the control is finished being created the default image would show (Picture1) then during runtime if the player clicked on the image or the image was affected by the other player it would switch over to Picture2 which would show the same image but highlighted then after certain events take place the image would then become the un-highlighted again(Picture1) etc.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
You could alternate those two images using a PushButton control from codejock. It allows you to load even five different pics in design-time (via its property page) and also it supports the png format (I would say the transparency is a must in all games). Because you want to display a clickable icon you have just to set the appearance button as flat or ultraflat and also the flatstyle property as true.
At least you could consider this an alternative.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Ok, I think I see what you're getting at and in general it might be a cleaner way to go.
At least the way PropertyBag resources are managed, "Picture" type properties are stored in the form loaded from disk rather than bulky BMPs! This spares you the need to do any fiddling with image processing API calls and such just to manage your two images.
As a result this should yield about the same size EXE as my other example.
Again, the archive is large but only because of my images. This second example runs the same way as the first one.
To use this new version of the UserControl at design-time:
Create an instance on your Form.
In the Properties panel set appropriate values for MaskColor, Picture1, and Picture2.
I also changed the name of the image selection property you'd use at run time (just to let you know).
This might be closer to what you were hoping to accomplish.
I was thinking why can you not just load a bitmap image (doesn't need to be a transparent gif) using the Picture1/Picture2 properties into a StdPicture variable then depending on which image you want to show get the image from the variable and copy it to the Picture property of the user control and apply TransparentBlt to it.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
I thought you wanted to do this at design time. If so, you don't want to use LoadPicture at all.
I'm not sure what TransparentBlt buys you here. Even if you wanted separate mask colors for the two images all you need are two maskcolor properties and flip/flop those as you switch images. And BTW, the example above does not require transparent GIFs, because it uses the MaskColor mechanism.
It sounds like you want to make this harder than it needs to be. But if you think that gives you spiffier results then go for it. The only reason I can see to add complexity would be to use alpha blending for antialiased transparency and translucency (drop shadows?).
No, dil, I think you are reading my last post wrong or I am not saying it correctly. I do want to put the images in the control at design time just like I explained in post #3. What I am saying in post #7 is basically the same thing (even if it doesn't sound like it is) but instead of using transparent gif images you could just load a regular non-transparent .BMP and the TransparentBlt would take care of the transparency. I wasn't aware that your code does the same thing using the MaskColor. So, after reading your post #8 I believe we are on the same page. Let me try your second example.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
It should work with BMP images (or even JPEGs if they are "clean enough" to use a simple mask/transparent color). The downside with BMPs is that they make your program huge since they lack any compression.
I was thinking why can you not just load a bitmap image (doesn't need to be a transparent gif) using the Picture1/Picture2 properties into a StdPicture variable then depending on which image you want to show get the image from the variable and copy it to the Picture property of the user control and apply TransparentBlt to it.
An example which does that (providing and handling true 32Bit-StdPictures with an AlphaChannel), is already in the Codebank.
And it doesn't even use the old TransparentBlt-GDI-Call, but the AlphaBlend-Call instead (TransparentBlt will give you non-antialiased Output with ragged-edges, whilst AlphaBlend will give you smooth edges, when the Resource you were using as the Base-Image came with an Alpha-Channel).
It would be relatively easy to adapt the Demo above from "4-state-Buttons" to "two-state-Sprites" -
and it doesn't use any 3rd-party Controls.
As for conversions of 100 BMP-Resource-Images to "two-state PNG-Image-Resources",
I could post a small 20 lines-snippet which does that automatically for you (in a loop) -
(in case that's a problem at all - maybe your original ImageResource-Sprites already exist
as a *.png somewhere - and your Bitmaps were derived from them)...
The Web is full of nice PNG- (or SVG-) Sprites - why bother with Bitmaps at all?
Your example from post #6 is exactly what I need. Thanks a million
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
If you wanted to convert your BMPs to compressed GIFs to save space in your compiled program... IrfanView can be a useful tool, and probably one you ought to have on hand anyway.
It could batch-convert folders of such images for you. You could save even more space by having it decrease the color depth before GIF conversion as well (i.e. if your BMPs only need 16 colors then decrease depth to 16-colors). However if each image has more than 16 unique colors then that won't buy you anything but distortion.
This isn't a drawing tool but great for doing lots of quick image transformations.