如何在C#中将图像从表单传递到另一个 [英] How to pass image from on form to another in C#

查看:73
本文介绍了如何在C#中将图像从表单传递到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我的应用程序中有两个表单,想要将图像从一个表单传递给另一个表单。假设我的第一个表格是注册,其中有一个图片框。而我正在捕捉另一种形式的图像(captureform)。现在我想将捕获的图像从captureform发送到注册表单。没有重新加载注册表。



我尝试过:



这里我先尝试了两件事,将图片框分配给另一个图片框,如

注册frmreg = new注册();

frmreg.imgUpload .Image = imgPreview.Image;



但这对我来说不起作用之后我尝试了带图像参数的方法这里数据即将到来但是无法显示在图片框中喜欢在Captureform中获得


注册frmreg = new注册();

frmreg.DisplayImage(imgPreview.Image);



以图像形式为我创建了一种方法。

hello i have two form in my application and want to pass the image from one form to another form. suppose my first form is registration in which have a picture box. and i am capturing the image i another form(captureform). now i want to send the captured image from captureform to registration form. without reloading the registration form.

What I have tried:

here i have tried two thing first by assign the picture box with another picturebox like
Registration frmreg = new Registration();
frmreg.imgUpload.Image = imgPreview.Image;

but this is not work for me after that i have tried with method with image parameter here data is coming but cant display in picture box like
in Captureform
Registration frmreg = new Registration();
frmreg.DisplayImage(imgPreview.Image);

and in image form i have created a method for it.

public void DisplayImage(Image upload)
     {
         imgUpload.Image = upload;
     }



这里的数据正在上传,但没有显示在我的图片框中


here data is coming in upload but not display in my picturebox

推荐答案

Don每次都要创建一个新实例:重用当前。

这应该有所帮助:在两种形式之间传递信息,第1部分:父母对儿童 [ ^ ]

但是......

添加班级注册变量:

Don't create a new instance each time: reuse the current.
This should help: Transferring information between two forms, Part 1: Parent to Child[^]
But...
Add a class level Registration variable:
private Registration frmReg = null;

然后,当您需要打开表单时,请执行以下操作:

Then, when you need to open the form, do this:

if (frmReg == null)
    {
    frmReg = new Registration();
    frmReg.FormClosing += frmReg_FormClosing;
    }



并添加一个简单的处理程序:


And add a simple handler:

void frmReg_FormClosing(object sender, FormClosingEventArgs e)
    {
    frmReg = null;
    }



那样,你有一个当前实例的记录,如果它关闭了,你下次将它丢弃。

然后,您可以在注册表单中添加一个属性,该表单需要一个图像并将其应用到它:


That way, you have a record of the current instance, and if it's closed, you throw it away for next time.
You can then add a property to the Registration form which takes an Image and app it to it:

if (frmReg != null)
   {
   frmReg.UserPicture = myImage;
   }


为什么不使用 OpenFileDialog类 [ ^ ]用于将图像从文件导入到注册表单图片框控件?



无论如何,此示例有效罚款:



Why don't You use OpenFileDialog class[^] for importing images from files to the Registration form picture box control ?

Any way, this example works fine :

using System;
using System.Windows.Forms;

namespace Pass_image_from_Capture_to_Registration_form
{
	/// <summary>
	/// Description of Registration Form.
	/// 
	/// Registration Form is main ( parrent ) form of application
	/// that contains it's own Registration_Form_Image picture box control
	/// </summary>
	public partial class Registration_Form : Form
	{
		
		// Create new Capture form that contains Image_Preview picture box
		// with initial image as child form of application
		//
		private Capture_Form Capture = new Capture_Form();
		
		public Registration_Form()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// Set image to Registration Form from Capture form
			//
			Registration_Form_Image.Image = Capture.Image_Preview.Image;
		}
	}
}





记得设置Capture Form图片的修饰符属性箱子控制到公众所以它可以通过注册表格进行修改或访问。



一切顺利,
$ b $bŽeljkoPerić



Remember to set modifiers property of Capture Form picture box control to public so it can be modified or accessed from the Registration form.

All the best,
Željko Perić


我在撰写SafeImage文章时发现的有关图像的内容 - 如果将图像加载到表单上的表单或控件中,当表单关闭时,该图像将被丢弃。如果你试图将该图像传递到其他地方,那么你就会遇到麻烦。你之前需要复制它。

Something I discovered about images when I was writing my SafeImage article - If you load an image into a form or control on the form, when the form closes, that image gets disposed. If you are trying to pass that image off somewhere else, you're going to get into trouble. You do need to make a copy of it before you pass it off.
Bitmap copy = Bitmap.FromImage(imgPreview.Image);
frmReg.UploadImage.Image = copy;



如果你不这样做,那么当关闭captureForm时,图像将被处理,并且所有种类奇怪的东西可能会开始发生。


If you don't do that, then when captureForm is closed, the image will be disposed, and all sorts of weird stuff is likely to start happening.


这篇关于如何在C#中将图像从表单传递到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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