How to show button images at design-time?

hrabia

Centurion
Joined
May 21, 2003
Messages
107
Location
Hamburg, Germany
I've some embedded images in my app. I load them dynamically at run-time:

Code:
Assembly assembly = Assembly.GetExecutingAssembly();
string assemblyName = Assembly.GetExecutingAssembly().GetName().Name.ToString();

Stream stream = assembly.GetManifestResourceStream(assemblyName + ".Images.ARROWDN.BMP");
this.butBottom.Image = Image.FromStream(stream);

I've no idea how I can do this at design-time. Help :) ?

Adam
 
What do you want to do with them at design time? If you use an image as a property on a control then the code to do this is generated for you. If not, then I don't understand what could be done for you at design time. Perhaps an ImageList component would make your life easier. This will embed the images for you and you can pull them up at run-time by index with a single line of code.

If I am making an app with a lot of embedded images, I always make a function that accepts the image's pre-compile filename and loads the image for me.
[Vb]
Public Shared Function GetEmbeddedImage(Name As String) As Image\
Dim Assm As Assembly = Assembly.GetExecutingAssembly()
Dim AssmName As String = Assm.GetName().Name

Dim Stream As Stream = Assm.GetManifestResourceStream(AssmName & "." & Name);
Return Image.FromStream(Stream)
End Sub
[/CODE]
 
You need to build a user control the inherits the control that you want this functionallity. Paint the image to the control in the user control's Overrides OnPaint sub. Although I've never tried this with embedded images I don't think there should be any problems.
 
marble_eater - it's simple, I'd like to see my button images in the designer. At the moment my buttons are all blank. I can reference an image using path to a file to see the image in designer, but how to do this with an embedded image (withaut referencing the file)?

Thanks DiverDan, that was my idea also, but how to change an image for different buttons?
 
Last edited:
I wish I had more time to knock up a sample for you as it's not that complicated. Not getting very deep into details, I think you'll only need to over ride the OnPaint sub and paint the image and text into the button. Also, you'll be able to customize the button borders, gradients, mouse enter & leave effects and a bunch more in only a couple of hours work. I have a few examples of my inherited button controls. If you're interested drop me a PM and I'll email some to you.
 
hrabia, as what marble_eater said, use an imagelist would make ur life easier. Just add all the images into the image list, then, u could call out the image in the design time.

So, if you are calling an embedded image, you could call by using the path like
"Images/abc.jpg" if you are putting in under the Images folder. And if you are putting it in the Project root folder, you could just call like "abc.jpg".

I used to do this few months ago. If not mistaken, this is the way i used.

Hope this help
 
Thanks georgepatotk. The idea with imagelist works very well but...

...is it really the one and only way "to see" images at design- and run-time without big effort?

Hmm... thanks anyway to you all!
 
I've got another thought hrabia. If the embedded images are only used for the buttons, then you can easily embed them in the inherited button control instead of the project. This would help speed up the painting, increase flexibility and reduce complexity.
 
just do it inside the constructor of the form, like the highlighted ( BOLD ) part below ...
Code:
#Region " Windows Form Designer generated code "
	Public Sub New()
		MyBase.New()
		'This call is required by the Windows Form Designer.
		InitializeComponent()
		'Add any initialization after the InitializeComponent() call
	End Sub
	'Form overrides dispose to clean up the component list.
	Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
		If disposing Then
			If Not (components Is Nothing) Then
				components.Dispose()
			End If
		End If
		MyBase.Dispose(disposing)
	End Sub
	'Required by the Windows Form Designer
	Private components As System.ComponentModel.IContainer
	'NOTE: The following procedure is required by the Windows Form Designer
	'It can be modified using the Windows Form Designer.  
	'Do not modify it using the code editor.
	Friend WithEvents Button1 As System.Windows.Forms.Button
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
		Me.Button1 = New System.Windows.Forms.Button
		Me.SuspendLayout()
		'
		'Button1
		'
		Me.Button1.Location = New System.Drawing.Point(16, 16)
		Me.Button1.Name = "Button1"
		Me.Button1.TabIndex = 0
		Me.Button1.Text = "Button1"

[b]		'/// like this ...
		Dim btnimg As Image = Image.FromStream(Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(Application.ProductName & ".Picture1.jpg"))[/b]
[b]		'/// SET IMAGE ON BUTTON ....[/b][b]
		Me.Button1.Image = btnimg[/b]

		'
		'Form1
		'
		Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
		Me.ClientSize = New System.Drawing.Size(432, 230)
		Me.Controls.Add(Me.Button1)
		Me.Name = "Form1"
		Me.Text = "Form1"
		Me.ResumeLayout(False)
	End Sub
#End Region
[size=2]
[/size]
 
I should apologize. I kinda... missed the title of the thread and I thought that you were simply asking about embedded images. Sorry for the not very useful answer.
 
Back
Top