C# "The name 'pictureBox1' does not exist in the current context."

Asked 2 years ago, Updated 2 years ago, 50 views

using System;
using System.Collections.General;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Drawing;

namespace WindowsFormsApplication1
{
    static class program
    {

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STATHREAD]
        static void Main()
        {

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(newForm1());

            US>//#string URL="http://www.example.jp/images/banana_i01.jpg";
            string path = Path.GetTempFileName();

            WebClient client=new WebClient();
            client.DownloadFile (URL, path);

            string oldPath=path;
            string newPath=Path.ChangeExtension(oldPath, ".jpg");

            File.Move(oldPath, newPath);

            Bitmap canvas = new Bitmap (pictureBox1.Width, pictureBox1.Height);

            Graphics g=Graphics.FromImage(canvas);

            Image img = Image.FromFile (newPath);
            g.DrawImage(img, 10, 20, img.Width, img.Height);
            img.Dispose();

            g.Dispose();

            pictureBox1.Image=canvas;


        }
    }
}

C#I'm a beginner.

I'm trying to write a program to download the image from the URL and display it in the dialog.

However, the name 'pictureBox1' appears as not in the current context.

What are the possible causes of this?

I would appreciate it if you could let me know.

c#

2022-09-30 19:26

2 Answers

The location to paste the referenced source code is incorrect.
PictureBox must be pasted onto the deployed form.

If you read the reference site carefully, wouldn't there be a description like "PictureBox" on the form?

It seems like you don't know the right or left yet, so I recommend you to buy a simple reference book and try it out.


2022-09-30 19:26

The Application.Run()method contains

Start running the standard application message loop on the current thread and display the specified form.

It only says up to , but it actually shows the form and does not return until it is closed.This means that the next line will be after the form is closed.

When do you expect to execute the added code? (for example, when a button is pressed) should be in the appropriate location.


2022-09-30 19:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.