【问题标题】:CS1503 Cannot convert Matrix to MatrixOrderCS1503 无法将矩阵转换为 MatrixOrder
【发布时间】:2019-12-21 08:24:28
【问题描述】:

我对 C# 编程很陌生。我想在 MVS 中对附件中的矩阵计算代码运行一个简单的测试。

但是我得到了以下错误:

1) 错误 CS1503 参数 2:无法从 'System.Drawing.Drawing2D.Matrix' 转换为 'System.Drawing.Drawing2D.MatrixOrder'

2) 错误 CS0019 运算符“*”不能应用于“矩阵”和“矩阵”类型的操作数

我曾尝试寻找解决方案,但无法解决此错误。

目标框架:.Net Framework 4.7.2 输出类型:控制台应用程序

请指教。 谢谢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Drawing.Drawing2D;

namespace Project2

{
    public class MatrixCalculation
    {
        static void Main()
        {
        }

        private void MultiplicationExample()
        {
            Matrix matrix1 = new Matrix(5, 10, 15, 20, 25, 30);
            Matrix matrix2 = new Matrix(2, 4, 6, 8, 10, 12);

            // matrixResult is equal to (70,100,150,220,240,352) 
          Matrix matrixResult = Matrix.Multiply(matrix1, matrix2);

            // matrixResult2 is also
            // equal to (70,100,150,220,240,352) 
            Matrix matrixResult2 = matrix1 * matrix2;
        }
    }
}

【问题讨论】:

    标签: c#


    【解决方案1】:

    这是因为您使用的是System.Drawing.Drawing2D.Matrix 而不是System.Windows.Media.Matrix

    System.Drawing.Drawing2D.Matrix.Multiply 方法采用一或两个参数。第一个是Matrix,第二个是可选的MatrixOrder

    删除这一行:

    using System.Drawing.Drawing2D;
    

    如果错误仍然存​​在,则您可能引用了错误的程序集。

    如果您使用的是 Visual Studio

    1. 右键单击项目的引用
    2. 点击“添加参考
    3. 搜索 WindowsBase 并选择它

    【讨论】:

    • 您好,感谢您的快速回复!但是,在我删除 using System.Drawing.Drawing2D 后,出现了新的错误。 1) 错误 CS0246 找不到类型或命名空间名称“Matrix”(您是否缺少 using 指令或程序集引用?) & 2) 错误 CS0103 当前上下文中不存在名称“Matrix”。我已将参考:PresentationCore、System.Windows、System.Drawing 添加到项目参考中。错误仍然弹出。对此有何建议?谢谢。
    • 我尝试使用 PresentationCore 插入行,最终弹出更多错误。
    • 好吧,你需要引用 WindowsBase.dll。我会编辑我的答案
    • 感谢您的回复和帮助!有效!真的很感激!
    【解决方案2】:

    如果你使用命名空间System.Drawing.Drawing2D,你可以像下面这样进行乘法运算:

    matrix1.Multiply(matrix2); //matrix1 will have the result.
    

    供参考:https://docs.microsoft.com/en-us/dotnet/api/system.drawing.drawing2d.matrix.multiply?view=netframework-4.8

    【讨论】:

    • 感谢您的回复!我将查看您提供的参考链接。我还是新手,还在学习 C#,这个参考资料可以把我带到另一个级别的编程矩阵计算。目前C.Champagne提供的上述解决方案有效(使用System.Windows.Media.Matrix,添加了参考WIndowsBase)。稍后我将尝试 System.Drawing.Drawing2D 命名空间。非常感谢您的帮助!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    相关资源
    最近更新 更多