2014-01-23 77 views
0

我是VB.net的新手,但我必須爲我們的項目執行此操作。我做了一個非常簡單的程序來調用我在vb.net中創建的matlab函數。這個程序很簡單,它只會確定圖片框中圖片的寬度和高度。在vb.net中使用MATLAB函數

這裏是我的MATLAB代碼,並使用.NET程序集導出一個.dll文件編譯它:

function [width, height, third] = imageInfo(input) 
inImage = imread(input); 
[width, height, third] = size(inImage); 
end 

這裏是我的程序的GUI:

enter image description here

而這裏的VB我做的代碼:

Imports MathWorks.MATLAB.NET.Arrays 
Imports MathWorks.MATLAB.NET.Utility 
Imports imageInfo 

Public Class Form1 
    Public myImageInfo As New imageInfoClass 

    Dim imageWidth As String 
    Dim imageHeight As String 
    Dim result As New MWNumericArray 
    Dim imagePath As String 

    Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click 
     If ofdSelectPicture.ShowDialog = DialogResult.OK Then 
      pbImage.Image = Image.FromFile(ofdSelectPicture.FileName) 
      imagePath = "'" & ofdSelectPicture.FileName & "'" 
     End If 
    End Sub 

    Private Sub btnProcess_Click(sender As Object, e As EventArgs) Handles btnProcess.Click 
     result = myImageInfo.imageInfo(imagePath) 
     tbHeight.Text = imageHeight 
     tbWidth.Text = imageWidth 
    End Sub 
End Class 

我總是得到一個錯誤,當r統一計劃。我希望你能用這個幫助我。謝謝

+0

這將真正幫助,如果你能說你在說什麼類型的錯誤。這是一個例外嗎?它是否有消息?你什麼時候得到這個錯誤? – MPelletier

+0

下面是錯誤的截圖:http://imagizer.imageshack.us/v2/800x600q90/819/yruy.jpg 我在運行程序時遇到了錯誤。 – elvinguitar

回答

0

這部分在這裏:

result = myImageInfo.imageInfo(imagePath) 

imagePath需求是完整路徑。你剝離出來,並與

imagePath = "'" & ofdSelectPicture.FileName & "'" 

添加逗號你可以試試:

imagePath = ofdSelectPicture.SafeFileName 
+0

'imread'需要文件名的完整路徑(除非圖像在當前目錄中或在MATLAB路徑中可用),但是您不必將其包裝在單引號中。 'MWArray'橋接類負責將.NET字符串轉換爲MATLAB字符串。所以我覺得'的ImagePath = ofdSelectPicture.FileName'應該工作.. – Amro

+0

我仍然得到一個錯誤: 「System.InvalidOperationException」類型的未處理的異常出現在圖片Info.exe 其他信息:出錯創建窗體。有關詳細信息,請參閱Exception.InnerException。錯誤是:'imageInfo.imageInfoClass'的類型初始值設定項引發異常。 – elvinguitar

+0

@elvinguitar你知道如何使用調試器嗎?它會幫助你很大。你可以在'result = myImageInfo.imageInfo(imagePath)'一行上放置一個斷點,這將使調試器在執行該行之前停止。然後你可以看到'imagePath'的值)。 – MPelletier