locked
Image Stretch RRS feed

  • Question

  • Hi,

    I figured it would be neat to have rounded corners for a Label on my form 1. I produced a .png for the mask, it has a white main area and the rounded corners are the same colour as the Form1 and TableLayoutPanel behind it. I coded the white area as transparent. Fine all works well.

    Come to run the application on a computer with a lower resolution and I find the mask disappears because there's no scaling of the image with a Label. Ok... No problem, I replaced the Label with a Button because that does have image options, I chose 'stretch'. 

    Works ok on the original machine but still doesn't stretch on the lower resolution... Why is that ?

    OSD

    Sunday, December 24, 2017 12:11 PM

Answers

  • Here are some examples?

    A label that stretches and is truly transparent to the form color below. But you have to draw the text yourself. You can see the teal form backcolor come through the label. Of course since its all draw your own you could use a picturebox or just draw a rectangle on the form and do away with the control altogether.

    A button that stretches but is not transparent you have to make the mask and the form the same color. So in the example the form would be black to hide the black mask.

    If you make your own class then you can do more. There are many ways.

    If you show us your method code then perhaps we can explain it. Perhaps you used the image property and should use backgroundimage as I show and etc.

    Public Class Form8
        Dim BgBmp As New Bitmap("c:\bitmaps\label round.png")
    
        Private Sub Label1_Paint(sender As Object, e As PaintEventArgs) Handles Label1.Paint
    
            'draw the text bitmap with transparent background
            Dim attribs As New System.Drawing.Imaging.ImageAttributes
            attribs.SetColorKey(Color.Black, Color.Black)
    
            e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
            e.Graphics.DrawImage(BgBmp,
                                 New Rectangle(0, 0, Label1.ClientRectangle.Width, Label1.ClientRectangle.Height),
                                 0, 0, BgBmp.Width, BgBmp.Height,
                                 GraphicsUnit.Pixel, attribs)
            e.Graphics.DrawString(Label1.Text, Label1.Font, Brushes.Black, 20, 10)
    
        End Sub
    
        Private Sub Form8_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            BackColor = Color.Teal
            Label1.Font = New Font("tahoma", 18, FontStyle.Bold)
            Label1.AutoSize = False
    
            Button1.Font = New Font("tahoma", 18, FontStyle.Bold)
            Button1.AutoSize = False
            Button1.BackgroundImage = BgBmp
            Button1.BackgroundImageLayout = ImageLayout.Stretch
            Button1.FlatStyle = FlatStyle.Flat
            Button1.FlatAppearance.BorderSize = 0
            Button1.ForeColor = Color.DimGray
        End Sub
    End Class

    here is the bitmap mask I used in the image:


    • Proposed as answer by Frank L. Smith Monday, December 25, 2017 1:38 PM
    • Marked as answer by Old_Sea_Dog Wednesday, December 27, 2017 10:01 AM
    Monday, December 25, 2017 1:28 PM
  • Ah... OK guys, I found my problem. "Must be more careful".

    I'd put the image in the Button's 'Image' instead of in it's 'BackgroundImage'.

    Thanks for the replies.

    OSD.


    • Edited by Old_Sea_Dog Monday, December 25, 2017 4:55 PM
    • Marked as answer by Old_Sea_Dog Wednesday, December 27, 2017 10:01 AM
    Monday, December 25, 2017 4:55 PM

All replies

  • Dog,

    Please show the code you use to put the image in the control.

    What are you calling resolution?

    Please show an image of the label when it works how you want and when it does not on the other computer. Describe what you mean.

    What is the display setting in Control Panel on the other computer and what version windows?

    Sunday, December 24, 2017 1:45 PM
  • Here are some examples?

    A label that stretches and is truly transparent to the form color below. But you have to draw the text yourself. You can see the teal form backcolor come through the label. Of course since its all draw your own you could use a picturebox or just draw a rectangle on the form and do away with the control altogether.

    A button that stretches but is not transparent you have to make the mask and the form the same color. So in the example the form would be black to hide the black mask.

    If you make your own class then you can do more. There are many ways.

    If you show us your method code then perhaps we can explain it. Perhaps you used the image property and should use backgroundimage as I show and etc.

    Public Class Form8
        Dim BgBmp As New Bitmap("c:\bitmaps\label round.png")
    
        Private Sub Label1_Paint(sender As Object, e As PaintEventArgs) Handles Label1.Paint
    
            'draw the text bitmap with transparent background
            Dim attribs As New System.Drawing.Imaging.ImageAttributes
            attribs.SetColorKey(Color.Black, Color.Black)
    
            e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
            e.Graphics.DrawImage(BgBmp,
                                 New Rectangle(0, 0, Label1.ClientRectangle.Width, Label1.ClientRectangle.Height),
                                 0, 0, BgBmp.Width, BgBmp.Height,
                                 GraphicsUnit.Pixel, attribs)
            e.Graphics.DrawString(Label1.Text, Label1.Font, Brushes.Black, 20, 10)
    
        End Sub
    
        Private Sub Form8_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            BackColor = Color.Teal
            Label1.Font = New Font("tahoma", 18, FontStyle.Bold)
            Label1.AutoSize = False
    
            Button1.Font = New Font("tahoma", 18, FontStyle.Bold)
            Button1.AutoSize = False
            Button1.BackgroundImage = BgBmp
            Button1.BackgroundImageLayout = ImageLayout.Stretch
            Button1.FlatStyle = FlatStyle.Flat
            Button1.FlatAppearance.BorderSize = 0
            Button1.ForeColor = Color.DimGray
        End Sub
    End Class

    here is the bitmap mask I used in the image:


    • Proposed as answer by Frank L. Smith Monday, December 25, 2017 1:38 PM
    • Marked as answer by Old_Sea_Dog Wednesday, December 27, 2017 10:01 AM
    Monday, December 25, 2017 1:28 PM
  • I think you guys have got hold of the wrong end of the stick somwhere. I thought I'd explained all this, my mask works just fine, no problems at all.

    My problem is that when I run exactly the same app. on a machine with a lower screen resolution, i.e. 1,366 x 768 instead of 1,920 x 1080  the Button's background image doesn't stretch despite stretch mode being selected.

    My question concerns the fact that the Button's Background Image doesn't 'Stretch'.

    OSD.


    Monday, December 25, 2017 4:40 PM
  • Ah... OK guys, I found my problem. "Must be more careful".

    I'd put the image in the Button's 'Image' instead of in it's 'BackgroundImage'.

    Thanks for the replies.

    OSD.


    • Edited by Old_Sea_Dog Monday, December 25, 2017 4:55 PM
    • Marked as answer by Old_Sea_Dog Wednesday, December 27, 2017 10:01 AM
    Monday, December 25, 2017 4:55 PM
  • Ah... OK guys, I found my problem. "Must be more careful".

    I'd put the image in the Button's 'Image' instead of in it's 'BackgroundImage'.

    Thanks for the replies.

    OSD.


     Hmmm...  apparently you did not read the quoted sentence below of what TommyTwoTrains said in his last post.

    "Perhaps you used the image property and should use backgroundimage as I show and etc"

     Maybe you did see that and just forgot to mark his post as the answer.  So, if Tom gave you the solution or the information that lead you to figuring out your problem,  then please remember to mark Tom's post as the answer.  Thanks.


    If you say it can`t be done then i`ll try it

    Monday, December 25, 2017 5:32 PM