当我收听PictureBox的Resize事件并给PictureBox提供新图像时,它不会调整大小

时间:2019-07-19 06:30:26

标签: c# winforms

我正在PictureBox上绘制图形,并且由于在调整大小时图像应该更改,因此我正在监听PictureBox的Resize事件。当我播放应用程序时,图像没有变化。它不会调整大小,而是固定在所有方向上。

我试图处置所有资源并重新分配它们,我尝试使用图像初始化的调整大小,但是没有任何效果

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CollisionTest {
    public partial class Form1 : Form {
        private Bitmap drawingBitmap;
        private Graphics canvasGraphics;

        public Graphics CanvasGraphics { get => canvasGraphics; }

        public Form1() {
            InitializeComponent();
        }

        private void CanvasResize(object sender, EventArgs e) {
            label1.Text = $"{Canvas.Width}, {Canvas.Height}";
            Canvas.Image.Dispose();
            // I think problem's here
            Canvas.Image = new Bitmap(Canvas.Width, Canvas.Height);
            canvasGraphics.Dispose();
            canvasGraphics = Graphics.FromImage(Canvas.Image);
            canvasGraphics.Clear(Color.FromArgb(255, 255, 255));
        }


        private void Form1_Load(object sender, EventArgs e) {
            Canvas.Image = new Bitmap(Canvas.Width, Canvas.Height);
            canvasGraphics = Graphics.FromImage(Canvas.Image);
            canvasGraphics.Clear(Color.FromArgb(255, 255, 255));
            Canvas.Resize += CanvasResize;
        }
    }
}

我希望PictureBox可以调整大小,但是PictureBox的大小没有变化。

0 个答案:

没有答案