Results 1 to 10 of 10

Thread: e.Graphics.DrawImage is fading stretched image

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    268

    e.Graphics.DrawImage is fading stretched image

    This is in a class, the class inherits Panel.
    VB.net Code:
    1. Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    2.         e.Graphics.DrawImage(_BackgroundImage, 0, 0, 300, 50)
    3.         MyBase.OnPaint(e)
    4.     End Sub

    _BackgroundImage is set to a PNG image, 1 px wide x 50 px tall.

    The goal is to have this image stretched to 300x50

    Whenever I do it, it works, but it fades the image from left to right, so that at Left = 0, the image is solid, but at Left = 300, the image is transparent.



    Thanks.
    Last edited by GregSenne; Dec 13th, 2010 at 11:20 AM.

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: e.Graphics.DrawImage is fading stretched image

    Add this line before you run yours:
    vb.net Code:
    1. e.Graphics.CompositingMode = Drawing2D.CompositingMode.SourceCopy

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    268

    Re: e.Graphics.DrawImage is fading stretched image

    Quote Originally Posted by ForumAccount View Post
    Add this line before you run yours:
    vb.net Code:
    1. e.Graphics.CompositingMode = Drawing2D.CompositingMode.SourceCopy
    So now it still fades, but fades to black instead of transparent.



    vb.net Code:
    1. Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    2.         e.Graphics.CompositingMode = Drawing2D.CompositingMode.SourceCopy
    3.         e.Graphics.DrawImage(_BackgroundImage, 0, 0, 300, 50)
    4.         MyBase.OnPaint(e)
    5.     End Sub

  4. #4
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: e.Graphics.DrawImage is fading stretched image

    It's an interpolation problem when magnifying.

    Code:
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            With e.Graphics
                .InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
                .PixelOffsetMode = Drawing2D.PixelOffsetMode.None
                .DrawImage(_BackgroundImage, 0, 0, 300, 50)
            End With
            MyBase.OnPaint(e)
        End Sub

  5. #5
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: e.Graphics.DrawImage is fading stretched image

    Why not just set the BackgroundImage of the Panel instead?

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    268

    Re: e.Graphics.DrawImage is fading stretched image

    Quote Originally Posted by ForumAccount View Post
    Why not just set the BackgroundImage of the Panel instead?
    Actually, this code is just really dumbed down. I need the image to appear in the panel a certain distance off the left and right edge, and stretched to fit the rest of the panel.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    268

    Re: e.Graphics.DrawImage is fading stretched image

    Quote Originally Posted by MattP View Post
    It's an interpolation problem when magnifying.

    Code:
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            With e.Graphics
                .InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
                .PixelOffsetMode = Drawing2D.PixelOffsetMode.None
                .DrawImage(_BackgroundImage, 0, 0, 300, 50)
            End With
            MyBase.OnPaint(e)
        End Sub
    Thanks, it stopped the fading and is working properly.
    Just one problem left. I adjusted it so that the image would start 20 px from the left border, and be 800 px wide.
    vb.net Code:
    1. e.Graphics.DrawImage(_BackgroundImage, 20, 0, 800, 16)
    But when I look at it in the designer, the panel I draw is 614 px wide, but looks like this:


    Even weirder, if I tell it to start 20 px in from the left border, and stretch to the width of the panel minus 40 (so there would be a 20px gap on each side, i get this):

    The above image has this code:
    vb.net Code:
    1. Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    2.  
    3.         e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
    4.         e.Graphics.PixelOffsetMode = Drawing2D.PixelOffsetMode.None
    5.         e.Graphics.DrawImage(_BackgroundImage, 20, 0, Me.Width - 40, Me.Height)
    6.         Call MyBase.OnPaint(e)
    If I set the width to 50 (same code as above just substitute me.Width - 40 for 50), it paints the background image 25 pixels wide.

    I have tried adjusting the InterpolationMode, but everything besides NearestNeighbor shows up as a fade to transparent.
    Thanks for the help.
    Greg

    Thanks.
    Last edited by GregSenne; Dec 13th, 2010 at 01:21 PM.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    268

    Re: e.Graphics.DrawImage is fading stretched image

    Okay I just multiplied the width by 2 and it seems to work fine now. I have no idea why (Why would I have to double the width to get the desired width), but it is working
    vb.net Code:
    1. e.Graphics.DrawImage(_BackgroundImage, 20, 0, (Me.Width - 40) * 2, Me.Height)

  9. #9
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: e.Graphics.DrawImage is fading stretched image

    Try it with PixelOffsetMode=Half. Then the stretched pixel will be centred in the drawing rectangle. In default PixelOffsetMode, the pixel is centred on the origin. BB

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    268

    Re: e.Graphics.DrawImage is fading stretched image

    Quote Originally Posted by boops boops View Post
    Try it with PixelOffsetMode=Half. Then the stretched pixel will be centred in the drawing rectangle. In default PixelOffsetMode, the pixel is centred on the origin. BB
    Thanks, I think I got it working just the way I want it now!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width