Server Help

Non-Subspace Related Coding - c# - saving images to file

tcsoccerman - Sat May 17, 2008 11:15 am
Post subject: c# - saving images to file
I'm trying to save images to a file along with some strings, and i have a few questions.

1.Can i save a whole image "data" in one line.
2.Can i open a whole image "data" (in one line).
3.How can i do this.
Doc Flabby - Sat May 17, 2008 1:41 pm
Post subject:
Fill a byte array with the data you need to save then simply use one of these functions.
1.
http://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes.aspx
2.
http://msdn.microsoft.com/en-us/library/system.io.file.readallbytes.aspx
3. see above.
tcsoccerman - Sat May 17, 2008 4:50 pm
Post subject:
thanks for the response.

the thing is i need to stick those bytes in between some strings.

example:

subspace
rules
everyone
(insert bitmap bytes here in one line)
because
it's
addicting
Bak - Sat May 17, 2008 6:42 pm
Post subject:
lookup how e-mail encodes attachments

http://en.wikipedia.org/wiki/Base64
tcsoccerman - Sat May 17, 2008 9:07 pm
Post subject:
not sure i'm getting it. i didn't see where it does the saving. i'm going to give it a go anyways, using bytes.
Bak - Sat May 17, 2008 9:12 pm
Post subject:
what if your bytes contain newlines
tcsoccerman - Sat May 17, 2008 9:57 pm
Post subject:
i'm going to try something like this

streamwritervar.WriteLine(binarywriter.write(bytes));

.Writeline only writes one line. that should make me safe.
Samapico - Sat May 17, 2008 10:09 pm
Post subject:
You could work with a binary file instead of a text file...
It would need headers and things like that so that your program knows, for example, the size (in bytes) of the image.
Kind of how .lvl files are... there's a bitmap at the start of the file, then there is the tile data.
You can check out the LVZ and eLVL file structure for some more complex examples too.
tcsoccerman - Sun May 18, 2008 10:13 am
Post subject:
ooo i'm not looking to revamp my whole saving system. but isn't there a way to mix binary with text.

example:
hi
samapico
(insert wierd chineese characters/binary)
thanks
for
the
help

that's what I'm hoping to do.
Doc Flabby - Sun May 18, 2008 10:34 am
Post subject:
Encode your binary as base64 as bak said earlier
Code: Show/Hide


public string EncodeTo64(byte[] toEncode)
    {
      return System.Convert.ToBase64String(toEncodeAsBytes);
    }

tcsoccerman - Sun May 18, 2008 11:13 am
Post subject:
i've figured out how to write and read binary. thanks guys.

now i have a small problem.

basically when i want to open it, i need to pass how many bytes to read as a arg. like this: "BinaryReaderVariable.ReadBytes(int count);"

how do i know how many bytes to read?

if i were to save something in that same file inorder to know how many bytes to read, what would it be

(it would look something like this)

write:

binarywriter.write(some int here)
binarywriter.write(write image bytes)

read:

int count = binaryreader.readint32();
binaryreader.readbytes(count);

thank you very much for your help!!!!!
tcsoccerman - Sun May 18, 2008 11:29 am
Post subject:
nvm i got it to work!

int count = byte[].Length;

only problem is that it sordof gets screwed up. lower quality stuff.
Samapico - Sun May 18, 2008 11:50 am
Post subject:
tcsoccerman wrote:
ooo i'm not looking to revamp my whole saving system. but isn't there a way to mix binary with text.

example:
hi
samapico
(insert wierd chineese characters/binary)
thanks
for
the
help

that's what I'm hoping to do.

That might not work properly... because in your line containing the data bytes, there might very well be bytes with the value 13 or 10, which are newline characters... So your data would then be split in different lines.
If you tell it how many bytes to read, however, it should work I guess. Your file should then look like:

the size
of the pic data is
452
(insert binary data)
so you know
how many
to read
when loading
tcsoccerman - Sun May 18, 2008 12:18 pm
Post subject:
yeh thats what i did.

my problem now is that when i try to save an empty string it screws everything up. do i have to go throw every variable in every object and make it = " ";??? can you do a foreach(string in player/staffmember/financeitem)?
tcsoccerman - Sun May 18, 2008 9:18 pm
Post subject:
i got it all worked out. and yes if the value was null or string was empty, i would have to make the string = " ";

i still have that photo quality loss problem though. sometimes it happens sometimes it doesn't. probably something to do with .png/.jpg conversions to .bmp.
Samapico - Mon May 19, 2008 2:29 am
Post subject:
hmmm, are you using a picturebox control? Maybe the image is resized in the control, and then you might be saving the content of the control, instead of the original image
Animate Dreams - Mon May 19, 2008 7:09 pm
Post subject:
Do you really have to use that Base64 thing? I've never saved any pictures, but in C++, I just saved things to files in binary format by putting everything in a struct(using fixed size char arrays for the text) and wrote to the file that way. I don't know what Base64 is, but it doesn't seem like it would be necessary. Or is it a C# thing?
tcsoccerman - Mon May 19, 2008 9:17 pm
Post subject:
Something to do with emails. i didn't use it, i used binary as well, but you have to convert it to bytes first.
Bak - Mon May 19, 2008 9:20 pm
Post subject:
base64 is a way to convert arbitrary binary data into ascii letters and numbers.

the inverters of email couldn't send arbitrary binary data so they needed to convert everything into letters and numbers. result: all email attachments take up 4/3 the download size than they need to.
tcsoccerman - Sat May 24, 2008 9:03 pm
Post subject:
slightly related to this problem, google has failed to help me with my imagelist problems.

i'm trying to store large images in the imagelist, but it always reduces the images to the size of small icons (256x256 to like 16x16). When i do "imagelist.ImageSize = 256, 256", it gives index errors.

Has anyone here successfuly stored large images in imagelists?
k0zy - Sun May 25, 2008 9:14 am
Post subject:
tcsoccerman wrote:
slightly related to this problem, google has failed to help me with my imagelist problems.

i'm trying to store large images in the imagelist, but it always reduces the images to the size of small icons (256x256 to like 16x16). When i do "imagelist.ImageSize = 256, 256", it gives index errors.

Has anyone here successfuly stored large images in imagelists?


Code: Show/Hide
imageList1.ImageSize = new Size(255,255);

http://msdn.microsoft.com/en-us/library/system.windows.forms.imagelist(VS.71).aspx

What did you google?
Second hit on the query "imagelist larger images"
Samapico - Mon May 26, 2008 6:36 pm
Post subject:
Don't use imageLists :S

Well, in some cases, it's probably good to use, but I would avoid it for large images.
ImageLists are usually used for all the small images of a toolbar, for example.
I'd use an array of Images if I were you... gives you more flexibility, as images can have different sizes.

And as Bob Dole pointed out, you can't just write "256, 256"... Always check the types. ImageSize is definitly a 'Size' type variable. So you have to set it to a 'Size' variable. A 'new' object often works for these things.
tcsoccerman - Mon May 26, 2008 7:55 pm
Post subject:
thats what i did, i was just using psuedo code. ty anyways.

i'll use an image array then, getting the images from the same directory as the executable.
tcsoccerman - Mon May 26, 2008 8:00 pm
Post subject:
not gonna lie, must of the questions i ask here i shouldn't, because by the time you answer i already did what you answered with. I'm just lazy. thanks anyways guys.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group