Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
here i am writing a file length into the first pixel of the image how can i read the exact data from the pixel to the filesize

C#
var fi = new FileInfo(fileName);
 bitmap.SetPixel(0, 0, Color.FromArgb((int)fi.Length));

int filesize;
Posted

You can't, not reliably. It will depend on what format you store your image in: Bitmap should be OK (though I'm not too sure about the Alpha channel: you may want to check for the type of bitmap you are storing in) but any compressed format such as JPG, PNG, GIF will almost certainly destroy the information.

And why would you want to do that? Surely you know the file size already? Or are you trying to "hide" a file in a bitmap? Because there are much better ways than that...
 
Share this answer
 
Comments
vishnulalr 13-Jun-11 9:19am    
thanks its a part of steganography but here just want to write and read the file size
OriginalGriff 13-Jun-11 9:26am    
Figured: Don't do it on a single pixel basis, it will be far too noticable - imagine a single red pixel on a field of white.
Ignoring the JPG fun-and-games and concentrating on "pure" bitmaps, spread all your data (including the file information) across the entire pixel space by using the least significant bit of each color - it will take a large bitmap to hide any real size of file, but it will be a lot less noticeable.
Then when you do read the file info back out, you don't "set it" as the file size, you write that number of bytes - which sets the file size for you.
Sergey Alexandrovich Kryukov 14-Jun-11 0:40am    
Correct, my 5.
--SA
How you set it is trivial. WHERE you set it is the most important part. I would add the length info at the end of the file as opposed to the beginning, because the end of the file is actual pixel data.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Jun-11 0:39am    
No, John. OP uses SetPixel(0, 0), this is always the actual pixel data.
It isn't possible to access meta-data via SetPixel :-) -- all correct.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900