Solved

GDI+ : Stretching vs. Tiling in Graphics.DrawImage

Posted on 2004-09-20
7
2,097 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
Independent Software Vendors: 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!

 

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 125 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

763 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