[英]how can I reach the value from another class coming from textbox when I click the button in C# Windows form?
I am trying to write a code for changing contrast.我正在尝试编写更改对比度的代码。 I create a class for this but I should get the value from texbox and write the value for the process in
Contrast.cs
but I could not get that.我为此创建了一个 class 但我应该从 texbox 获取值并将该过程的值写入
Contrast.cs
但我无法得到。 How can I send the value to the Contrast.cs
when I click to the btn_contrast
?当我单击
btn_contrast
时,如何将值发送到Contrast.cs
?
PS:There are some instances like for sending it from form to another form but I could not find any instance about sending it to class and using it. PS:有一些例子,比如将它从一个表单发送到另一个表单,但我找不到任何关于将它发送到 class 并使用它的实例。
Here is the code for Form1:这是Form1的代码:
using AForge.Video.DirectShow;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace imageprocessing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public FilterInfoCollection devices;
public VideoCaptureDevice camera;
private void Form1_Load(object sender, EventArgs e)
{
devices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo item in devices)
{
cb_show.Items.Add(item.Name);
}
camera = new VideoCaptureDevice();
cb_show.SelectedIndexChanged += cb_show_SelectedIndexChanged;
}
private void cb_show_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (camera.IsRunning == false)
{
camera = new VideoCaptureDevice(devices[cb_show.SelectedIndex].MonikerString);
camera.NewFrame += Camera_NewFrame;
camera.Start();
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message + "");
}
}
List<byte> alldata = new List<byte>();
public void Camera_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
{
Bitmap image = (Bitmap)eventArgs.Frame.Clone();
byte[] maindata = new byte[image.Width * 3];
if (btn_grayWasClicked == true)
{
alldata.Clear();
for (int i = 0; i < image.Height; i++)
{
int count = 0;
for (int j = 0; j < image.Width; j++)
{
Color color = image.GetPixel(j, i);
maindata[count] = color.R;
maindata[count + 1] = color.G;
maindata[count + 2] = color.B;
count += 3;
}
byte[] processGray = Gray.GrayFilter(maindata, image.Width);
alldata.AddRange(processGray);
}
}
}
private bool btn_contrastWasClicked = false;
private void btn_contrast_Click(object sender, EventArgs e)
{
btn_contrastWasClicked = true;
}
Here is the code for Contrast.cs这是 Contrast.cs 的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace imageprocessing
{
class Contrast
{
public static byte[] Contrast(byte[] data, int width)
{
List<byte> alldataa = new List<byte>();
double cont =(100.0 + (Convert.ToInt32()-100))/100.0)*(100.0 + (Convert.ToInt32() - 100)) / 100.0);
for (int i = 0; i < width - 1; i += 3)
{
data[i]=((((data[i]/255)-0.5)*cont)+0.5)*255;
if (data[i]<0)
{
data[i] = 0;
}
if(data[i]>255)
{
data[i] = 255;
}
}
return data;
}
}
}
I need the value that is coming from tb_contrast in this code:我需要此代码中来自 tb_contrast 的值:
double cont =(100.0 + (Convert.ToInt32()-100))/100.0)*(100.0 + (Convert.ToInt32() - 100)) / 100.0);
Keep the value in Session
, then use it wherever you want.保留
Session
中的值,然后在任何地方使用它。
First Make your class public
and the create a object for your class in Form1 like below:首先
public
您的 class 并在 Form1 中为您的 class 创建一个 object,如下所示:
Contrast con = new Contrast();
now you can call your class methods wherever you wan.现在您可以在任何地方调用您的 class 方法。
private void btn_contrast_Click(object sender, EventArgs e)
{
btn_contrastWasClicked = true;
con.Contrast('SomeVal','Your Required TextBoxValue');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.