# [HELP] Double Values in array between 0-255 Displayed as Image on a form

• January 4th, 2012, 10:16 AM
earlbob91
[HELP] Double Values in array between 0-255 Displayed as Image on a form
Hello, I have refined my earlier question that actually got no replies to one that should hopefully get a little bit more attention. I will try to structure the question so that it is easy to get to the point for more educated readers.

My Knowledge:
Logically, Syntactically good knowledge. Understanding of algorithms and programming etiquette. I find mathematical programming quite easy. Been self teaching myself for about 2 years now, sticking to the stuff I essentially NEED to know.

Graphical programming knowledge is VERY basic.
File I/O understanding isnt great.
SQL/Database is Basic.

My problem:
Array of Double[,] Double[] Byte[,] or Byte[] being displayed as a BitMap type image or similar.

So Far:
I have managed to turn an array double[,] to byte[,] presuming all the values are 8bit integers, which....so far I havent actually come across data that works with this function. Then using code snippets I have found on the internet to 'convert' this numerical data into an object of image (or so i think I have, like I said my graphic knowledge is very poor)

Now what?:
So alternatively and preferably, some ideas on just using the double values between 0-255 as colour values even though they can go deep into the decimal place (whether or not this is possible I don't yet know), OR a method to round each number to its closest 8bit integer for convertion aspects of the data to type Byte[] or Byte[,] (Probably easiest way?) making it more conventional.

Can I get a result similar to this with what I'm trying to do?:
(not necessarily so complex, best example I could find)
Raster Grid Example

What the program currently does:
Its essentially a raster data manipulation software, with simple and advanced raster calculation being applied to raster data in the ascii grid format (used typically in ArcGIS) and then saved back to disk as a new table. The functionality of this is 100% ok and the only thing I'm trying to do is display the data im working on for a visual confirmation of the data manipulation.

It loads the data into an object Grid which has a double field Values[,] systematically, after reading the header in the file. The dataset also contains a value for no value, generally -9999, this can probably be changed to 0 for the sake of the pallette.

Example Raster Grid Input Data

Code snippet that calls the conversion process:
Code:

```g1 = new Grid(fname);             g1 = g1.LinearScale8Bit();             byte[,] Values = new byte[g1.Values.GetLength(0), g1.Values.GetLength(1)];             Image img;             Bitmap bmp;                                                 for (int i = 0; i <= g1.Cols; i++)             {                 for (int j = 0; j <= g1.Rows; j++)                 {                     Values[i, j] = Convert.ToByte(g1.Values[i, j]);                                    }             }             img = Grid.ByteArrayToImage(Values);             bmp = new Bitmap(img);             Form frm2 = new Form2(bmp);                        frm2.Show();```

Code Grid.LinearScale8Bit()
Code:

```public Grid LinearScale8Bit()         {             Grid newgrid = new Grid();             double min = this.GridGlobalMin();             double max = this.GridGlobalMax();             int r, c;             newgrid.Cols = this.Cols;             newgrid.Rows = this.Rows;             newgrid.XCorner = this.XCorner;             newgrid.YCorner = this.YCorner;             newgrid.CellSize = this.CellSize;             newgrid.NoData = this.NoData;             newgrid.Values = new double[newgrid.Rows, newgrid.Cols];             for (r = 0; r < this.Rows; r++)                 for (c = 0; c < this.Cols; c++)                     newgrid.Values[r, c] = (this.Values[r, c] - min) / (min - max) * -255;                         return newgrid;         }```
Code Grid.ByteArrayToImage(Byte[,])
Code:

```public static Image ByteArrayToImage(byte[,] byteArrayIn)         {             Byte[] byteArrayCopy = byteArrayIn.Cast<byte>().ToArray();             MemoryStream ms = new MemoryStream(byteArrayCopy);             Image returnImage = Image.FromStream(ms);             return returnImage;         }```

Entire Grid Class

I have supplied the entire grid class just incase you want to familiarise yourself with the class more than the code supplied.

ANY help is much appreciated, I have been looking through the internet, but I'm either searching for the 'wrong' thing or my problem isnt usually a problem to most people, or its not a problem yet needed answering.

I can post a screenshot of the program itsself if necessary.

earlbob91
• January 4th, 2012, 12:22 PM
Re: [HELP] Double Values in array between 0-255 Displayed as Image on a form
So if I understand correctly, you're having trouble converting a byte[] to an Image, right? I think that would isolate the problem to the ByteArrayToImage() method. Maybe it's an encoding issue? Or maybe you're missing some critical descriptor bytes that describe the image. What do you get for the image? Is it just a messed up looking image or is the image blank?
• January 4th, 2012, 12:45 PM
earlbob91
Re: [HELP] Double Values in array between 0-255 Displayed as Image on a form
When the ByteArrayToImage method is called it throws the exception:
Code:

```System.ArgumentException was unhandled   Message=Parameter is not valid.   Source=System.Drawing   StackTrace:       at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)       at System.Drawing.Image.FromStream(Stream stream)       at Assignment_2_Raster_Mapematics.Grid.ByteArrayToImage(Byte[,] byteArrayIn) in E:\MyDocumentsBackup\University\Stage 2\CEG2706 Geospatial Algorithms\Assignment 2\Assignment 2 Raster Mapematics\Assignment 2 Raster Mapematics\Grid.cs:line 560       at Assignment_2_Raster_Mapematics.Form1.btnDrawRaster_Click(Object sender, EventArgs e) in E:\MyDocumentsBackup\University\Stage 2\CEG2706 Geospatial Algorithms\Assignment 2\Assignment 2 Raster Mapematics\Assignment 2 Raster Mapematics\Form1.cs:line 510       at System.Windows.Forms.Control.OnClick(EventArgs e)       at System.Windows.Forms.Button.OnClick(EventArgs e)       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)       at System.Windows.Forms.Control.WndProc(Message& m)       at System.Windows.Forms.ButtonBase.WndProc(Message& m)       at System.Windows.Forms.Button.WndProc(Message& m)       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)       at System.Windows.Forms.Application.Run(Form mainForm)       at Assignment_2_Raster_Mapematics.Program.Main() in E:\MyDocumentsBackup\University\Stage 2\CEG2706 Geospatial Algorithms\Assignment 2\Assignment 2 Raster Mapematics\Assignment 2 Raster Mapematics\Program.cs:line 18       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)       at System.Threading.ThreadHelper.ThreadStart()   InnerException:```
It seems to convert the arrays Ok though, its an error with the memory stream.

earlbob91
• January 4th, 2012, 01:01 PM
Re: [HELP] Double Values in array between 0-255 Displayed as Image on a form
The only thing I can think of is that the MemoryStream argument you're supplying to Image.FromStream() is invalid for some reason. Since the MemoryStream constructor executes successfully, then I imagine a valid MemoryStream object is being passed to the Image.FromStream() method. However, the data the MemoryStream object contains may be invalid. You may try a different method of converting the byte[] to an image, such as using the Bitmap class. It has more methods and such for performing conversions :) But the data in the byte[] could still be invalid for producing an image from. However, I think the Bitmap class is a little more forgiving since it has traditionally used the 2d grid layout to produce images.
• January 4th, 2012, 01:26 PM
earlbob91
Re: [HELP] Double Values in array between 0-255 Displayed as Image on a form
Ok I'll give that a try thanks, is there anything specific you can recommend me using in the bitmap class? how would you think about going about doing it? just need a bit of inspiration thats all :P

Cheers mate
earlbob91
• January 4th, 2012, 01:53 PM