简体   繁体   English

如何使Windows窗体适合任何屏幕分辨率,以及如何使我的图像大小适合窗体

[英]How to fit windows form to any screen resolution and how to made my image size fit to form

I have the following code in which I am trying to display an image and play an MP3 song. 我有以下代码,其中我试图显示图像并播放MP3歌曲。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
using WMPLib;
using System.Windows;

namespace CreatingInstaller
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Size = Screen.PrimaryScreen.WorkingArea.Size;
            var size = this.Size;
            var screen = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
            PictureBox pb1 = new PictureBox();
            Image img = Image.FromFile(Application.StartupPath + "\\Input\\CYMERA_20141109_141742.jpg");
            //this.Width = img.Width;
            //this.Height = screen.Height;
            pb1.Image = img;
            //pb1.Width = img.Width;
            //pb1.Height = screen.Height; 
            pb1.Size = this.Size;
            this.Controls.Add(pb1);

            WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();

            wplayer.URL = Application.StartupPath + "\\Input\\Nee Sneham - [www.MazaMp3.com].mp3";
            wplayer.controls.play();
        }
    }
}

When I ran this code Form height is exceeding my screen height and I am unable to see the full image. 当我运行此代码时,表单高度超过了我的屏幕高度,并且我看不到完整的图像。 Image is not fitting in the form. 图片与表单不符。

Can anyone help on this? 有人可以帮忙吗?

对图片框使用锚点 ,也可以对SizeMode使用锚点

WindowState = FormWindowState.Maximized; WindowState = FormWindowState.Maximized;

write the above line on load event of the form. 将上面的代码写在表单的load事件上。

I think the best in your case is to avoid to use PictureBox, and set the image in the BackgroundImage of the form and set the form BackgroundImageLayout to Stretch. 我认为最好的情况是避免使用PictureBox,并在窗体的BackgroundImage中设置图像,并将窗体BackgroundImageLayout设置为Stretch。

In this way you can change the size of form and the image will be stretched automatically. 这样,您可以更改表单的大小,图像将自动拉伸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM