images and saving time/space

  • Thread starter Thread starter Michael Turner
  • Start date Start date
M

Michael Turner

Hi Guys

I have several images that will be used over and over again on different
forms in the same place, I have decided to create a control to save
space/time in development, I have create a control which inherits the panel
and added three panel control which host the 3 background images ( one to
the left the other to the right and one to fill the gap) now with this
control textboxes will be placed on these controls once they are added to
the intended form however when you get to this stage they seem to dissapear
when I move them to the front the repear they come to the from but have
inherited the background colour of the main form and not of my panels in my
control any ideas on how to get round this.


Thanks Mike.
 
Hi Michael,

I've done this recently with my new free program (including source)
WTR - Web The Ripper 2. I'll highlight what I done before giving you the
link.

*Embed the image as a resource but use a PNG file if you want it alpha
blended into the background colour. I chose 15% alpha.
*In the Paint event of the panel control in the base form, do the
following,

~~~

Static pBmpBackdrop As Bitmap = getBackdrop(panStep.Width, panStep.Height,
panStep.BackColor)
Call e.Graphics.DrawImageUnscaled(pBmpBackdrop, 0, 0)

~~~

Private Function getBackdrop(ByVal iWidth As Integer, ByVal iHeight As
Integer, ByVal iBackColour As Color) As Bitmap
Dim pBmpAlpha As New Bitmap(iWidth, iHeight)
Dim pImgBack As Image =
getImageFromResource("fullnamespace.backdrop.png")
Dim pGraAlpha As Graphics = Graphics.FromImage(pBmpAlpha)
Call pGraAlpha.Clear(iBackColour)
Call pGraAlpha.DrawImage(pImgBack, New Rectangle(CInt((pBmpAlpha.Width /
2) - (pImgBack.Width / 2)), CInt((pBmpAlpha.Height / 2) - (pImgBack.Height /
2)), pImgBack.Width, pImgBack.Height), 0, 0, pImgBack.Width,
pImgBack.Height, GraphicsUnit.Pixel)
Call pGraAlpha.Dispose()
Return (pBmpAlpha)
End Function

Public Shared Function getImageFromResource(ByVal iKey As String) As Image
Dim pAssAssembly As [Assembly] = [Assembly].GetExecutingAssembly
Dim pStmInput As Stream = pAssAssembly.GetManifestResourceStream(iKey)
Dim pImgImage As Image = Image.FromStream(pStmInput, False)
Call pStmInput.Close()
Return (pImgImage)
End Function

~~~

Then compile your application and run it. You should see a picture
painted into the middle of the panel. One thing you will also want to do is
make the back colour of any labels, check boxes and radio buttons
Transparent. But *dont* use the web transparent, this had strange effects
for me. Instead you can call the following method from the derived class
(put it in the base class) upon loading it.

Protected Sub makeControlsTransparent()
Dim pCtlCurControl As Control
For Each pCtlCurControl In panStep.Controls
If (TypeOf (pCtlCurControl) Is Label) Then
CType(pCtlCurControl, Label).BackColor =
System.Drawing.Color.Transparent
ElseIf (TypeOf (pCtlCurControl) Is CheckBox) Then
CType(pCtlCurControl, CheckBox).BackColor =
System.Drawing.Color.Transparent
ElseIf (TypeOf (pCtlCurControl) Is RadioButton) Then
CType(pCtlCurControl, RadioButton).BackColor =
System.Drawing.Color.Transparent
End If
Next
End Sub

For a full working application as an example with very readable code,
goto my home page

http://www.members.lycos.co.uk/nickpatemanpwp/
^Click "My Own Software" > "WTR - Web The Ripper 2".

Nick.
 
Cheers, after a bit of playing to get the result I wanted I have finally got
exactly what I was after

Thanks again

Mike.


Nak said:
Hi Michael,

I've done this recently with my new free program (including source)
WTR - Web The Ripper 2. I'll highlight what I done before giving you the
link.

*Embed the image as a resource but use a PNG file if you want it alpha
blended into the background colour. I chose 15% alpha.
*In the Paint event of the panel control in the base form, do the
following,

~~~

Static pBmpBackdrop As Bitmap = getBackdrop(panStep.Width, panStep.Height,
panStep.BackColor)
Call e.Graphics.DrawImageUnscaled(pBmpBackdrop, 0, 0)

~~~

Private Function getBackdrop(ByVal iWidth As Integer, ByVal iHeight As
Integer, ByVal iBackColour As Color) As Bitmap
Dim pBmpAlpha As New Bitmap(iWidth, iHeight)
Dim pImgBack As Image =
getImageFromResource("fullnamespace.backdrop.png")
Dim pGraAlpha As Graphics = Graphics.FromImage(pBmpAlpha)
Call pGraAlpha.Clear(iBackColour)
Call pGraAlpha.DrawImage(pImgBack, New Rectangle(CInt((pBmpAlpha.Width
/ 2) - (pImgBack.Width / 2)), CInt((pBmpAlpha.Height / 2) -
(pImgBack.Height / 2)), pImgBack.Width, pImgBack.Height), 0, 0,
pImgBack.Width, pImgBack.Height, GraphicsUnit.Pixel)
Call pGraAlpha.Dispose()
Return (pBmpAlpha)
End Function

Public Shared Function getImageFromResource(ByVal iKey As String) As Image
Dim pAssAssembly As [Assembly] = [Assembly].GetExecutingAssembly
Dim pStmInput As Stream = pAssAssembly.GetManifestResourceStream(iKey)
Dim pImgImage As Image = Image.FromStream(pStmInput, False)
Call pStmInput.Close()
Return (pImgImage)
End Function

~~~

Then compile your application and run it. You should see a picture
painted into the middle of the panel. One thing you will also want to do
is make the back colour of any labels, check boxes and radio buttons
Transparent. But *dont* use the web transparent, this had strange effects
for me. Instead you can call the following method from the derived class
(put it in the base class) upon loading it.

Protected Sub makeControlsTransparent()
Dim pCtlCurControl As Control
For Each pCtlCurControl In panStep.Controls
If (TypeOf (pCtlCurControl) Is Label) Then
CType(pCtlCurControl, Label).BackColor =
System.Drawing.Color.Transparent
ElseIf (TypeOf (pCtlCurControl) Is CheckBox) Then
CType(pCtlCurControl, CheckBox).BackColor =
System.Drawing.Color.Transparent
ElseIf (TypeOf (pCtlCurControl) Is RadioButton) Then
CType(pCtlCurControl, RadioButton).BackColor =
System.Drawing.Color.Transparent
End If
Next
End Sub

For a full working application as an example with very readable code,
goto my home page

http://www.members.lycos.co.uk/nickpatemanpwp/
^Click "My Own Software" > "WTR - Web The Ripper 2".

Nick.

Michael Turner said:
Hi Guys

I have several images that will be used over and over again on different
forms in the same place, I have decided to create a control to save
space/time in development, I have create a control which inherits the
panel and added three panel control which host the 3 background images
( one to the left the other to the right and one to fill the gap) now
with this control textboxes will be placed on these controls once they
are added to the intended form however when you get to this stage they
seem to dissapear when I move them to the front the repear they come to
the from but have inherited the background colour of the main form and
not of my panels in my control any ideas on how to get round this.


Thanks Mike.
 
Hi there,
Cheers, after a bit of playing to get the result I wanted I have finally
got exactly what I was after

Cool, I love doing that to panel controls now, it makes them look so
much nicer!

Nick.
 
Nak said:
Protected Sub makeControlsTransparent()
Dim pCtlCurControl As Control
For Each pCtlCurControl In panStep.Controls
If (TypeOf (pCtlCurControl) Is Label) Then
CType(pCtlCurControl, Label).BackColor =
System.Drawing.Color.Transparent
ElseIf (TypeOf (pCtlCurControl) Is CheckBox) Then
CType(pCtlCurControl, CheckBox).BackColor =
System.Drawing.Color.Transparent
ElseIf (TypeOf (pCtlCurControl) Is RadioButton) Then
CType(pCtlCurControl, RadioButton).BackColor =
System.Drawing.Color.Transparent
End If
Next
End Sub

FWIW compare with:

Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is Label _
OrElse TypeOf ctl Is CheckBox _
OrElse TypeOf ctl Is RadioButton _
Then
ctl.BackColor = Color.Transparent
End If
Next

....Works in all standard Windows forms! <g>

LFS
 
Hi Larry,
FWIW compare with:

Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is Label _
OrElse TypeOf ctl Is CheckBox _
OrElse TypeOf ctl Is RadioButton _
Then
ctl.BackColor = Color.Transparent
End If
Next

LOL, thats good point, I was written in a testing phase honest! ;-)

Nick.
 
To continue the discussion, here is another way to do that same task. While
Larry's routine would catch all contols, I went on the assumption that you
know which controls you want included in the group:

\\\
For Each ctr As Control In New Controls() {Label1, Checkbox1, RadioButton1}
ctr.BackColor = Color.Transparent
Next
///

One optimization would be to save the Control array at the module level to
avoid creating a new array each time a new Control is selected. If you have
other events that would need the array, then it would be advantageous to
keep such an array alive for the entire lifetime of the form....

:-))))

Cor
 
Hi Cor,
To continue the discussion, here is another way to do that same task.
While Larry's routine would catch all contols, I went on the assumption
that you know which controls you want included in the group: \\\
For Each ctr As Control In New Controls() {Label1, Checkbox1,
RadioButton1}
ctr.BackColor = Color.Transparent
Next
///

Tis all dynamic, it checks the form for controls of a specific type then
sets the back colour to transparent. It's all done in a base for and the
derived form calls the protected even on Load. This means I can add
controls at my hearts content and they will always look as desired.
One optimization would be to save the Control array at the module level to
avoid creating a new array each time a new Control is selected. If you
have
other events that would need the array, then it would be advantageous to
keep such an array alive for the entire lifetime of the form....

It's only called once anyay on Load. But I see what your getting at, I
just wanted a quick way to get all controls on my "wizard panel" derived
forms to be able to show the graphics drawn onto the panel. And this suited
the need perfectly. I would like to do type checking in a select block, but
I don't think this is possible is it? :-\

Anyway, cheers for the ideas Mr Cor, much appreciated.

Nick.
 
Larry,

I hope you understand it was just for fun, I told in that thread that I was
always doing that way (There was no discussion about that between us), and
than I saw you did not do it here.

And there was nothing bad with showing that too Nick.

:-)

Cor
 
Some might be surprised how Cor suddenly got very fluent in English....


Some have to do it with there English, some with the quality of what they
say.



Cor
 
Cor Ligthert said:
I hope you understand it was just for fun,

What is so fun about offering a seemingly legitimate suggestion
that does not work?

LFS
 
Cor Ligthert said:
I forgot, did you try that test I made?

No, that test was not going to be very informative.

What needs to be done is to bind an event that would not
get optimized out of the application (there was no code in
your event), and then unbind it.

After several iterations, the amount of time needed to
bind and unbind the event could be approximated.

That time has to be compared to calling the same event for
some number of calls, on a similar object that uses the
Handles keyword for the same event.

The figures obtained from that could be used to determine
approximately how many calls could be made to that event,
in the time it takes to bind and unbind the event.

I didn't have time to code that up today, maybe later
at the weekend....

LFS
 
Hi,
What is so fun about offering a seemingly legitimate suggestion
that does not work?

Well whether or works or not is a completely different story, because I
wasn't actually going to try it anyway. But isn't the group about sharing
and pooling of knowledge? My solution was made under the presumption that
you *didnt* know what controls you had on the form, that was why I was not
resorting to anything hard-coded.

Anyway, easy tiger! ;-)

Nick.
 
I now see, that you are writing a lot of words, from which I ask myself if
you do understand them yourself.

The test shows exactly how much time it takes adding and removing of an
event, from which you did not say, however with many words gave the
impression that it would consume a lot of effort from the system.
 
I see a typo, which every beginner in VBNet sees directly. I have seen more
people in this newsgroup, who wants with that, give the idea that the
complete sample is a wrong solution. Luckily, it shows to regulars who see
those messages after a while what kind of persons that are.

More persons are telling when they see a typo from somebody else, that the
solution is wrong. However, most of us have done with that kind of people
and qualified them on the level they act.

Somebody, with not only limited knowledge, points on the error with no more
words than needed and is a little bit ashamed that he does it, because it is
a little bit childish.

In making a solution as Nick did, is the first challenge making the solution
from which the first goal is that it works. When needed than it can be
optimized, however in many cases is that useless spending time of the
programmer looking at the 20/80 rule.

By instance have I seen Nick given a sample in past, from which he self
wrote afterwards (when he made a test program, because the problem was more
general discussed and more solutions where made), that his solution was
"sloppy". However, he gave an answer that did work and nobody criticised him
because of his answer.

You see this easy criticizing mostly with beginners, although some never
learn that productivity is important and that gaining a byte is mostly penny
wise and pound-foolish.

Cor
 
Cor Ligthert said:
I see a typo, which every beginner in VBNet sees directly. I have seen more
people in this newsgroup, who wants with that, give the idea that the
complete sample is a wrong solution. Luckily, it shows to regulars who see
those messages after a while what kind of persons that are.

OK, Cor, have your fun, but plan on doing it without me. There are plenty here
that will take my posts at face value, and not try to read their own meanings into
what I write. You post whatever you think will work, and I will do the same....

LFS
 
OK, Cor, have your fun, but plan on doing it without me. There are plenty
here
that will take my posts at face value, and not try to read their own
meanings into
what I write. You post whatever you think will work, and I will do the
same....
Sorry, I don't understand this, can you explain this too me?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top