469,625 Members | 1,077 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

Convert Array of Int to image

Hi,

i have a array of uint[] and want to display this array as a
System.Web.UI.WebControls.Image.
//=========================================
uint [] ar = new uint[3072];
string fName;

System.Web.UI.WebControls.Image i = new System.Web.UI.WebControls.Image();
vm.Display._GenerateThumbnail(ar);
fName = "c:\\temp\\f_" + vm.Name + ".bmp";

SaveArrayToFile(ar,fName);
i.ImageUrl = fName;
i.ID = "I_1";
Panel1.Controls.Add(i);

private void SaveArrayToFile(uint [] ar, string FileName)
{
FileStream fs = new FileStream(FileName, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
for (int i = 0; i < ar.Length; i++)
{
w.Write(ar[i]);
}
w.Close();
fs.Close();
}

//=========================================

The problem is, that the pictures aren't shown, as the picures are garbage.
What is wrong ?

Best regards

Clas

--
The light at the end of the tunnel,
was switch off due to high costs.
Nov 16 '05 #1
2 7125
Hi Clas,

Let's first step through your code. You create an Image class instance. Then
you call a method which you haven't told us anything about. It seems to be
generating a thumbnail from the (empty) image you just created, although I
can't tell for sure, again, because you've told us nothing about it. But it
gets worse. Now you call a method called "SaveArrayToFile" which writes the
array to a file specified in the previous line. Note that you have abandoned
the Image class which you created empty. You never do anything with it, nor
do you associate it in any way with a file. I have no idea why you would
think that this creates an image of any kind. An image file is not created
by writing an array of unsigned integers to a file. What you DO get is a
binary file containing an array of unsigned integers.

Okay, let's talk about how to do it right. Yes, you DO need to create an
Image, but more specifically, a Bitmap. You need to make sure that no value
in your array is greater than 255, or less than 0. That is the maximum and
minimum values of a pixel byte. And your array elements must match the size
of a byte, or you will have to convert them on the fly, which is expensive.
And your array must have "width * height" values in it (width and height of
the Bitmap).

Now you have 2 choices. You can use Managed code to loop through the pixels,
using the Bitmap.GetPixel() method, and set the values of each R,G, and B
byte, or you can use an unsafe code block (more complicated, but much
faster) to loop through the pixels of the Bitmap with a pointer, and set
them that way.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
"Clas Hortien" <ho*****@hotmail.com> wrote in message
news:ci**********@news1.wdf.sap-ag.de...
Hi,

i have a array of uint[] and want to display this array as a
System.Web.UI.WebControls.Image.
//=========================================
uint [] ar = new uint[3072];
string fName;

System.Web.UI.WebControls.Image i = new System.Web.UI.WebControls.Image();
vm.Display._GenerateThumbnail(ar);
fName = "c:\\temp\\f_" + vm.Name + ".bmp";

SaveArrayToFile(ar,fName);
i.ImageUrl = fName;
i.ID = "I_1";
Panel1.Controls.Add(i);

private void SaveArrayToFile(uint [] ar, string FileName)
{
FileStream fs = new FileStream(FileName, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
for (int i = 0; i < ar.Length; i++)
{
w.Write(ar[i]);
}
w.Close();
fs.Close();
}

//=========================================

The problem is, that the pictures aren't shown, as the picures are garbage. What is wrong ?

Best regards

Clas

--
The light at the end of the tunnel,
was switch off due to high costs.

Nov 16 '05 #2
Clas Hortien <ho*****@hotmail.com> wrote:
i have a array of uint[] and want to display this array as a
System.Web.UI.WebControls.Image.


Well, how does the array of uints relate to the actual image? Where did
you get them from?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Fritz Switzer | last post: by
5 posts views Thread by stef | last post: by
1 post views Thread by =?Utf-8?B?UmljYXJkbyBGdXJ0YWRv?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.