How to stretch a background image of a windows form

来源:互联网 发布:楠木梁知txt书包网 编辑:程序博客网 时间:2024/06/04 17:59
Hello,

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?

TIA,

Arthur

Reply to this message...        Herfried K. Wagner Hello,

"Arthur Hsu" <Click here to reveal e-mail address> schrieb:
[Original message clipped]

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
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet

Reply to this message...        Arthur Hsu 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
 
原创粉丝点击