Solved

GDI+ : Stretching vs. Tiling in Graphics.DrawImage

Posted on 2004-09-20
7
2,026 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
  • 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now