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.
http://i1098.photobucket.com/albums/...sue_121310.png
Thanks.
Re: e.Graphics.DrawImage is fading stretched image
Add this line before you run yours:
vb.net Code:
e.Graphics.CompositingMode = Drawing2D.CompositingMode.SourceCopy
Re: e.Graphics.DrawImage is fading stretched image
Quote:
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.
http://i1098.photobucket.com/albums/...e_121310_2.png
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
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
Re: e.Graphics.DrawImage is fading stretched image
Why not just set the BackgroundImage of the Panel instead?
Re: e.Graphics.DrawImage is fading stretched image
Quote:
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.
Re: e.Graphics.DrawImage is fading stretched image
Quote:
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:
http://i1098.photobucket.com/albums/...e_121310_3.png
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):
http://i1098.photobucket.com/albums/...e_121310_4.png
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.
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)
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
Re: e.Graphics.DrawImage is fading stretched image
Quote:
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! :D