日期:2014-05-17  浏览次数:21119 次

C# PictureBox自适应尺寸
我的PictureBox中有图片,我想要当图像过大时,我点击自适应尺寸按钮,图片能完全显示在PictureBox中且图片不变形。可是按照我的代码只是图片按照要求显示了,但是实际尺寸并没有变化,我想让pictureBox中的图片的尺寸就是窗口显示的那么大尺寸,而pictureBox的尺寸等于里面图片尺寸变化后的尺寸。我的关键代码:
C# code
     //自适应尺寸
        private void adaptButton_Click(object sender, EventArgs e)
        {
            //获取屏幕的宽度和高度
            int w = Screen.PrimaryScreen.Bounds.Width;
            int h = Screen.PrimaryScreen.Bounds.Height;
            
            ddoc.pictureBox1.Location = new Point(0, 0);
            ddoc.pictureBox1.Size = new Size(w, h);

            ddoc.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
        }



------解决方案--------------------
C# code

string strWelcome = System.Windows.Forms.Application.StartupPath + "\\welcome.jpg";
if (File.Exists(strWelcome))
{
    Bitmap bm = new Bitmap(Image.FromFile(strWelcome), ddoc.pictureBox1.Width, ddoc.pictureBox1.Height);
    bm.Save("filename");
}