2

When I size up a pictureBox to be really big, I get a "System.AccessViolationException" exception. But I can't seem to avoid it with a try / catch... How can I get this to not throw an exception?

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pic.Image = Image.FromFile(@"D:\barren.jpg");
            pic.SizeMode = PictureBoxSizeMode.StretchImage;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                pic.Width = 30000;
                pic.Height = 35000;
            }
            catch { MessageBox.Show("catch"); }
        }
spunit
  • 523
  • 2
  • 6
  • 23
  • 1
    Looks like the code doing the resizing is failing in unmanaged code. There's not a lot you can do about this. – Sean Apr 11 '20 at 11:23
  • Hmmm, don't really understand that much. So there's really no way to not make it crash? – spunit Apr 11 '20 at 11:37
  • 1
    You're trying to make the `PictureBox` a ridiculously large size, which is causing the native code (GDI+) underlying it to fail, hence the `AccessViolationException`. About the only thing you can do is avoid specifying such huge values - you can catch an `AccessViolationException`, but you really shouldn't. – Ian Kemp Apr 11 '20 at 22:15
  • Ok, thanks for your help everybody. – spunit Apr 12 '20 at 00:48

0 Answers0