A THOUSAND apologies if this question has been asked time and time again.

Google gave no results and Jeeves is retired so I come looking for help in the best programming forum around.

private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.ImageLocation=???????????
        }

My question is fairly simple, what do I put at the other side of the "="?

Dani AI

Generated

ImageLocation expects a string path or a URL. called that out, and the rest of the thread shows other ways to set Image. If you want to stick with ImageLocation, set the path and then call Load() (or LoadAsync() so the UI does not freeze). Use an absolute or a reliable app-relative path.

var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "images", "photo.png");

if (File.Exists(path))
{
    pictureBox1.ImageLocation = path;
    pictureBox1.Load();            // or: pictureBox1.LoadAsync();
}
else
{
    MessageBox.Show("Image not found: " + path);
}

Remote image? Same idea, but use async:

pictureBox1.ImageLocation = "https://example.com/photo.jpg";
pictureBox1.LoadAsync();

One common pitfall with the Image property (see and ’s approach) is file locking and leaks. Before assigning a new image, dispose the old one; and if you load from disk, clone the image so the file is not locked by GDI+.

// Dispose previous image to avoid memory leaks
if (pictureBox1.Image != null)
{
    var old = pictureBox1.Image;
    pictureBox1.Image = null;
    old.Dispose();
}

// Load without locking the file
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var img = Image.FromStream(fs))
{
    pictureBox1.Image = new Bitmap(img); // clone breaks the file lock
}

Quick tips: set pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; for nice scaling. If you want zero file-path headaches, and ’s idea of embedding images as project resources is solid. For smoother UX when loading from the web, use LoadAsync() and handle LoadCompleted to update the UI.

Recommended Answers

All 8 Replies

either a path or a url to the image file

Image.FromFile(/*put your path here*/);

Or you might open a filedialog in your buttonclick like this :

OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Open bitmap or jpeg.";
//dlg.Filter = "jpg files (*.jpg);*.jpg;*.* | bmp files (*.bmp); *.bmp";

 if (dlg.ShowDialog() == DialogResult.OK)
 {
         this.fotoPictureBox.Image = new Bitmap(dlg.OpenFile());
 }
 dlg.Dispose();

Several ways to do it, here is an article on . Just make sure to dispose of new objects properly depending on how you load the images

pictureBox1.Image = new Bitmap("D:\\2.png");

or if picture in resources folder then
//pictureBox1.Image = Properties.Resources.PictureName;
i.e pictureBox1.Image = Properties.Resources.img3;

public partial class Form1 : Form

{

  private Bitmap Bmp;

  private Point BmpLoc;

  public Form1()

  {

    InitializeComponent();

    this.button1.Click += new System.EventHandler(this.button1_Click);

    this.Paint += new System.Windows.Forms.PaintEventHandler(Form1_Paint);

  }

  private void button1_Click(object sender, EventArgs e)

  {

    Bmp = new Bitmap("C:\\test.jpg");

    BmpLoc = new Point(10, 10);

    Rectangle R = new Rectangle(BmpLoc, Bmp.Size);

    this.Invalidate(R);

  }

  private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)

  {

    if (Bmp != null)

    {

      e.Graphics.DrawImage(Bmp, BmpLoc);

    }

  }

}

I would add the image to your project resources and call it like so:

pictureBox1.Image = Properties.Resources.image;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.