Getting byte data from an Image.

wyrd

Well-known member
Joined
Aug 23, 2002
Messages
1,408
Location
California
With Bitmaps theres an option to LockBits which provides a BitmapData class and access to the actual bytes in memory. However, I want to load something other than a bitmap (a TGA to be more specific). Using Image.LoadFromFile gives me a nice Image object, unfortunately I see nothing similar to LockBits.

Thanks in advance.
 
No kiddin? Wonder how I missed that constructor.
Anyway.. dont know if it loads TGAs or not, I hope so. :) If it doesnt, Ill have to find another format that uses alphas and .NET accepts. There anywhere I can find a list of supported image types?

EDIT:
Maybe it doesnt supports TGAs. I load a TGA using Image.FromFile(file) and then create a bitmap with Bitmap(img), after that it gives me an out of memory exception. Quite odd.
 
Yup it was the TGA file format that was the problem to begin with. I used a PNG file just to test, and it loaded fine using the regular Bitmap constructor (no Image).

Meh.. anyone know a good format that allows alphas and is acceptable by .NET?
 
Im not sure if that is all the formats supported natively but take a look at this class which lists some image formats:
System.Drawing.Imaging.ImageFormat
 
Hmm.. looks like Ill just go ahead with PNG since it allows alpha channels. Thanks for the help guys.
 
Yeah.. I never looked at the PNG format before. They are certainly smaller in file size. Only 6k for an image that was ~24 in both BMP and TGA format. Unless theres some serious reason why I shouldnt be using it, I think Ill stick with it for future projects. :)
 
pngs are good
their format doesnt look lossy at all (like jpgs)
since youre using them in an app its good
but on a webpage it wont show up - you have to physically open it yourself in like notepad or something


btw - 6k for an image?? my pngs (lets say 32x32) are measured in BYTES ;) (dont remmeber how many tho)


edit: earlier, looking at your title
Getting byte data from an Image.
i was gonna suggest using RAW files. the only thing it stores are the bytes in an image (i use them for heightmaps.. well im trying to figure it out)
prob: not sure if you can do Alpha
im not sure if .DDS would work either - use the textureviewer in the sdk , but im not sure if itll suit your purposes

pent
 
in C# you can do something like this:

Code:
#region ConvertImageToBytes
		/// <summary>
		/// Converts Image (from path) to string
		/// </summary>
		/// <param name="imagePath">string</param>
		/// <returns>byte[]</returns>
		public byte[] ConvertImageToBytes(string imagePath)
		{			
			FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
			BinaryReader br = new BinaryReader(fs);

			byte[] b = br.ReadBytes((int)fs.Length);			
			
			br.Close();
			fs.Close();

			return b;
		}
		#endregion

gl
 
Hmm, .png is good but there is a one problem. PNG uses similar bitmap data comprimation like GIF. It means that in all PNG file can be used only 256 different colors of 24bit palette (16,7 milion colors). Theese colors are defined in the head of file. So if you are working with internet bitmaps it is good to use PNG (btw. Mozilla supports Alpha channel) , but if you are working with photos or professional images it is a big restriction.

If you would like to edit images for professional use, I thing you ought to study PDF file structure. :-) OK, I know that it looks difficulty but its better like create new file format. You can downlad pdf SDK form adobe.com, but its pay-for-use.
 
Err, PNG isnt limited to 256 colours - certainly one of the formats it supports is 256 bit palette-based colours but that isnt the only format.
Out of interest it supports palette based in 1, 2, 4 or 8 bit formats, greyscale up to 16bits and true colour up to 48 bits.
 
:eek: Really?

The hell, if this is true then I am the happiest developer worldwide!
...that also means that I had downloaded bad PNG format specifications time ago.

So... PNG! :cool:

Ok, but the first queston was how to extract data from image file...

Any idea?
 
just a tought: use the FreeImage-Library [Sourceforge-page] ..it supports all common Image-Formats and is a lot faster for loading Images that the .NET-Framework. to draw those Images youd need a little bit of Interop- or API-Calls (to be exact you need two of them) but that should be acceptable.. the library is opensource and theres a C#-Wrapper in the package.. you can convert, open and save images in every supported format.

Andreas
 
Dear Hamburger1984,

I will NEVER import to my application something, what is protected by GNU GPL licence - product of Free Software Foundation. FSF with some other
organizations are sects based on communistic ideology wich is recuiting young developers for non-payed work. Best example is GNU/Linux - developers are
creating it for free, but linux distribution management advantages from technical support. Uncrowned (or crowned?) king of this all is Richard Stallman - man, wich resonds expressly comunistic idea - intellectulal work never can be payed as manual work.

You may think - its free, so I can use it and GNU ideology never mind me. But I am from country wich was before second world war country with sewenth worlds best economic and after 40 years of communism we are now at 60th position. So I am looking at GNU bit another.

If you would like to use FreeImage-Library you must paste this in about box:
This software uses the FreeImage open source image library. See http://freeimage.sourceforge.net for details.
FreeImage is used under the (GNU GPL or FIPL), version (licence version).


But I departed out of problem.

So... back to programming:

Time ago was posted in forum Graphics question by ThePentiumGuy, how to draw image from array to screen - but fast.
PlausiblyDamp posted answer - shorcout to PascalsTriangle tutoriual.

In that tutorial is a method how to covert Integer Array to Bitmap object. So must exists way to convert Bitmap to Integer Array i think.
But how? Any idea?

For better understand:

1. Load image from file to bitmap object - No problem

2. Convert Bitmap Object to Integer array - how???

3. Edit the image = edit the array - No problem

4. Save changed image / draw on scren - No problem

So how to convert bitmap object to Integer Array - using .Net framework.


predseda
 
hey hey predseda.. no need to get pissed off here.. if you dont like OpenSource (the idea to share thoughts for free - like for example on this forum [why are you using it? should look for a company to pay for the tips you get here for free]) thats your problem.. I dont see a problem with using such library and giving credit to the people that created it, but thats my opinion.. with that post I just wanted to point out, that theres a faster way to open, convert and save images than using the Framework (which isnt capable to do real converting at all - only 24bpp.. happy birthday!! ;))

Have a nice day!

Andreas
 
Hamburger1984,

I like OpenSource idea for example Mozilla project, but not GNU.
I dont see a problem with using such library and giving credit to the people that created it, but thats my opinion.. - Yes me too, but not GNU. I dont agree with GNU GPL backgroung.

...and save images than using the Framework (which isnt capable to do real converting at all - only 24bpp.. happy birthday!! ;))

24bpp?? wake up man, it suppots up to 64bpp = 16R + 16G +16B + 16A
But I think 24bit = over 16 milion colors is adequate accuracy.

Why to use third-party componet when I can use built-in function?

predseda
 
well okay.. maybe I missread your post.. but as I said - Id use that library because of speed, better compression (why save a grayscale-image at 24 (or 32) bpp when you only need 8bpp?.. but that depends on what you need..


..and btw.. can you show me how to save a .NET Bitmap with 64bpp or if you like with 1bpp (b/w) or 8bpp (grayscale)? if theres a possibility to do this Id like to learn how.. prove me wrong that the .NET-Framework isnt very flexible when it comes to saving Images.. (I know you could use API-Calls to the GDI+ Library, which supports different bpps afaik.. - but Im talking about the Framework..)

Andreas

..and btw.. parts of Mozilla are under GNU-License

..to actually answer the question asked:
take a look at these articles:
- Image Processing Part 1
- Image Processing Part 2
- Image Processing Part 3
- Image Processing Part 4
- Image Processing Part 5
 
Last edited by a moderator:
Dim MyBitMap as Bitmap
MyBitMap = New Bitmap(Width, Height, Imaging.PixelFormat.Format32bppRgb)

Vivat Microsoft .Net ;)

I have seen that tutorials time ago, but I dont know why I thought they are using GetPixel method. The hell! It looks good. And It looks that there is no Bitmap to Integer Array converting!
But I have minimum knowledge about C#...
So I will try to translate it.

Parts of Mozilla are GNU-GPL ?
Maybe, I am not sure, but I have never seen gnu craps (neither word GNU) on mozilla.org

predseda
 
Last edited by a moderator:
Back
Top