Results 1 to 12 of 12

Thread: [RESOLVED] How can I create a rectangle with a repeated image as background?

  1. #1

    Thread Starter
    Lively Member Simonetos The Greek's Avatar
    Join Date
    Mar 2018
    Location
    Athens, Greece
    Posts
    65

    Resolved [RESOLVED] How can I create a rectangle with a repeated image as background?



    For example, I have this part of code which draw an image on a rectangle.
    vb.net Code:
    1. '
    2. Dim _Image = My.Resources.MyImage
    3. Dim _Rectangle As New Rectangle(0, 0, 100,100)
    4. e.Graphics.DrawImage(_Image, _Rectangle)
    5. '
    And I want this image to appear as repeated background image. Any idea how can I do that?



    EDIT: I changed the title because it didn't exactly describe what I wanted to do. I also changed a part of my question's body for the same reason.
    Last edited by Simonetos The Greek; May 19th, 2018 at 03:42 AM. Reason: Title and question's body correction.

  2. #2

    Thread Starter
    Lively Member Simonetos The Greek's Avatar
    Join Date
    Mar 2018
    Location
    Athens, Greece
    Posts
    65

    Re: How can I create a rectangle with a repeated image as background?

    Sorry I found what I was looking for, here. And here is a simple example...
    vb.net Code:
    1. Dim image As New Bitmap("HouseAndTree.gif")
    2. Dim tBrush As New TextureBrush(image)
    3. Dim blackPen As New Pen(Color.Black)
    4. e.Graphics.FillRectangle(tBrush, New Rectangle(0, 0, 200, 200))
    5. e.Graphics.DrawRectangle(blackPen, New Rectangle(0, 0, 200, 200))


    EDIT: Please ignore this answer!!!
    Last edited by Simonetos The Greek; May 19th, 2018 at 03:36 AM.

  3. #3
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: [RESOLVED] I need help with DrawImage

    there is a graphics.drawimage as well so you do not need the texture brush

  4. #4

    Thread Starter
    Lively Member Simonetos The Greek's Avatar
    Join Date
    Mar 2018
    Location
    Athens, Greece
    Posts
    65

    Re: How can I create a rectangle with a repeated image as background?

    Quote Originally Posted by digitalShaman View Post
    there is a graphics.drawimage as well so you do not need the texture brush
    Oh, thank you very much my friend I'll look for it!!!



    EDIT: Please ignore this answer!!!
    Last edited by Simonetos The Greek; May 19th, 2018 at 03:44 AM.

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: [RESOLVED] I need help with DrawImage

    Not only can you use the DrawImage method like digitalShaman suggested, but I would also suggest wrapping the pen in a Using statement:
    Code:
    'Declare the image and the size/location it'll be drawn
    Dim image As New Bitmap("HouseAndTree.gif")
    Dim bounds As New Rectangle(0, 0, 200, 200)
    
    With e.Graphics
        .DrawImage(image, bounds) 'Draw the image
    
        'Create a pen to draw the border
        Using border As New Pen(Color.Black)
            .DrawRectangle(border, bounds) 'Draw the border
        End Using
    End With
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: [RESOLVED] I need help with DrawImage

    Quote Originally Posted by digitalShaman View Post
    there is a graphics.drawimage as well so you do not need the texture brush
    Quote Originally Posted by Simonetos The Greek View Post
    Oh, thank you very much my friend I'll look for it!!!
    Since you used DrawImage in your first post, haven't you already looked at it. I'm not sure why it was brought up if you've already tried it and decided it didn't do what you needed.
    Code:
    Dim _Image = My.Resources.MyImage
    Dim _Rectangle As New Rectangle(0, 0, 100,100)
    e.Graphics.DrawImage(_Image, _Rectangle)
    Since you discounted it after the first post, I'm not sure how the requirements description in your first post are solved by the code in the second post, i.e. "... image to appear as BackGroundImage? With Layout property. ..."

  7. #7

    Thread Starter
    Lively Member Simonetos The Greek's Avatar
    Join Date
    Mar 2018
    Location
    Athens, Greece
    Posts
    65

    Re: How can I create a rectangle with a repeated image as background?

    Quote Originally Posted by passel View Post
    Since you used DrawImage in your first post, haven't you already looked at it. I'm not sure why it was brought up if you've already tried it and decided it didn't do what you needed.
    Code:
    Dim _Image = My.Resources.MyImage
    Dim _Rectangle As New Rectangle(0, 0, 100,100)
    e.Graphics.DrawImage(_Image, _Rectangle)
    Since you discounted it after the first post, I'm not sure how the requirements description in your first post are solved by the code in the second post, i.e. "... image to appear as BackGroundImage? With Layout property. ..."
    Yes you are right, I am sorry, I confused with a question on another site... Tomorrow I'll correct it!!!
    Last edited by Simonetos The Greek; May 19th, 2018 at 03:40 AM.

  8. #8

    Thread Starter
    Lively Member Simonetos The Greek's Avatar
    Join Date
    Mar 2018
    Location
    Athens, Greece
    Posts
    65

    Re: How can I create a rectangle with a repeated image as background?

    Well, first of all sorry that I marked my question as solved without this actually being true in relation to the code example I gave. The reason is because I confused with an other question which I made on another site.

    In a few words, in my initial question, I asked how can I create a rectangle with a repeated image as background. I gave this example, which creates a rectangle with an image "inside" but I couldn't achieve the repeat of this image...
    vb.net Code:
    1. '
    2. Dim _Image = My.Resources.MyImage
    3. Dim _Rectangle As New Rectangle(0, 0, 100,100)
    4. e.Graphics.DrawImage(_Image, _Rectangle)
    5. '
    Then I came up with a wrong solution cause as I said before I confused with an other question which I made on another site. So, this is the solution on which I ended up and I found it here.
    vb.net Code:
    1. Dim _Image As New Bitmap(My.Resources.my_image)
    2. Dim _TextureBrush As New TextureBrush(_Image, WrapMode.Tile)
    3. Dim _Rectangle As New Rectangle(0,0, 100, 100)
    4. e.Graphics.FillRectangle(_TextureBrush, _Rectangle)
    The "key" was the TextureBrush and the WrapMode property.



    Now, about digitalShaman's post...
    Quote Originally Posted by digitalShaman View Post
    there is a graphics.drawimage as well so you do not need the texture brush
    I looked for it but I didn't find any example on how can make it repeat the image. This was my problem. I did use drawimage but I didn't know how to repeat the image. So, If anyone have an other solution with the use of drawimage...

    Quote Originally Posted by passel View Post
    Since you used DrawImage in your first post, haven't you already looked at it. I'm not sure why it was brought up if you've already tried it and decided it didn't do what you needed.
    Code:
    Dim _Image = My.Resources.MyImage
    Dim _Rectangle As New Rectangle(0, 0, 100,100)
    e.Graphics.DrawImage(_Image, _Rectangle)
    Since you discounted it after the first post, I'm not sure how the requirements description in your first post are solved by the code in the second post, i.e. "... image to appear as BackGroundImage? With Layout property. ..."
    Sorry for this confusion I caused!!!



    PS: For anyone who posted here, please change the title of your post to "Re: How can I create a rectangle with a repeated image as background?". Thank you and sorry again...
    Last edited by Simonetos The Greek; May 19th, 2018 at 03:49 AM.

  9. #9
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: How can I create a rectangle with a repeated image as background?

    Wouldn't it just be a mather of looping?

    something like so?

    Code:
            Dim Img As Bitmap = My.Resources.MyImage
            Dim Size As New Size(100, 100)
            Dim Offset As New Point(0, 0)
    
            Do While Offset.Y < Me.Height
                Do While Offset.X < Me.Width
                    e.Graphics.DrawImage(Img, New Rectangle(Offset.X, Offset.Y, Size.Width, Size.Height))
                    Offset.X += Size.Width
                Loop
                Offset.Y += Size.Height
                Offset.X = 0
            Loop
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  10. #10

    Thread Starter
    Lively Member Simonetos The Greek's Avatar
    Join Date
    Mar 2018
    Location
    Athens, Greece
    Posts
    65

    Re: How can I create a rectangle with a repeated image as background?

    Quote Originally Posted by Goggy View Post
    Wouldn't it just be a mather of looping?

    something like so?

    Code:
            Dim Img As Bitmap = My.Resources.MyImage
            Dim Size As New Size(100, 100)
            Dim Offset As New Point(0, 0)
    
            Do While Offset.Y < Me.Height
                Do While Offset.X < Me.Width
                    e.Graphics.DrawImage(Img, New Rectangle(Offset.X, Offset.Y, Size.Width, Size.Height))
                    Offset.X += Size.Width
                Loop
                Offset.Y += Size.Height
                Offset.X = 0
            Loop
    Yes, nice idea, thank you for your answer!!! But I'll prefer the solution I posted last because it gives me the ability of WrapMode property. Except if there is any similar way to do that with DrawImage...

  11. #11
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How can I create a rectangle with a repeated image as background?

    DrawImage will only draw one image. You could do a nested loop to do multiple draws, but there is really no reason to do that. Filling a rectangle with a pattern, such as a seamless tile image, is a primary reason for having a texturebrush.

    Also, using a texturebrush usually draws much quicker than using DrawImage. If you're reusing the same image to fill the background whenever you need to draw, I would normally create the texturebrush at the Form Class level so I could use it repeatedly whenever needed without having the overhead of creating and disposing of it every time used.

  12. #12

    Thread Starter
    Lively Member Simonetos The Greek's Avatar
    Join Date
    Mar 2018
    Location
    Athens, Greece
    Posts
    65

    Re: How can I create a rectangle with a repeated image as background?

    Quote Originally Posted by passel View Post
    If you're reusing the same image to fill the background whenever you need to draw, I would normally create the texturebrush at the Form Class level so I could use it repeatedly whenever needed without having the overhead of creating and disposing of it every time used.
    Thank you for your advice my friend!!!

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