Results 1 to 9 of 9

Thread: Adding a label to specific Images

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    29

    Adding a label to specific Images

    Hi, So what I am looking to do is to add a label to a specific image. At the moment I have a gallery and I would like to add labels to it to display what the image is.
    For example if Images(0) was shown, then the program would lbl.show() and lbl.hide() the other existing labels (I have three labels in total all in the same position)

    I have tried doing these lines below but the program was saying that it cannot be converted to a bitmap.

    Code:
     If PodGalleryPictureBox.Image = Images(0) Then
    lblLondon.show()
    lblLiverpool.hide()
    lblManchester.hide(0)
    End If
    
    If PodGalleryPictureBox.Image = Images(1) Then
    lblLondon.hide()
    lblLiverpool.show()
    lblManchester.hide(0)
    End If
    and this code was vice versa for each image.


    Current Code:

    Code:
    Public Class PodsGallery
    
        Dim Images(14) As Bitmap
        Dim Pos As Integer = 0
        Private Sub Pods_Gallery_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'Inserting images from resources
            Images(0) = GloriousGetaways.My.Resources._1
            Images(1) = GloriousGetaways.My.Resources._2
            Images(2) = GloriousGetaways.My.Resources._3
            Images(3) = GloriousGetaways.My.Resources._4
            Images(4) = GloriousGetaways.My.Resources._5
            Images(5) = GloriousGetaways.My.Resources._6
            Images(6) = GloriousGetaways.My.Resources._7
            Images(7) = GloriousGetaways.My.Resources._8
            Images(8) = GloriousGetaways.My.Resources._9
            Images(9) = GloriousGetaways.My.Resources._10
            Images(10) = GloriousGetaways.My.Resources._11
            Images(11) = GloriousGetaways.My.Resources._12
            Images(12) = GloriousGetaways.My.Resources._13
            Images(13) = GloriousGetaways.My.Resources._14
    
            'Puts the images into order
            PodGalleryPictureBox.Image = Images(Pos)
        End Sub
    
        'Opens Gallery Form
        Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
            Gallery.Show()
            Me.Hide()
        End Sub
    
        'Opens Book a Pod Form
        Private Sub btnBookAPod_Click(sender As Object, e As EventArgs) Handles btnBookAPod.Click
            BookAPod.Show()
            Me.Hide()
        End Sub
    
        'Opens Main Meny Form
        Private Sub btnMainMenu_Click(sender As Object, e As EventArgs) Handles btnMainMenu.Click
            MainMenu.Show()
            Me.Hide()
        End Sub
    
        'Directing the right button to show the next photo
        Private Sub btnRight_Click(sender As Object, e As EventArgs) Handles btnRight.Click
            Pos = Pos + 1
    
            If Pos < Images.Length - 1 Then
    
                PodGalleryPictureBox.Image = Images(Pos)
    
            Else
                Pos = Images.Length - 2
            End If
        End Sub
    
        'Directing the left button to show the previous photo
        Private Sub btnLeft_Click(sender As Object, e As EventArgs) Handles btnLeft.Click
            Pos = Pos - 1
    
            If Pos >= 0 Then
    
                PodGalleryPictureBox.Image = Images(Pos)
    
            Else
                Pos = 0
            End If
        End Sub
    Last edited by THopwood; Jan 20th, 2021 at 08:12 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Adding a label to specific Images

    Instead of using a Bitmap array, you should define a type that has a Bitmap property and a String property and then create an array of that type. You can then create instances of that type and put your existing Images in the Bitmap properties and the descriptions in the String properties. When you get an instance from the array, you get the Bitmap and assign it to the Image property of the PictureBox and you get the String and assign it to the Text of the Label. You can store the descriptions in resources too, if you like. Now you only need one Label, just like you only need one PictureBox.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    29

    Re: Adding a label to specific Images

    I'm using more than one label because the strings are all different sizes, therefore formatted to be centred horizontally and vertically within the panel I have put them in. Whether it's possible to display the labels like this I'm unsure.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Adding a label to specific Images

    Use a single Label, set AutoSize to False, change the Size so it is the full width of the available area and set TextAlign to MiddleCenter or, if some of the descriptions may be multi-line, TopCenter.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    29

    Re: Adding a label to specific Images

    How do I create an array with a bitmap and a string property please?
    Last edited by THopwood; Jan 20th, 2021 at 09:19 PM.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Adding a label to specific Images

    You do what I said. You define a type that had those properties and then you create an array of that type. Creating an array is the same regardless of the type and you obviously already know how to create an array, so that's a moot point. If you don't know how to define your own type (class or structure) then that's what you need to research. That's programming fundamentals and something that just about any tutorial will teach you.

  7. #7
    Lively Member
    Join Date
    Jan 2020
    Posts
    120

    Re: Adding a label to specific Images

    you should consider opting for a cleaner solution. You can instead use the PictureBox's Paint event:

    Code:
    PB = new PictureBox();
    PB.Paint += new PaintEventHandler((sender, e) =>
    {
        e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        e.Graphics.DrawString("Text", Font, Brushes.Black, 0, 0);
    });
    Edit To draw the text centered:

    Code:
    PB.Paint += new PaintEventHandler((sender, e) =>
    {
        e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
    
        string text = "Text";
    
        SizeF textSize = e.Graphics.MeasureString(text, Font);
        PointF locationToDraw = new PointF();
        locationToDraw.X = (PB.Width / 2) - (textSize.Width / 2);
        locationToDraw.Y = (PB.Height / 2) - (textSize.Height / 2);
    
        e.Graphics.DrawString(text, Font, Brushes.Black, locationToDraw);
    });


    Reference: link

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Adding a label to specific Images

    Quote Originally Posted by Prahlad View Post
    you should consider opting for a cleaner solution.
    It's a different solution but it's not inherently "cleaner". What if you don't want the image obscured by the text?

    Also, this is the VB.NET forum and you have provided C# code. Not a criminal offence but far from ideal, especially to a new programmer who is unlikely to be au fait with inline event handlers in C#.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    29

    Re: Adding a label to specific Images

    I think I'll be able to do it now using just one label. Do you have a sample of code the changes the text of the label and that's all I'll need Thank you for your help. And Yeah I'm unfamiliar with C#, this is a school project so I needed it as simple as possible to have it completed within a given timeframe.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width