-
May 18th, 2018, 05:44 AM
#1
Thread Starter
Lively Member
[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:
'
Dim _Image = My.Resources.MyImage
Dim _Rectangle As New Rectangle(0, 0, 100,100)
e.Graphics.DrawImage(_Image, _Rectangle)
'
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.
-
May 18th, 2018, 05:56 AM
#2
Thread Starter
Lively Member
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:
Dim image As New Bitmap("HouseAndTree.gif")
Dim tBrush As New TextureBrush(image)
Dim blackPen As New Pen(Color.Black)
e.Graphics.FillRectangle(tBrush, New Rectangle(0, 0, 200, 200))
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.
-
May 18th, 2018, 08:25 AM
#3
Re: [RESOLVED] I need help with DrawImage
there is a graphics.drawimage as well so you do not need the texture brush
-
May 18th, 2018, 08:59 AM
#4
Thread Starter
Lively Member
Re: How can I create a rectangle with a repeated image as background?
Originally Posted by digitalShaman
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.
-
May 18th, 2018, 10:18 AM
#5
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
-
May 18th, 2018, 12:37 PM
#6
Re: [RESOLVED] I need help with DrawImage
Originally Posted by digitalShaman
there is a graphics.drawimage as well so you do not need the texture brush
Originally Posted by Simonetos The Greek
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. ..."
-
May 18th, 2018, 03:03 PM
#7
Thread Starter
Lively Member
Re: How can I create a rectangle with a repeated image as background?
Originally Posted by passel
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.
-
May 19th, 2018, 03:22 AM
#8
Thread Starter
Lively Member
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:
'
Dim _Image = My.Resources.MyImage
Dim _Rectangle As New Rectangle(0, 0, 100,100)
e.Graphics.DrawImage(_Image, _Rectangle)
'
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:
Dim _Image As New Bitmap(My.Resources.my_image)
Dim _TextureBrush As New TextureBrush(_Image, WrapMode.Tile)
Dim _Rectangle As New Rectangle(0,0, 100, 100)
e.Graphics.FillRectangle(_TextureBrush, _Rectangle)
The "key" was the TextureBrush and the WrapMode property.
Now, about digitalShaman's post...
Originally Posted by digitalShaman
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...
Originally Posted by passel
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.
-
May 19th, 2018, 04:36 AM
#9
Addicted Member
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
-
May 19th, 2018, 05:01 AM
#10
Thread Starter
Lively Member
Re: How can I create a rectangle with a repeated image as background?
Originally Posted by Goggy
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...
-
May 19th, 2018, 03:59 PM
#11
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.
-
May 19th, 2018, 04:15 PM
#12
Thread Starter
Lively Member
Re: How can I create a rectangle with a repeated image as background?
Originally Posted by passel
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|