2

I have an image in ushort variable, want to save this image in binary format.

Please, anyone, tell me How can this be done using C#?

I have tried this but its not working

ushort[] Depthdata;
Depthdata = new ushort[DWidth * DHeight];
string s1 = string.Format("{0}", count_depth);
FileStream fs = new FileStream("C:\\img" + s1 + ".bin", FileMode.Create, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
string image_str = Convert.ToString(Imagedata);
bw.Write(image_str);
bw.Close();
fs.Close();

Here is attached my full code

Addee
  • 663
  • 10
  • 21
  • for giving -1 please explain the reason so i can update or check whats wrong i am doing – Addee Jun 11 '15 at 11:24
  • @Addee, your image must be quite small if it can fit in a `ushort`. Could it be an array of `ushort` instead? – Frédéric Hamidi Jun 11 '15 at 13:21
  • @FrédéricHamidi: The definition of Depthdata is like this `ushort[] Depthdata;` `Depthdata = new ushort[DWidth * DHeight];` – Addee Jun 11 '15 at 13:33
  • That's an array, then. You probably should update your question with that information. – Frédéric Hamidi Jun 11 '15 at 13:34
  • Ok, I have updated the question. do you have any solution for this? – Addee Jun 11 '15 at 13:41
  • I'm still trying to understand why you're calling `Convert.ToString()` on that data... – Frédéric Hamidi Jun 11 '15 at 13:49
  • Actually, I just did that because it was showing me error when I do `bw.Write(Imagedata);` so I used that to convert to string. but I am not sure it is correct or not – Addee Jun 11 '15 at 14:01
  • Do you have an `Image` or `Bitmap` built in your code anywhere? It would be easier to save that. Otherwise do you have a **format** that the data should be saved in? – Idle_Mind Jun 11 '15 at 14:46
  • @Idle_Mind, i modified my post and added my full code to check. i want to save in .bin format, which will later used in MATLAB for further processing – Addee Jun 11 '15 at 14:59
  • I have no idea what format MATLAB expects. That's a pretty important piece of information you should have included in your problem description. – Idle_Mind Jun 11 '15 at 15:00
  • Why did you want to use a .bin extension for an image? That's very uncommon, and looking at [MATLAB supported import formats](http://www.mathworks.com/help/matlab/import_export/supported-file-formats.html), it looks like it doesn't even support .bins for import... – Dylan Corriveau Jun 11 '15 at 15:10
  • @DylanCorriveau: I can save the image in (.png, .gif, .tif) but that took much in saving the image and there is a stream of image coming at 30fps. So I decided to save in .bin format which take less time save – Addee Jun 11 '15 at 15:14
  • @Addee, since you are not really interested in the data as an image but rather as a matrix, I recommend you use the `.MAT` format. It's text-based and thus a bit easier to handle in my opinion. – Good Night Nerd Pride Jun 11 '15 at 15:18
  • @Abbondanza Can you please tell me the code how can save `Imagedata` variable to `Mat` in c#? – Addee Jun 11 '15 at 15:22
  • @Addee for .mat files, take a look [here](http://www.mathworks.com/matlabcentral/answers/98947-is-it-possible-to-read-write-mat-files-from-a-c-application) – Dylan Corriveau Jun 11 '15 at 15:47

2 Answers2

1

I would like to mention that the code here, and the one in your link are different...

In any case, going by the one in your link:

ushort[] Depthdata;
....
string s1 = string.Format("{0}", count_depth);
FileStream fs = new FileStream("G:\\test" + s1 + ".bin", FileMode.Create, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
string depth_str = Convert.ToString(Depthdata);
bw.Write(depth_str);
bw.Close();
fs.Close();

You shouldn't actually need to convert your Depthdata to a string. BinaryWriter can actually take a ushort value in one of its overloads. Why not just iterate through and write it out? Also, you should use using statements for your filestream and binarywriter.

Try the following:

using(FileStream fs = new FileStream("G:\\test" + s1 + ".bin", FileMode.Create, FileAccess.Write))
{
    using(BinaryWriter bw = new BinaryWriter(fs))
    {
        foreach(ushort value in Depthdata)
        {
            bw.write(value);
        }
    }
}
Dylan Corriveau
  • 2,561
  • 4
  • 29
  • 36
  • In case how can i use `IplImage` instead of `ushort`? – Addee Jun 11 '15 at 17:19
  • `IplImage` has a method called `ToBitmap()`, just use that, then use .Save (filename). You won't need to use what i wrote if you are using that... – Dylan Corriveau Jun 11 '15 at 17:33
  • 1
    can you tell me the extact code for that? and can i save the `IplImage` to .bin also? – Addee Jun 11 '15 at 18:09
  • There's a nice explanation found [here](http://stackoverflow.com/questions/697801/convert-iplimage-into-a-jpeg-without-using-cvsaveimage-in-opencv) – Dylan Corriveau Jun 15 '15 at 00:10
1

I think this will help you.i tested this on *.tiff file

first make separate Class Ext

public static class Ext
    {

        public static string ToHexString(this byte[] hex)
        {
            if (hex == null) return null;
            if (hex.Length == 0) return string.Empty;

            var s = new StringBuilder();
            foreach (byte b in hex)
            {
                s.Append(b.ToString("x2"));
            }
            return s.ToString().ToUpper();
        }
    }

then you can Add following code to convert image to string binary

 FileStream fs=new FileStream(ImgPathID, FileMode.Open, FileAccess.Read); //set file stream
 Byte[] bindata=new byte[Convert.ToInt32(fs.Length)];
 fs.Read(bindata, 0, Convert.ToInt32(fs.Length));
string  bindatastring = Ext.ToHexString(bindata);// call to class