[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

GDI+ : Stretching vs. Tiling in Graphics.DrawImage

Posted on 2004-09-20
7
Medium Priority
?
2,214 Views
Last Modified: 2008-02-01
My app copies rectangular sections of one image to another. The destination is not necessarily the same size as the source...sometimes I want to stretch these images, and sometimes tile them.

Looks like Graphics.DrawImage can't tile an image, but I don't know. When copying using Graphics.DrawImage, the image is stretched...that's the default behavior. Certain overloads take ImageAttributes, which should allow me to tile, but can anyone get this to work. Here's a line from my code:

G.DrawImage(PaintImage, Destination, iLeftMargin, iTopMargin, iWidth, iHeight, GraphicsUnit.Pixel, imgAttributes)

Can anyone construct a code example where imgAttributes causes the source to tile into the destination? If not, then how to tile an image? I was thinking maybe that I have to create a brush to tile an image since DrawImage doesn't allow it...

Thanks.

Roger
0
Comment
Question by:rdavis101
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12100351
This should work


        Dim x As System.Drawing.Imaging.ImageAttributes
        x.SetWrapMode(Drawing2D.WrapMode.Tile)
G.DrawImage(PaintImage, Destination, iLeftMargin, iTopMargin, iWidth, iHeight, GraphicsUnit.Pixel, x)
0
 

Author Comment

by:rdavis101
ID: 12100408
Tried it, but no luck... You try testing it?
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12100430
Yep, It works

this is how I tested it. The original image (test.jpg) is  141 x 109 big (so it is tiled 4 times, once complete and 3 times incomplete)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim x As New System.Drawing.Imaging.ImageAttributes
        x.SetWrapMode(Drawing2D.WrapMode.Tile)
        Dim g As Graphics = Me.CreateGraphics
        Dim i As Image = Image.FromFile("c:\test.jpg")
        Dim rec As New Rectangle(0, 0, 200, 200)
        g.DrawImage(i, rec, 0, 0, 200, 200, GraphicsUnit.Pixel, x)
   end sub


0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:rdavis101
ID: 12100832
I tested your code and it does indeed work.  I'm clueless why this doesn't work for me. Let me tell you what I'm doing. I decided to build a custom tab control that could be skinned, because there weren't any 3rd party controls I liked. So...when a button (or any other window) is skinned, the bitmap used to skin the button is divided into 9 areas, like a tic-tac-toe grid. The corners don't get stretched or tiled, and the sides get stretched or tiled only in such a way as to look pretty.

So I create a class that takes the TopMargin, LeftMargin, RightMargin, and BottomMargin as properties. The user can attach the class to any button, or really any control, and then skin the object in its Paint Event...like so:
---------------------------
    Private Sub SkinMe(ByVal G As Graphics)
        If imgNormal Is Nothing Then Exit Sub 'If the user hasn't assigned an skinning image yet, just exit

        Dim PaintImage As Image '


        If bInside = True And Not imgMouseover Is Nothing Then 'The the user isn't using a Mouseover image.
            PaintImage = imgMouseover
        Else
            PaintImage = imgNormal
        End If
        If bInside = False Then PaintImage = imgNormal

        Dim RightSideX As Integer
        Dim BottomSideY As Integer
        RightSideX = PaintImage.Width - iRightMargin
        BottomSideY = PaintImage.Height - iBottomMargin

        atTopAttributes.SetWrapMode(Drawing2D.WrapMode.Tile)
        atBottomAttributes.SetWrapMode(Drawing2D.WrapMode.Tile)
        atRightAttributes.SetWrapMode(Drawing2D.WrapMode.Tile)
        atLeftAttributes.SetWrapMode(Drawing2D.WrapMode.Tile)
        atMiddleAttributes.SetWrapMode(Drawing2D.WrapMode.Tile)

        Dim UpperLeftDestination As New Rectangle(0, 0, iLeftMargin, iTopMargin)
        Dim UpperMiddleDestination As New Rectangle(iLeftMargin, 0, sknButton.Width - iRightMargin - iLeftMargin, iTopMargin)
        Dim UpperRightDestination As New Rectangle(sknButton.Width - iRightMargin, 0, iRightMargin, iTopMargin)
        Dim MiddleLeftDestination As New Rectangle(0, iTopMargin, iLeftMargin, sknButton.Height - iBottomMargin - iTopMargin)
        Dim MiddleMiddleDestination As New Rectangle(iLeftMargin, iTopMargin, sknButton.Width - iRightMargin - iLeftMargin, sknButton.Height - iBottomMargin - iTopMargin)
        Dim MiddleRightDestination As New Rectangle(sknButton.Width - iRightMargin, iTopMargin, iRightMargin, sknButton.Height - iBottomMargin - iTopMargin)
        Dim LowerLeftDestination As New Rectangle(0, sknButton.Height - iBottomMargin, iLeftMargin, iBottomMargin)
        Dim LowerMiddleDestination As New Rectangle(iLeftMargin, sknButton.Height - iBottomMargin, sknButton.Width - iRightMargin - iLeftMargin, iBottomMargin)
        Dim LowerRightDestination As New Rectangle(sknButton.Width - iRightMargin, sknButton.Height - iBottomMargin, iRightMargin, iBottomMargin)

        G.DrawImage(PaintImage, UpperLeftDestination, 0, 0, iLeftMargin, iTopMargin, GraphicsUnit.Pixel)
        G.DrawImage(PaintImage, MiddleLeftDestination, 0, iTopMargin, iLeftMargin, PaintImage.Height - iBottomMargin - iTopMargin, GraphicsUnit.Pixel, atLeftAttributes)
        G.DrawImage(PaintImage, LowerLeftDestination, 0, BottomSideY, iLeftMargin, iBottomMargin, GraphicsUnit.Pixel)

        G.DrawImage(PaintImage, UpperRightDestination, RightSideX, 0, iRightMargin, iTopMargin, GraphicsUnit.Pixel)
        G.DrawImage(PaintImage, MiddleRightDestination, RightSideX, iTopMargin, iRightMargin, PaintImage.Height - iTopMargin - iBottomMargin, GraphicsUnit.Pixel, atRightAttributes)
        G.DrawImage(PaintImage, LowerRightDestination, RightSideX, BottomSideY, iBottomMargin, iRightMargin, GraphicsUnit.Pixel)

        G.DrawImage(PaintImage, UpperMiddleDestination, iLeftMargin, 0, PaintImage.Width - iLeftMargin - iRightMargin, iTopMargin, GraphicsUnit.Pixel, TopAttributes)
        G.DrawImage(PaintImage, MiddleMiddleDestination, iLeftMargin, iTopMargin, PaintImage.Width - iLeftMargin - iRightMargin, PaintImage.Height - iTopMargin - iBottomMargin, GraphicsUnit.Pixel, atMiddleAttributes)
        G.DrawImage(PaintImage, LowerMiddleDestination, iLeftMargin, BottomSideY, PaintImage.Width - iLeftMargin - iRightMargin, iBottomMargin, GraphicsUnit.Pixel, atBottomAttributes)

    End Sub
--------------------
The code works...I can anchor a button to three sides of a form, then skin it and it stretches in realtime and does a nice stretched mouseover, too (obviously, there's other code supporting this).

But it doesn't let me choose between tile or stretch. I tried passing attributes as properties, which is ultimately what I want to do. No luck. That led to simply including the block

        atTopAttributes.SetWrapMode(Drawing2D.WrapMode.Tile)
        atBottomAttributes.SetWrapMode(Drawing2D.WrapMode.Tile)
        atRightAttributes.SetWrapMode(Drawing2D.WrapMode.Tile)
        atLeftAttributes.SetWrapMode(Drawing2D.WrapMode.Tile)
        atMiddleAttributes.SetWrapMode(Drawing2D.WrapMode.Tile)

directly in the code that handles the paint event... But it's not working. The program runs and the button edges stretch instead of tile.

Any clues?

Roger
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12101071
I'll have a look
0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 500 total points
ID: 12101577
Hi Rdavis101,

try this, replace your drawcode with this

        G.DrawImage(PaintImage, UpperLeftDestination, 0, 0, iLeftMargin, iTopMargin, GraphicsUnit.Pixel)
        G.DrawImage(PaintImage, MiddleLeftDestination, 0, 0, MiddleLeftDestination.Width, MiddleLeftDestination.Height, GraphicsUnit.Pixel, atLeftAttributes)
        G.DrawImage(PaintImage, LowerLeftDestination, 0, BottomSideY, iLeftMargin, iBottomMargin, GraphicsUnit.Pixel)

        G.DrawImage(PaintImage, UpperRightDestination, RightSideX, 0, iRightMargin, iTopMargin, GraphicsUnit.Pixel)
        G.DrawImage(PaintImage, MiddleRightDestination, 0, 0, MiddleRightDestination.Width, MiddleRightDestination.Height, GraphicsUnit.Pixel, atMiddleAttributes)
        G.DrawImage(PaintImage, LowerRightDestination, RightSideX, BottomSideY, iBottomMargin, iRightMargin, GraphicsUnit.Pixel)

        G.DrawImage(PaintImage, UpperMiddleDestination, 0, 0, UpperMiddleDestination.Width, UpperMiddleDestination.Height, GraphicsUnit.Pixel, atTopAttributes)
        G.DrawImage(PaintImage, MiddleMiddleDestination, 0, 0, MiddleMiddleDestination.Width, MiddleMiddleDestination.Height, GraphicsUnit.Pixel, atMiddleAttributes)
        G.DrawImage(PaintImage, LowerMiddleDestination, 0, 0, LowerMiddleDestination.Width, LowerMiddleDestination.Height, GraphicsUnit.Pixel, atBottomAttributes)
0
 

Author Comment

by:rdavis101
ID: 12106568
Awesome, dude. Thanks so much!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question