从班级发送bmp到主表格 [英] Send a bmp to main Form from a class

查看:63
本文介绍了从班级发送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.

推荐答案

你好,



首先我建议你阅读有关代表的一个好的开始是MSDN:

http://msdn.microsoft.com/en-us/library/orm-9780596521066-01-17.aspx [ ^ ]



无论如何这里使用代表是一种方法:



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





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



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.


你好







Hello



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


感谢Valery。你的解决方案对我不好,但你的链接非常有用。这让我以正确的方式。这是代码工作:



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


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆