【问题标题】:How can I adapt the code in dll to the main code?如何使 dll 中的代码适应主代码?
【发布时间】: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,由于下面的命令,我的部分问题得到了解决,我的代码仍然存在一些问题,并试图通过最后的建议来解决这个问题。谢谢你回答。

标签: c# dll


【解决方案1】:

DoGray 函数是私有的。为了使它在 Class1 之外可见,它必须是 public。此外,您应该将其设为static

public static Bitmap DoGray(Bitmap bmp)

下一步是引用 gdll dll(带有 DoGray 函数的那个​​),需要从 WinForms 项目(带有 Form1.cs 文件的那个)中引用。 最简单的方法是将两个项目(WinForms 项目和 dll 项目)放在同一个解决方案中,并使用一个项目reference

然后右键单击您的 WinForms 项目(包含您的 Form1.cs 文件的项目)并选择“Add Reference...

在“Reference Manager”中选择左侧的“Projects”并检查你的gdll(带有Class1.cs文件的那个)并点击ok。

现在你可以使用 DoGray 函数了:

private void button2_Click(object sender, EventArgs e)
{
    Bitmap image= new Bitmap(pictureBox1.Image);
    Bitmap gray = gdll.Class1.DoGray(image);
    pictureBox2.Image = gray;
}

【讨论】:

  • 非常感谢您的全面解释。您编写的代码在我添加 System.Drawing.Common 时有效,但我仍然有一点问题。现在正在编译,但是当我尝试推送button2 我收到此错误=>Exception Unhandled:System.IO.FileNotFoundException: 无法加载文件或程序集 ''System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 或其依赖项之一。系统找不到指定的文件。'
  • @n_stckwff 编译后 gdll.dll 必须与 WinForms 应用程序位于同一目录中。如果不是这种情况,启动 WinForms 应用程序时将找不到 gdll.dll。所以你应该将gdll.dll项目的输出路径设置为与WinForms项目相同的文件夹(docs.microsoft.com/en-us/visualstudio/ide/…)。
猜你喜欢
  • 1970-01-01
  • 2021-12-23
  • 1970-01-01
  • 2012-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-09
  • 1970-01-01
相关资源
最近更新 更多