|
-
Dec 13th, 2010, 11:11 AM
#1
Thread Starter
Hyperactive Member
e.Graphics.DrawImage is fading stretched image
This is in a class, the class inherits Panel.
VB.net Code:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) e.Graphics.DrawImage(_BackgroundImage, 0, 0, 300, 50) MyBase.OnPaint(e) 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.
-
Dec 13th, 2010, 11:21 AM
#2
Re: e.Graphics.DrawImage is fading stretched image
Add this line before you run yours:
vb.net Code:
e.Graphics.CompositingMode = Drawing2D.CompositingMode.SourceCopy
-
Dec 13th, 2010, 11:29 AM
#3
Thread Starter
Hyperactive Member
Re: e.Graphics.DrawImage is fading stretched image
 Originally Posted by ForumAccount
Add this line before you run yours:
vb.net Code:
e.Graphics.CompositingMode = Drawing2D.CompositingMode.SourceCopy
So now it still fades, but fades to black instead of transparent.

vb.net Code:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
e.Graphics.CompositingMode = Drawing2D.CompositingMode.SourceCopy
e.Graphics.DrawImage(_BackgroundImage, 0, 0, 300, 50)
MyBase.OnPaint(e)
End Sub
-
Dec 13th, 2010, 11:36 AM
#4
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
-
Dec 13th, 2010, 11:46 AM
#5
Re: e.Graphics.DrawImage is fading stretched image
Why not just set the BackgroundImage of the Panel instead?
-
Dec 13th, 2010, 11:51 AM
#6
Thread Starter
Hyperactive Member
Re: e.Graphics.DrawImage is fading stretched image
 Originally Posted by ForumAccount
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.
-
Dec 13th, 2010, 12:03 PM
#7
Thread Starter
Hyperactive Member
Re: e.Graphics.DrawImage is fading stretched image
 Originally Posted by MattP
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:
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:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor e.Graphics.PixelOffsetMode = Drawing2D.PixelOffsetMode.None e.Graphics.DrawImage(_BackgroundImage, 20, 0, Me.Width - 40, Me.Height) 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.
-
Dec 13th, 2010, 01:31 PM
#8
Thread Starter
Hyperactive Member
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:
e.Graphics.DrawImage(_BackgroundImage, 20, 0, (Me.Width - 40) * 2, Me.Height)
-
Dec 13th, 2010, 01:35 PM
#9
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
-
Dec 13th, 2010, 04:27 PM
#10
Thread Starter
Hyperactive Member
Re: e.Graphics.DrawImage is fading stretched image
 Originally Posted by boops boops
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|