From: Arcee Gomes on
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
From: Ken M. on
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
From: Ken M. on
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
From: Ken M. on
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)