【发布时间】:2021-09-13 11:11:16
【问题描述】:
我正在尝试在 dll 中找到我的函数,但我是使用 dll 和划分代码的新手。 dll中的代码是:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace gdll
{
public class Class1
{
private Bitmap DoGray(Bitmap bmp)
{
for (int i = 0; i < bmp.Height - 1; i++)
{
for (int j = 0; j < bmp.Width - 1; j++)
{
int value = (bmp.GetPixel(j, i).R + bmp.GetPixel(j, i).G + bmp.GetPixel(j, i).B) / 3;
Color clr;
clr = Color.FromArgb(value, value, value);
bmp.SetPixel(j, i, clr);
}
}
return bmp;
}
}
}
但是form1.cs中的代码应该怎么写。 Form1.cs[Design]有两个按钮和2个pictureBox。第一个按钮是原图,第二个是过滤后的图片。我写了没有dll的代码版本(我把函数写到同一页)下面:
private void button2_Click(object sender, EventArgs e)
{
Bitmap image= new Bitmap(pictureBox1.Image);
Bitmap gray = DoGray(image);
pictureBox2.Image = gray;
}
当然,在 dll 文件中运行时它不起作用。
这是 form1.cs 中的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using gdll;
namespace dnmimage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog sfd = new OpenFileDialog();
sfd.Filter = "Image Files|*.bmp|All Files|*.*";
sfd.InitialDirectory = ".";
if (sfd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
{
return;
}
pictureBox1.ImageLocation = sfd.FileName;
}
private void button2_Click(object sender, EventArgs e)
{
Bitmap image= new Bitmap(pictureBox1.Image);
Bitmap gray = DoGray(image);
pictureBox2.Image = gray;
}
【问题讨论】:
-
究竟是什么不工作?它编译吗?它会抛出任何错误吗?我最好的猜测是,您没有将 dll 复制到构建目录
-
不,我添加了 dll。这与它无关。它没有被编译,但这是正常的,因为我应该编写引用 dll 代码的代码,但这就是重点。我不知道如何要做。我不知道如何在这两个文件之间创建连接。
-
我猜你正在使用 VisualStudio?如果是这样,您可以将对 dll 的引用添加到您的项目中。为此,请右键单击您项目的解决方案资源管理器。寻找“添加参考”。然后你可以选择你的 dll
-
是的,我正在使用 Visual Studio,由于下面的命令,我的部分问题得到了解决,我的代码仍然存在一些问题,并试图通过最后的建议来解决这个问题。谢谢你回答。