Click here to Skip to main content
16,004,602 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have taken input of an image in form1 and want to process it in another form2. I have to pass form1_User_Input_Image in form2_first underlined Line of code.
Please help me to do this.

Form1
//browse image in pictureBox1 Click
private void pictureBox1_Click(object sender, EventArgs e)
{
    OpenFileDialog opf = new OpenFileDialog();
    opf.Filter = "Choose Image(*.jpg; *.png; *.gif)|*.jpg; *.png; *.gif";
    if (opf.ShowDialog() == DialogResult.OK)
    {
        pictureBox1.Image = Image.FromFile(opf.FileName);
    }
}


Form2

C#
{
Image img = Image.FromFile(img);
int widthThird = (int)((double)img.Width / 25.0 + 0.5);
int heightThird = (int)((double)img.Height / 20.0 + 0.5);
Bitmap[,] bmps = new Bitmap[20, 25];
for (int i = 0; i < 20; i++)
    for (int j = 0; j < 25; j++)
    {
        bmps[i, j] = new Bitmap(widthThird, heightThird);
        Graphics g = Graphics.FromImage(bmps[i, j]);
        g.DrawImage(img, new Rectangle(0, 0, widthThird, heightThird), new Rectangle(j * widthThird, i * heightThird, widthThird, heightThird), GraphicsUnit.Pixel);
        g.Dispose();
    }
Posted
Comments
Sergey Alexandrovich Kryukov 19-May-15 13:35pm    
What's the problem? Why do you use PictureBox at all?
Why the second code fragment shows rendering on the image in another image? Why not rendering it in Paint (or OnPaint)?
The problem is simple like hell, it's possible that you are over-complicated it.
—SA

If you want to catch the Picture, which is shown on Form1, to another PictureBox, which is placed on Form2, you have to build an Object-Reference to Form1 to access the PictureBox on it.
 
Share this answer
 
Comments
Member 11657542 20-May-15 1:28am    
Yeh Ralf Meier I have done this. Thanks
You are not actually reusing the bitmap. In second form, you create another bitmap from file. So, all your question about collaboration between forms is totally pointless, the problem is not explained. The code looks like something unacceptable. You hard-code the image size and other figures, use the hard-coded values twice, which makes the code unsupportable. Drawing in an image for showing the image on the form is pointless. If this is what you need, you just need to use Graphics.DrawImage on some control (even on the form itself) in the handler of the event Control.Paint or overridden method Control.OnPaint.

If that's all you need, you also don't need this redundant control PictureBox. (Sorry if this advice is redundant; this is because I don't know where do you use it and how, so, consider this advice as as "just in case" one.) Please see my past answers:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

For more information on graphics and image rendering, please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^].

On form collaboration issues, please see my article: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900