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
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
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
MatNumImageArray = plotter.plotfftim()
ByteArray = MatNumImageArray.ToVector(0)
ImageStream = New MemoryStream(ByteArray)
Form1.PictureBox1.Image = Image.FromStream(ImageStream)