January 4th, 2012, 10:16 AM
[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.
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.
Array of Double[,] Double Byte[,] or Byte being displayed as a BitMap type image or similar.
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)
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:
g1 = new Grid(fname);
g1 = g1.LinearScale8Bit();
byte[,] Values = new byte[g1.Values.GetLength(0), g1.Values.GetLength(1)];
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);
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;
public static Image ByteArrayToImage(byte[,] byteArrayIn)
Byte byteArrayCopy = byteArrayIn.Cast<byte>().ToArray();
MemoryStream ms = new MemoryStream(byteArrayCopy);
Image returnImage = Image.FromStream(ms);
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.
Cheers for reading
Tags for this Thread
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.