Convert Array of Int to image

C

Clas Hortien

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);
}
w.Close();
fs.Close();
}

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

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

Best regards

Clas
 
K

Kevin Spencer

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
 
J

Jon Skeet [C# MVP]

Clas Hortien said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top