Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. [VB.NET 2008] How to put an image in a button

[VB.NET 2008] How to put an image in a button

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpgraphicstutorialquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    steve_9496613
    wrote on last edited by
    #1

    Hi all, I need to pu an image in a button but my application runs in Windows CE so I'm using the Compact Framework and in this context the object Button has no property like Image, BackgroundImage, Bitmap or something similar. Anybody knows how I can do? Thanks to all.

    L S 2 Replies Last reply
    0
    • S steve_9496613

      Hi all, I need to pu an image in a button but my application runs in Windows CE so I'm using the Compact Framework and in this context the object Button has no property like Image, BackgroundImage, Bitmap or something similar. Anybody knows how I can do? Thanks to all.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      steve_9496613 wrote:

      Anybody knows how I can do?

      Try MSDN[^] :)

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

      S L 2 Replies Last reply
      0
      • S steve_9496613

        Hi all, I need to pu an image in a button but my application runs in Windows CE so I'm using the Compact Framework and in this context the object Button has no property like Image, BackgroundImage, Bitmap or something similar. Anybody knows how I can do? Thanks to all.

        S Offline
        S Offline
        s3275049
        wrote on last edited by
        #3

        Hi steve, You can use ImageButton control. An attribute called ImageUrl is where you specify the path to your image. :) Hope that have you. Happy coding! :)

        S 1 Reply Last reply
        0
        • S s3275049

          Hi steve, You can use ImageButton control. An attribute called ImageUrl is where you specify the path to your image. :) Hope that have you. Happy coding! :)

          S Offline
          S Offline
          steve_9496613
          wrote on last edited by
          #4

          Hi s3275049, thank you for your replay but unfortunately MSDN says that the ImageButton class is not supported in the Windows CE platform. :((

          1 Reply Last reply
          0
          • L Lost User

            steve_9496613 wrote:

            Anybody knows how I can do?

            Try MSDN[^] :)

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

            S Offline
            S Offline
            steve_9496613
            wrote on last edited by
            #5

            Well... I can't say that I have understood everything in the MSDN example but I managed to convert it to Visual Studio .NET 2008 and to make it work!!!! Yes, it would be easier something like Button.BkImage("image.bmp") but ... it seems that the Compact Framework is missing many things that I need!!! Once again, Thank you Eddy!! I publish the code of MyImageButton class if it can help someone or anyone has any corrections to suggest.

            Imports System
            Imports System.Drawing
            Imports System.Windows.Forms
            Imports System.Drawing.Imaging

            Public Class MyImageButton
            Inherits Control

            'Private members
            Private image As Image
            Private FirstTime As Boolean = True
            'flag to indicate the pressed state
            Private bPushed As Boolean
            Private m_bmpOffscreen As Bitmap

            Sub New()
            bPushed = False
            'default minimal size
            Me.Size = New Size(21, 21)
            End Sub

            Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            Dim gxOff As Graphics 'Offscreen graphics
            Dim imgRect As Rectangle 'image rectangle
            Dim backBrush As Brush 'brush for filling a backcolor

            'If (m\_bmpOffscreen.Equals(Nothing)) Then   'Bitmap for doublebuffering
            If (FirstTime) Then
              m\_bmpOffscreen = New Bitmap(ClientSize.Width, ClientSize.Height)
            End If
            
            gxOff = Graphics.FromImage(m\_bmpOffscreen)
            
            gxOff.Clear(Me.BackColor)
            
            If (Not bPushed) Then
              backBrush = New SolidBrush(Parent.BackColor)
            Else 'change the background when it's pressed
              backBrush = New SolidBrush(Color.LightGray)
            End If
            
            gxOff.FillRectangle(backBrush, Me.ClientRectangle)
            
            If (Not image.Equals(DBNull.Value)) Then
              'Center the image relativelly to the control
              Dim imageLeft As Int32 = (Me.Width - image.Width) / 2
              Dim imageTop As Int32 = (Me.Height - image.Height) / 2
            
              If (Not bPushed) Then
                imgRect = New Rectangle(imageLeft, imageTop, image.Width, image.Height)
              Else  'The button was pressed
                'Shift the image by one pixel
                imgRect = New Rectangle(imageLeft + 1, imageTop + 1, image.Width, image.Height)
              End If
            
              'Set transparent key
              Dim imageAttr As ImageAttributes = New ImageAttributes()
              imageAttr.SetColorKey(BackgroundImageColor(image), BackgroundImageColor(image))
            
              'Draw image
              gxOff.DrawImage(image, imgRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr)
            End If
            
            If (bPushed) Then
            
            L 1 Reply Last reply
            0
            • S steve_9496613

              Well... I can't say that I have understood everything in the MSDN example but I managed to convert it to Visual Studio .NET 2008 and to make it work!!!! Yes, it would be easier something like Button.BkImage("image.bmp") but ... it seems that the Compact Framework is missing many things that I need!!! Once again, Thank you Eddy!! I publish the code of MyImageButton class if it can help someone or anyone has any corrections to suggest.

              Imports System
              Imports System.Drawing
              Imports System.Windows.Forms
              Imports System.Drawing.Imaging

              Public Class MyImageButton
              Inherits Control

              'Private members
              Private image As Image
              Private FirstTime As Boolean = True
              'flag to indicate the pressed state
              Private bPushed As Boolean
              Private m_bmpOffscreen As Bitmap

              Sub New()
              bPushed = False
              'default minimal size
              Me.Size = New Size(21, 21)
              End Sub

              Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
              Dim gxOff As Graphics 'Offscreen graphics
              Dim imgRect As Rectangle 'image rectangle
              Dim backBrush As Brush 'brush for filling a backcolor

              'If (m\_bmpOffscreen.Equals(Nothing)) Then   'Bitmap for doublebuffering
              If (FirstTime) Then
                m\_bmpOffscreen = New Bitmap(ClientSize.Width, ClientSize.Height)
              End If
              
              gxOff = Graphics.FromImage(m\_bmpOffscreen)
              
              gxOff.Clear(Me.BackColor)
              
              If (Not bPushed) Then
                backBrush = New SolidBrush(Parent.BackColor)
              Else 'change the background when it's pressed
                backBrush = New SolidBrush(Color.LightGray)
              End If
              
              gxOff.FillRectangle(backBrush, Me.ClientRectangle)
              
              If (Not image.Equals(DBNull.Value)) Then
                'Center the image relativelly to the control
                Dim imageLeft As Int32 = (Me.Width - image.Width) / 2
                Dim imageTop As Int32 = (Me.Height - image.Height) / 2
              
                If (Not bPushed) Then
                  imgRect = New Rectangle(imageLeft, imageTop, image.Width, image.Height)
                Else  'The button was pressed
                  'Shift the image by one pixel
                  imgRect = New Rectangle(imageLeft + 1, imageTop + 1, image.Width, image.Height)
                End If
              
                'Set transparent key
                Dim imageAttr As ImageAttributes = New ImageAttributes()
                imageAttr.SetColorKey(BackgroundImageColor(image), BackgroundImageColor(image))
              
                'Draw image
                gxOff.DrawImage(image, imgRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr)
              End If
              
              If (bPushed) Then
              
              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              You're welcome :)

              steve_9496613 wrote:

              I publish the code of MyImageButton class if it can help someone or anyone has any corrections to suggest.

              It might get buried under new messages on the forum here; it'd be more visible if you post it as a "Tip[^]".

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

              1 Reply Last reply
              0
              • L Lost User

                steve_9496613 wrote:

                Anybody knows how I can do?

                Try MSDN[^] :)

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                You can use Label instead button and lay text over image.

                S 1 Reply Last reply
                0
                • L Lost User

                  You can use Label instead button and lay text over image.

                  S Offline
                  S Offline
                  steve_9496613
                  wrote on last edited by
                  #8

                  Thank you halabella for your answer. It can be a possibility but in this way I think you lose the behavior of the button (button pressed - button not pressed). In my particular case I could just use an image because the text I need to write is a symbol of a font type not included in my Windows CE (for this reason I need to put an image in a button) but there would be much difference aesthetically with the other buttons of the form. ...but perhaps I could change the image on the click event... yes, it is a possibility. Thanks :thumbsup:

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups