Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Displaying Matlab image in VB.NET picturebox

394 views
Skip to first unread message

Arcee Gomes

unread,
Jan 22, 2010, 1:28:08 AM1/22/10
to
Hello,

I have created a user defined function in matlab which reads an image, converts it to grayscale and displays it to the user.

I have created the dll file using Matlab Builder NE --> Generic COM and added a referrence to use it VB.NET 2008 GUI.

The code works fine. Only problem is that it displays the grayscale image in VB.NET in a separate window, but I want it to display the grayscale image in the picturebox in VB.NET (picturebox is present in the second form)
Please provide the syntax of inserting the image in the picturebox. Also I don't want it to display outside in a sepate window. Thanking you in advance.

The Matlab and VB.NET code are as follows

*** Matlab code

function y = Test2(p)
y = imread(p);
j = rgb2gray(y);
imshow(j);


***** VB code

Public Class Form1
Private cal As SampleCon.SampleConClass

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim d, b As Object
OpenFileDialog1.Multiselect = False
OpenFileDialog1.Filter = "Jpeg|*.jpg|Gif|*.gif|Png|*.png|Bitmap|*.bmp"
OpenFileDialog1.RestoreDirectory = True
OpenFileDialog1.Title = "Select an image"
If OpenFileDialog1.ShowDialog <> DialogResult.Cancel Then
TextBox1.Text = OpenFileDialog1.FileName
d = Me.OpenFileDialog1.FileName
Call cal.Test2(1, b, d)
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cal = New SampleCon.SampleConClass
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Hide()
Form2.Show()
End Sub
End Class

Ken M.

unread,
Feb 13, 2010, 7:59:20 AM2/13/10
to
I'm trying the same thing without luck.
I tried it this way, if anyone can help?:
I removed some not relevant code.

In the M file:
function imageByteData
fig=figure;
plot(x,y);
set(fig, 'Visible', 'off');
imageByteData = figToImStream(fig, 'bmp', 'uint8');
close(fig);

In VB.net:
Dim byteArray As Byte()
Dim tempimage As Image = Nothing
byteArray = plotter.plotfftim().ToArray ' .m function output
Byte2Image(tempimage, byteArray)
Form1.PictureBox1.Image = tempimage
Form1.PictureBox1.Show()

The Byte2image function is something I found on the internet.
Public Sub Byte2Image(ByRef NewImage As Image, ByVal ByteArr() As Byte)
Dim ImageStream As MemoryStream
ImageStream = New MemoryStream(ByteArr)
NewImage = Image.FromStream(ImageStream)
End Sub

Ken M.

unread,
Feb 13, 2010, 12:33:07 PM2/13/10
to
I got it to work!!:
There is probably an easier way to avoid/ or convert this 2 dimentional array (of .ToArray) in a single one than with this for loop but this works.

M file: (PlotterSignalAnalyzer class with this function in the dll)


fig=figure;
plot(x,y);
set(fig, 'Visible', 'off');

imageByteData = figToImStream('figHandle',fig,'imageFormat', 'png','outputType', 'uint8');
close(fig);

VB.net.:
Dim plotter As New PlotterSignalAnalyzer


Dim tempimage As Image = Nothing

Dim byteArray(,) As Byte
byteArray = plotter.plotfftim().ToArray()


Byte2Image(tempimage, byteArray)
Form1.PictureBox1.Image = tempimage

Sub used:
Public Sub Byte2Image(ByRef NewImage As Image, ByVal ByteArr(,) As Byte)
Dim ByteArray1(ByteArr.GetUpperBound(0)) As Byte
Dim ImageStream As MemoryStream
Try
If ByteArr.GetUpperBound(0) > 0 Then
For i = 0 To ByteArr.GetUpperBound(0)
ByteArray1(i) = ByteArr(i, 0)
Next
ImageStream = New MemoryStream(ByteArray1)
NewImage = Image.FromStream(ImageStream)
Else
NewImage = Nothing
End If
Catch ex As Exception
NewImage = Nothing
End Try
End Sub

Ken M.

unread,
Feb 14, 2010, 5:20:20 AM2/14/10
to
The Simplified VB.net code: (no need for that for loop)

Dim plotter As New PlotterSignalAnalyzer
Dim MatNumImageArray As New MWNumericArray
Dim ByteArray() As Byte
Dim ImageStream As MemoryStream

MatNumImageArray = plotter.plotfftim()
ByteArray = MatNumImageArray.ToVector(0)
ImageStream = New MemoryStream(ByteArray)
Form1.PictureBox1.Image = Image.FromStream(ImageStream)

Sameera Abar

unread,
Jun 26, 2016, 11:08:10 AM6/26/16
to

I have made some changes in the Matlab's m-files and VB-program along with exception error handling block and some configurations as explained at the end of this post and then eventually got this procedure functional.

DEPLOYING a .NET COMPONENT with MATLAB .NET BUILDER:
Conversion of m (MATLAB source code) file to .NET component in the form of a DLL (Dynamic Link Library) file using MATLAB deployment project tool:
You need to install:
1: Matlab Compiler SDK: extends the functionality of MATLAB Compiler and helps to build DLLs (using .NET Assemblies) to be accessed within Microsoft Visual Studio programs.
2: MATLAB Runtime on end-user computers: The MATLAB Runtime is a standalone set of shared libraries that enables the execution of compiled MATLAB applications or components on computers that do not have MATLAB installed.

Creating *.m files in Matlab:
I have created two *.m files that plot two different images in Matlab2016a.

FILE: 1 (imageByteData01.m)
function returnFigure01 = imageByteData01()
fig1=figure;
x = [-2 -1 0];
y = 2*x;
plot(x,y);
set(fig1, 'Visible', 'off');
returnFigure01 = figToImStream('figHandle',fig1,'imageFormat', 'png','outputType', 'uint8');
%n = 15;
%pause(n)
close(fig1);
end

FILE: 2 (imageByteData02.m)
function returnFigure02 = imageByteData02()
fig2=figure;
surf(peaks);
set(fig2, 'Visible', 'off');
returnFigure02 = figToImStream('figHandle',fig2,'imageFormat', 'png','outputType', 'uint8');
%disp(imageByteData);
close(fig2);
end

Building a DLL Component in Matlab2016a using Matlab Compiler SDK:
Then I combined these files in a single DLL (imageByteData0102.dll) by using Matlab Compiler SDK with 'Microsoft .NET Assembly' option and the method works well.
Compiler --> Compiler Library Project --> .NET Assembly --> Click + icon and add two files (imageByteData01.m) and (imageByteData02.m)
In the Library Information dialogbox, input:
DLL Name: imageByteData0102
Class Name: ImagePlotters

--------------------------------------------------------------------
Creating a Microsoft Visual Studio project (a graphical user interface):
Encapsulating the Matlab functions as DLLs, and then via calls to classes/methods of MWArray-API, integrate the Matlab's functionality with Visual Basic wrapper-GUI application:
On the Visual Basic Form1, put a button and picturebox control.

Imports System.IO
Imports MathWorks.MATLAB.NET.Arrays
Imports imageByteData0102
Imports MathWorks.MATLAB.NET.Utility

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Try
Dim myplotter As ImagePlotters = New ImagePlotters()
'Creating an instance of MWObjectArray to wrap the .Net Component
Dim MatNumericImageArray01 As New MWNumericArray
Dim BytesArray01() As Byte
Dim StreamData01 As MemoryStream
MatNumericImageArray01 = myplotter.imageByteData01()
BytesArray01 = MatNumericImageArray01.ToVector(0)
StreamData01 = New MemoryStream(BytesArray01)
PictureBox1.Image = Image.FromStream(StreamData01)

'Two images appear in a VB PictureBox1 with a certain delay
PictureBox1.Refresh()
Threading.Thread.Sleep(20000)

Dim MatNumericImageArray02 As New MWNumericArray
Dim BytesArray02() As Byte
Dim StreamData02 As MemoryStream
MatNumericImageArray02 = myplotter.imageByteData02()
BytesArray02 = MatNumericImageArray02.ToVector(0)
StreamData02 = New MemoryStream(BytesArray02)
PictureBox1.Image = Image.FromStream(StreamData02)
Console.WriteLine(StreamData02)

Catch Exception As Exception
Console.WriteLine("Error: {0}", Exception)
End Try

CreateObject("WScript.Shell").Popup("Images from Matlab DLLs are displayed in the VB Picturebox area !", 1, "Fetching Images")

End Sub

Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click

End Sub
End Class
--------------------------------------------------------------------
SETTINGS: CREATING REFERENCES TO THE .NET COMPONENT + MWArray API
Now add the following two class library references (Project --> References) in the Visual Basic environment.
1: MWArray.dll (Already present at: C:\Program Files\MATLAB\R2016a\toolbox\dotnetbuilder\bin\win64\v2.0)
2: imageByteData0102.dll (Newly created Matlab's DLL as explained above)

IMPORTANT: If your Operating System and Matlab software are 64-bit, then set the 'Debug' option to 'x64' instead of 'Any CPU' compilation target.

RUN:
On building the VB application, click the button control and two images appear one after another with a certain delay in a PictureBox1 control.
--------------------------------------------------------------------
Note: SOME DETAILS ON CALLING MWArray WRAPPER-API (Root of the hierarchy is: MWArray abstract class):
The MWArray API, a standard API that is used in conjunction with MATLAB Builder NE to deal with all basic MATLAB data types: MWNumericArray, MWLogicalArray, MWCharArray, MWCellArray, and MWStructArray. This API requires the MATLAB runtime to be installed on the target machine as it makes use of several primitive MATLAB functions. All data returned from MATLAB function to a .NET method is represented as an instance of the appropriate MWArray subclass. For each MATLAB function, the builder generates a wrapper class that has overloaded methods to implement the various forms of the generic MATLAB function calls.

See the Reference of MWArray Class Library Specification:
http://radio.feld.cvut.cz/matlab/toolbox/mathlib/cppmathref/cpp_mwar.html
http://files.matlabsite.com/docs/books/matlab-docs/matlab_builder_ne_dotnetbuilder_r2014b.pdf
Overview of Classes and Methods in the Data Conversion Class Hierarchy:
C:\Program Files\MATLAB\R2016a\help\dotnetbuilder\MWArrayAPI\index.html

Hope this helps.
Sameera Abar
0 new messages