Click here to Skip to main content
15,909,091 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I want send a bmp from a class to picturebox in main form.
I think that may use delegate, but i don´t know how make it.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        class clsLoadBmp
        {
            public void LoadBmp()
            {
                Bitmap bmp = (Bitmap)Image.FromFile("c:\\test.jpg");
                UpdatePictureBox(bmp);
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            clsLoadBmp n = new clsLoadBmp();
            n.LoadBmp();
        }

        public void UpdatePictureBox(Bitmap bmp)
        {
            this.pictureBox1.Image = bmp;
        }
    }
}


Can you help me?

Thanks in advance.
Posted
Comments
Sergey Alexandrovich Kryukov 1-Jul-14 16:51pm    
The request makes little sense. What do you really want? "From class to a picturebox"? And what, PictureBox is not a class, or what? You work not with classes, but instances (objecT) To show a bitmap on request? But what's the problem then? Where is the code which is supposed to "send" anything? What don't you know?

In all cases, you are not "sending" anything; this is the same process, same application. Also, there are no cases when hard-coded file paths could be useful (like "c:\test.jpg"). In newer version of Windows, 7 or 8, this path is not even legitimate.

—SA

Hello,

First I would recommend you read about delegates, a good start is MSDN:
http://msdn.microsoft.com/en-us/library/orm-9780596521066-01-17.aspx[^]

Anyway here is a way to do it using delegates:

public partial class Form1 : Form
{	
	public Form1()
	{
		InitializeComponent();
	}
	
	static void Loader(PictureBox pict)
    	{
        	Bitmap bmp = (Bitmap)Image.FromFile("c:\\test.jpg");
	        pict.Image = bmp;
    	}		
	
	void button1_Click(object sender, EventArgs e)
	{
		LoadBmpHelper myLoader = new LoadBmpHelper();
		LoadBmpHelper.PictureHandler loader = new LoadBmpHelper.PictureHandler(Loader);
        	myLoader.LoadBmp(loader, pictureBox1);
	}
}

public class LoadBmpHelper
{
    public delegate void PictureHandler(PictureBox pict);

    public void LoadBmp(PictureHandler pictureHandler, PictureBox pict)
    {
        if (pictureHandler != null)
        {
            pictureHandler(pict);
        }
    }
}


A delegate is a reference to a method, basically you declare a method signature. When you use the class that contains the delegate you pass it your own method that matches the signature. It's basically an easy way to decouple class.

Valery.
 
Share this answer
 
Hello



C#
using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        class clsLoadBmp
        {
            public Bitmap LoadBmp()
            {
                Bitmap bmp = (Bitmap)Image.FromFile("c:\\test.jpg");
                return bmp;
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            clsLoadBmp n = new clsLoadBmp();
            UpdatePictureBox(n.LoadBmp());
        }

        public void UpdatePictureBox(Bitmap bmp)
        {
            this.pictureBox1.Image = bmp;
        }
    }
}
 
Share this answer
 
Thanks to Valery. Your solution is not good for me, but your link was very usefull. This put me in the correct way. This is the code working:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        class clsLoadBmp
        {
            public delegate void BmpChangeHandler(Bitmap bmp);
            public BmpChangeHandler BmpChanged;

            public void LoadBmp()
            {

                Bitmap bmp = (Bitmap)Image.FromFile("c:\\test.jpg");
                BmpChanged(bmp);
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            clsLoadBmp n = new clsLoadBmp();
            n.BmpChanged += new clsLoadBmp.BmpChangeHandler(UpdatePictureBox);
            n.LoadBmp();
        }

        public void UpdatePictureBox(Bitmap bmp)
        {
            this.pictureBox1.Image = bmp;
        }
    }
}
 
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