Re: How to stretch a background image of a windows form?

  • Thread starter Thread starter Herfried K. Wagner
  • Start date Start date
H

Herfried K. Wagner

Hello,

Arthur Hsu said:
I write a windows form using C# with a background image on it. When it's
running on US English Windows, everything is fine. When it's running on
Chinese Windows, the background image tiled up. How can I stretch the
background image to fit this form?

In VB .NET:

\\\
Private m_bi As Bitmap

Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles MyBase.Load
m_bi = Image.FromFile("C:\WINDOWS\Angler.bmp")
End Sub

Private Sub Form1_Resize( _
ByVal sender As Object, _
ByVal e As System.EventArgs _
) Handles MyBase.Resize
Me.CreateGraphics().DrawImage( _
m_bi, 0, 0, m_bi.Width, m_bi.Height _
)
End Sub

Private Sub Form1_Paint( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs _
) Handles MyBase.Paint
e.Graphics.DrawImage(m_bi, 0, 0, m_bi.Width,
m_bi.Height)
End Sub
///

Regards,
Herfried K. Wagner
 
Hello,

I got the answers from Google and it works well.

The C# solution

protected override void OnPaintBackground(PaintEventArgs pevent)
{
if (this.BackgroundImage != null)
{
pevent.Graphics.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.Low;
//pevent.Graphics.Clear(this.BackColor);
pevent.Graphics.DrawImage(this.BackgroundImage, 0, 0,
this.ClientRectangle.Width, this.ClientRectangle.Height);
}
else
{
base.OnPaintBackground(pevent);
}
}

The pevent.Graphics.Clear(this.BackColor); is commented out because I use a
background image with alpha channel. If you don't, you'd better add it
back.

Regards,

Arthur
 
Back
Top