本文介绍了从班级发送bmp到主表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望从主要表格中将一个bmp从一个类发送到图片框。

我认为可以使用委托,但我不知道如何制作它。



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.

推荐答案

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);
        }
    }
}





委托是对方法的引用,基本上你是声明的方法签名。当您使用包含委托的类时,您将传递与签名匹配的自己的方法。它基本上是一种简单的解耦方法。



Valery。



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.


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;
        }
    }
}


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;
        }
    }
}


这篇关于从班级发送bmp到主表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 02:28