i place two forms one contain 2 picturebox and 2 button and 2nd form contain 2 picturebox and one button
when insert image in 1st form it automaticaly load on 2nd form when 2nd form load.
plzzzzzzzzzzzzzz help me to do this

Dani AI

Generated

As demonstrated, passing the image information to Form2 and loading it there is a simple, effective solution that solved 's original request. A few practical improvements make that pattern more reliable in real projects: avoid Image.FromFile (it keeps the file handle open), always dispose previous Image objects to prevent GDI handle leaks, and prefer passing an Image (or a byte[]/MemoryStream) instead of a file path when the image may change or the file might be removed.

A compact VB.NET pattern that avoids file locks and cleans up resources:

' Form1: load without locking and pass a safe copy
Dim path As String = openFileDialog1.FileName
If String.IsNullOrEmpty(path) Then Exit Sub

Dim img As Image
Using fs As New IO.FileStream(path, IO.FileMode.Open, IO.FileAccess.Read)
    img = Image.FromStream(fs)
End Using

Dim copy As Image = New Bitmap(img)  ' detach from original stream
img.Dispose()
PictureBox1.Image = copy

Dim f2 As New Form2(copy)
f2.Show()
' Form2: take ownership of a clone and dispose on close
Public Class Form2
    Private ReadOnly _img As Image

    Public Sub New(ByVal img As Image)
        InitializeComponent()
        If img IsNot Nothing Then _img = CType(img.Clone(), Image)
    End Sub

    Private Sub Form2_Load(...) Handles MyBase.Load
        PictureBox1.Image = _img
    End Sub

    Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
        If PictureBox1.Image IsNot Nothing Then
            PictureBox1.Image.Dispose()
            PictureBox1.Image = Nothing
        End If
        MyBase.OnFormClosed(e)
    End Sub
End Class

Common pitfalls and tips:

  • "Generic GDI+ error" when saving usually means the image is still in use — clone or dispose before saving.
  • When updating an already-open Form2, expose a SetImage(img As Image) method (clone inside) instead of reopening the form.
  • Always null-check and wrap file access in Try/Catch.
  • For consistent display, set PictureBox.SizeMode = PictureBoxSizeMode.Zoom.

These notes extend ’s approach with safer resource handling and are suitable when the page is viewed later or the image source changes.

Recommended Answers

All 3 Replies

when you show the 2nd form pass the path of loaded image in 2nd form and in form2_load event load that image in picturebox

as

//code in form 1
 String strFileName = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Image FIles (*.jpg)|*.jpg|All files (*.*)|*.*";
            //openFileDialog1.InitialDirectory = @"d:/";
            openFileDialog1.Title = "Select an image file"; 
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
                strFileName = openFileDialog1.FileName;
          
            if (strFileName != "")
                pictureBox1.Image = Image.FromFile(strFileName);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form2 obj = new Form2(strFileName);
            obj.Show();
        }

//code in form 2

 String PathOfImage = "";
        public Form2(String Path)
        {
            InitializeComponent();
            PathOfImage = Path;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            pictureBox1.Image = Image.FromFile(@PathOfImage);
        }
commented: Continous effort. +14
commented: Godd Logic +3

Thankes alot realy thankes thankes alot

You are welcome :) if your problem is solved please mark this thread solved

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.