Home > Mobile >  C# sizing picturebox to fit the fullscreen version of the window
C# sizing picturebox to fit the fullscreen version of the window

Time:02-26

I don't want to change the image size. I added this feature after I created my image. I am using the Windows Forms App (.NET framework). I made stuff using the paint feature, with coordinates and stuff. And I now anted to change it so it gets converted so it's in a picturebox to getter better quality on my animations. But I can only size the picturebox to the size of the small window. But I have based everything off of the full sized window. And now I can only see the part of the painting that fits in the small window which is the stuff in the top left. I need the picturebox to be as big as the full size window Here is my code:

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;
using System.Drawing.Drawing2D;

namespace France
{
    public partial class Form1 : Form
    {
        int cloudone1, cloudone2, cloudone3, cloudtwo1, cloudtwo2, cloudtwo3, cloudthree1, cloudthree2, cloudthree3;
        Bitmap bmp;
        public Form1()
        {
            InitializeComponent();
            cloudone1 = 0;
            cloudone2 = 50;
            cloudone3 = 100;
            cloudtwo1 = 400;
            cloudtwo2 = 450;
            cloudtwo3 = 500;
            cloudthree1 = 800;
            cloudthree2 = 850;
            cloudthree3 = 900;
            bmp = new Bitmap(this.Width, this.Height);
            this.FormBorderStyle = FormBorderStyle.None;
            pictureBox1.Dock = DockStyle.Fill;
            this.WindowState = FormWindowState.Maximized;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.Transparent);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            Pen BlackP = new Pen(Color.Black, 4);
            PointF[] Triangle = new PointF[4];
            g.FillRectangle(Brushes.LightBlue, 0, 0, 1280, 660);
            g.FillRectangle(Brushes.Green, 0, 500, 1280, 660);
            g.FillEllipse(Brushes.Yellow, 1100, 50, 100, 100); // Sun (middle of sun is 1150, 100)
            g.DrawLine(Pens.Yellow, 1075, 100, 1000, 100);
            g.DrawLine(Pens.Yellow, 1225, 100, 1300, 100);
            g.DrawLine(Pens.Yellow, 1150, 175, 1150, 250);
            g.DrawLine(Pens.Yellow, 1150, 25, 1150, 0);
            g.DrawLine(Pens.Yellow, 1100, 150, 1050, 200);
            g.DrawLine(Pens.Yellow, 1200, 150, 1250, 200);
            g.DrawLine(Pens.Yellow, 1100, 50, 1050, 4);
            g.DrawLine(Pens.Yellow, 1200, 50, 1250, 4); // Sun rays
            g.DrawLine(BlackP, 30, 550, 65, 470); // Eiffel Bottom Left Outer Line
            g.DrawLine(BlackP, 50, 550, 85, 470); // Eiffel Bottom Left Inner Line
            g.DrawLine(BlackP, 70, 459, 85, 409); // Eiffel Middle Left Outer Line
            g.DrawLine(BlackP, 90, 459, 105, 409); // Eiffel Middle Left Inner Line
            g.DrawLine(BlackP, 107, 400, 110, 368); // Eiffel Top Left Inner Line
            g.DrawLine(BlackP, 88, 400, 100, 325); // Eiffel Top (Bottom Half) Left Outer Line
            g.DrawLine(BlackP, 100, 325, 105, 250); // Eiffel Top (Top Half) Left Outer Line

            g.DrawLine(BlackP, 190, 550, 155, 470); // Eiffel Bottom Right Outer Line
            g.DrawLine(BlackP, 170, 550, 135, 470); // Eiffel Bottom Right Inner Line
            g.DrawLine(BlackP, 150, 459, 135, 409); // Eiffel Middle Right Outer Line
            g.DrawLine(BlackP, 130, 459, 115, 409); // Eiffel Middle Right Inner Line
            g.DrawLine(BlackP, 113, 400, 110, 368); // Eiffel Top Right Inner Line
            g.DrawLine(BlackP, 132, 400, 120, 325); // Eiffel Top (Bottom Half) Right OuterLine
            g.DrawLine(BlackP, 120, 325, 115, 250); // Eiffel Top (Top Half) Right Outer Line

            g.DrawLine(BlackP, 60, 485, 160, 485); // Eiffel Bottom Horizontal Line
            g.DrawLine(BlackP, 80, 424, 140, 424); // Eiffel Middle Horizontal Line
            g.DrawLine(BlackP, 100, 260, 120, 260); // Eiffel Top (Bottom) Horizontal Line
            g.DrawLine(BlackP, 100, 250, 120, 250); // Eiffel Top (Top) Horizontal Line

            g.DrawLine(BlackP, 110, 250, 110, 220); // Eiffel Top Vertical Line

            g.DrawRectangle(BlackP, 27, 550, 23, 15); // Eiffel Bottom Left Base Box
            g.DrawRectangle(BlackP, 170, 550, 23, 15); // Eiffel Bottom Right Base Box
            g.DrawRectangle(BlackP, 60, 459, 100, 11); // Eiffel Bottom/Middle Seperator Box
            g.DrawRectangle(BlackP, 80, 400, 60, 9); // Eiffel Middle/Top Seperator Box

            g.DrawArc(BlackP, 67, 495, 86, 35, 180, 180); // Eiffel Bottom Arch

            g.DrawLine(BlackP, 170, 550, 175, 515); // Bottom Right Crosses
            g.DrawLine(BlackP, 175, 515, 142, 485); //
            g.DrawLine(BlackP, 190, 550, 155, 515); //
            g.DrawLine(BlackP, 155, 515, 160, 485); //

            g.DrawLine(BlackP, 50, 550, 45, 515); // Bottom Left Crosses
            g.DrawLine(BlackP, 45, 515, 78, 485); //
            g.DrawLine(BlackP, 30, 550, 65, 515); //
            g.DrawLine(BlackP, 65, 515, 60, 485); // ____

            g.DrawLine(BlackP, 70, 459, 95, 440); // Middle Left Crosses
            g.DrawLine(BlackP, 95, 440, 80, 425); //
            g.DrawLine(BlackP, 90, 459, 75, 437); //
            g.DrawLine(BlackP, 75, 437, 100, 426); // ____

            g.DrawLine(BlackP, 150, 459, 125, 440); // Middle Right Crosses
            g.DrawLine(BlackP, 125, 440, 140, 425); //
            g.DrawLine(BlackP, 130, 459, 145, 440); //
            g.DrawLine(BlackP, 145, 440, 120, 425); // ____

            g.DrawLine(BlackP, 88, 400, 124, 345); // Top crosses
            g.DrawLine(BlackP, 132, 400, 96, 345); //
            g.DrawLine(BlackP, 96, 345, 120, 320); //
            g.DrawLine(BlackP, 124, 345, 100, 320); //
            g.DrawLine(BlackP, 100, 320, 118, 295); //
            g.DrawLine(BlackP, 120, 320, 102, 295); //
            g.DrawLine(BlackP, 102, 295, 118, 275); //
            g.DrawLine(BlackP, 118, 295, 102, 275); //
            g.DrawLine(BlackP, 102, 275, 118, 260); //
            g.DrawLine(BlackP, 118, 275, 102, 260); // ____
            g.FillEllipse(Brushes.LightGray, cloudone1, 10, 75, 50); // Cloud one
            g.FillEllipse(Brushes.LightGray, cloudone2, 20, 100, 50); //
            g.FillEllipse(Brushes.LightGray, cloudone3, 5, 100, 50); //  ____
            g.FillEllipse(Brushes.LightGray, cloudtwo1, 55, 75, 50); // Cloud two
            g.FillEllipse(Brushes.LightGray, cloudtwo2, 45, 100, 50); //
            g.FillEllipse(Brushes.LightGray, cloudtwo3, 50, 100, 50); //  ____
            g.FillEllipse(Brushes.LightGray, cloudthree1, 45, 75, 50); // Cloud three
            g.FillEllipse(Brushes.LightGray, cloudthree2, 40, 100, 50); //
            g.FillEllipse(Brushes.LightGray, cloudthree3, 35, 100, 50); //  ____

            Triangle[0] = new PointF(1100, 200);
            Triangle[1] = new PointF(1175, 50);
            Triangle[2] = new PointF(1250, 200);
            Triangle[3] = new PointF(1100, 200);
            //g.FillPolygon(Brushes.Purple, Triangle);
            pictureBox1.Image = bmp;
            g.Dispose();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            cloudone1 = cloudone1   2;
            if (cloudone1 == 1000)
            {
                cloudone1 = 0;
            }
            Invalidate();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

CodePudding user response:

to full size on picturebox use this code this.WindowState = FormWindowState.Maximized; , It is better to use this symbol as well, in order to fill the image into the window pictureBox1.Dock = DockStyle.Fill; The window must also be none border this.FormBorderStyle = FormBorderStyle.None; Use them in the following order

  1. this.FormBorderStyle = FormBorderStyle.None;
  2. pictureBox1.Dock = DockStyle.Fill;
  3. this.WindowState = FormWindowState.Maximized;

CodePudding user response:

to stop image effect use this

first go to Form_Load and put this code:

  • this.KeyPreview = true;

after this go to Form Event and double click in KeyDown and put this code to stop the moving image

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
      if (e.KeyCode == Keys.Space)
      {
          timer1.Enabled = false;
      }
}

if you press Space will stop the timer, and stop the image

if you want to stop the image like auto use this

private void timer1_Tick(object sender, EventArgs e)
        {
            cloudone1 = cloudone1   2;
            if (cloudone1 == 1000)
            {
                timer.Enable = false;
            }
            Invalidate();
        }
  • Related