?
Solved

GDI+ : Stretching vs. Tiling in Graphics.DrawImage

Posted on 2004-09-20
7
Medium Priority
?
2,181 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
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!

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

764 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