473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Copy two-dimensional array to BitmapData.Scan0

With Marshal you can copy data from a one-dimensional array to
BitmapData.Scan0.

What do I do if my array is two-dimensional?

Or, alternatively, can I create somehow two variables
byte[] arr1; // size 480*640
byte[,] arr2; // size [480,640]
which are references to the same memory block and allow me to access it
at will with one or two indices, e.g. arr1[640] or arr2[1,0]?

I tried to assign arr2[0,0] to Scan0 to avoid copying at all, but it
didn't work.

Nov 17 '05 #1
2 4362
fr****@terra.com.br wrote:
With Marshal you can copy data from a one-dimensional array to
BitmapData.Scan0.

What do I do if my array is two-dimensional?

Or, alternatively, can I create somehow two variables
byte[] arr1; // size 480*640
byte[,] arr2; // size [480,640]
which are references to the same memory block and allow me to access
it at will with one or two indices, e.g. arr1[640] or arr2[1,0]?


Hi Frank. Because arrays in C# are not pointer-based, unlike in C, it is not
possible to have two arrays share the same underlying data (and in fact, I'm
not sure if multidimensional arrays even have a linear memory
representation). However, one simple solution that achieves a similar effect
is to create a class that wraps a one-dimensional array and performs index
calculations for you in an indexer like this:

public class Array2D<T> {
T[] a;
int columns;

public Array2D(int rows, int columns) {
this.columns = columns;
this.a = new T[rows * columns];
}

public T this[int row, int column] {
get {
return a[row * columns + column];
}
set {
a[row * columns + column] = value;
}
}

public T[] ToArray() {
return a;
}
}

This code is untested and uses generics, a feature of C# 2.0, but you could
use "object" in place of T at the expense of type safety and added casts.
You simply use it like a normal two-dimensional array until you want the
underlying one-dimensional array, then you call ToArray(). If the calls are
inlined, the efficiency hit should be relatively small. I hope this helps.
--
Derrick Coetzee, MCP, MSFT (Speech Server)
This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included code samples are subject to the terms
specified at http://www.microsoft.com/info/cpyright.htm
Nov 17 '05 #2
Well, actually I do something similar, less fancy. I simply select my
one-dimensional array element with a method "int Index (int i, int j)
which calculates a single index from an index pair. I just thought,
twodimensional arrays would be stored as a contigous series of rows in
the same way as C compilers do it. In this case, the index mapping
algorithm would already be implemented and eventually be faster than my
Index method.

The other reason was my hope that I could display an image which
already exists in the form of a bidirectional array, just redirecting a
BitmapData.Scan0 pointer to that array, without copying anything.

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
4
by: Jean Stax | last post by:
Hi ! I tried to understand when the explicit attribute in copy constructor prevents from me to create a new object. Bellow is the sample code. While the two first cases really generate a...
16
by: bluekite2000 | last post by:
I want Matrix A(B) to create shallow copy of B but A=B to create deep copy of B. Is that bad design? Why and why not?
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
0
by: igendreau | last post by:
I have a database with a Header table. Each record in tblHeader has two One-to-Many Relationships: with tblLines and tblKeys. The HeaderID field ties tblHeader to the other two tables. The data...
8
by: downwitch | last post by:
Either I don't understand (entirely possible), or there's no way to copy parts of a class hierarchy from one instance to another. Say I have a class called Foo, and it contains, among other...
2
by: flamexx7 | last post by:
http://www.rafb.net/paste/results/V3TZeb28.html In this code, why copy constructor is not called while returning object from no_arg() . I was trying to find answer in C++ Standard. and there it's...
22
by: Steven Blair | last post by:
I need to perform a Deep Copy on an ArrayList. I wrote a small sample app to prove this could be done: ArrayList a = new ArrayList(); ArrayList b = new ArrayList(); a.Add("Hello"); b =...
1
by: dkmarni | last post by:
Hi, I am trying to do this perl script, but not able to complete it successfully. Here is the description what the script has to do.. Accept two and only two command line arguments. Again,...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.