Secret
Secret

Reputation: 337

How to convert MATLAB MWArray to Bitmap with C# from MATBLAB .NET Compiler

I searched and some a few examples but I did not get any idea even it seems to be a something relatively easy I hope someone can help

I have a very simple MATLAB function, taking a string filePath of an image and covert it to GrayScale using rgb2gray(filePath)

function I = ConvertToGrayScale(filePath)     
  RGB  = imread(filePath);
  I = rgb2gray(RGB);     
end

Then it got compiled to dll file using MATLAB Comiler with the class name ImageProfessing to have ConvertToGrayScale.dll. No error, compiler gave the dll

In C# Winform, I included the 2 dlls:MWArray and ConvertToGrayScale as reference in my C#, the test image is loaded directly from @"D:\\Caputure.PNG" which is also displayed on pictureBox1, when user hits button Convert to GrayScale, the image is converted and displayed on pictureBox2

using MathWorks.MATLAB.NET.Arrays;
using CovertToGrayScale;

WinForm

private void Form1_Load(object sender, EventArgs e)
{   
    pictureBox1.Image = Image.FromFile(@"D:\\Capture.PNG");
}

private void btCovertToGrayScale_Click(object sender, EventArgs e)
{   
    ImageProcessing imageProcessing = new ImageProcessing();
    MWCharArray fileStringArray = new MWCharArray(@"D:\Capture.PNG");
    MWArray I = imageProcessing.CovertToGrayScale(fileStringArray);
    int[] dim = I.Dimensions;
    MessageBox.Show(dim.ToString()); // just to see what dim is
} 

---> I do not know how to convert I to Bitmap so I can display, save, or load to MemoryStream. I checked I and it is not empty, it does contain data of the GrayScale image (using I.IsEmpty and I.NumberofElements)

Upvotes: 1

Views: 762

Answers (1)

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23675

The standard approach for converting a byte array to a Bitmap in C# uses the Bitmap(Stream) constructor overload as follows:

Byte[] data = ...;
Bitmap bmp;

using (MemoryStream ms = new MemoryStream(data))
    bmp = new Bitmap(ms);

But this works only if your data array contains the whole image binary data, and not only pixels data. So things get a little bit harder in this case, since what you retrieve from Matlab is just an height-by-width matrix with pixels information. First, you have to convert the MWArray into a flatten Byte[]:

MWArray I = imageProcessing.CovertToGrayScale(fileStringArray);
MWNumericArray I_num = I.ToArray();
Byte[] I_bytes = (Byte[])I_num.ToVector(MWArrayComponent.Real);

Once you have the grayscale pixel data converted into the native Byte format, you must proceed as follows (w and h are two integers representing the respectively the width and the height of the image... you can assign their values from I.Dimensions):

Bitmap bmp = new Bitmap(w, h, PixelFormat.Format8bppIndexed);
ColorPalette cp = bmp.Palette;

for (Int32 i = 0; i < 256; ++i)
    cp.Entries[i] = Color.FromArgb(255, i, i, i);

bmp.Palette = cp;

BitmapData data = bmp.LockBits((new Rectangle(0, 0, bmp.Width, bmp.Height)), ImageLockMode.WriteOnly, bmp.PixelFormat);
Marshal.Copy(I_bytes, 0, data.Scan0, I_bytes.Length);
bmp.UnlockBits(data);

Upvotes: 1

Related Questions