Binary Image Extension? - c#

When we convert an image to binary data, (let's say a .png image) is there a way to get the extension back while converting the binary to image again in .net?

Short answer, no. You can't get the name either. The file name is not generally stored in image data.
If you know what the image format is you can use either a sensible, generally recognised extension or a file extension registered to that file type on your system. Hopefully, these will not differ.
If you don't know the format perhaps you could read it before serialising to binary and prefix it to the representation.
For a less general answer please expand your question.
EDIT
I guess you could attempt to display the image using a set of potential formats, then visually assess all succesful decodes to choose the correct format. Somehow, it seems easier to just include the original extension in the binary serialization.

Related

C# My own file format

I'm looking to make my own file format .
that format should contains pictures/pdf/and other files ...
also need to know how I can packer-unpacker for this format to unpack files from it/pack in it & reading the pictures from my own format to picture boxes on my WinForm for example.
I've searched but didn't really found what I'am looking for
I hope someone can help me , thank you
Zip is an excellent choice. Because you can encrypt the file and of course reduce the file size in some cases (text and uncompressed things). But if you want to create your own file format you can easily decide rules for your storage and order inside the file. Then serialize the info into the file. For example by object serialization or by writing the binary date to file object by object .
if you really want to write your own file format then I would suggest one of two things. One, you could do it entirely in binary at which point you would want to do a 'chunk' format. Chunk format is to basically have a header to each subsection. The header contains the size of both the header as well as the size of the payload. Create a serialization class for your header then add the bytes to the filestream from your payload. Actually pretty easy to do.
Second (and easier) way to do this would be to create an XML format. Create a master class for your format then add all of the data as collections of sub classes under that. Once you have that, use any of .net xml serialization classes to serialize it out to disk.
You can also use SQLite for your purposes. It provides dbms power without needing server. That is popular solution for your problem.
System.Data.SQLite is an ADO.NET adapter for SQLite.

Saving different file formats

In my application I receive different files in base64string.
After receiving those base64Strings my application needs to convert them
into their original formats.
These files could be pdf,txt,jpeg image,bmp image,gif image or png image formats.
How do I know what format this file is in order to convert them to their
respective formats. Is there any way the base64string gives this info.
Any help will be appreciated.'
The base64 data only contains the file data itself, no metadata about it (including file name / extension). You could potentially try to parse the first few bytes of the decoded base64 data to try to find out the file type, but an easier approach would be for the service to add this information in some HTTP header (such as Content-Disposition).
I think you only need to convert it to binary format from base64string and save on disk. You only need to get the correct file extension or complete file name so that user can use associated program to open it.
The only reliable means to get the file type is through metadata associated with the file. If this is not available in your case, a workaround is to read the first few bytes of the file. Many common formats require that files of that format begin with a sequence of bytes, known as "magic numbers".
This Wikipedia article provides maic numbers for PDF, JPG, PNG, and GIF formats. BMP files typically begin with the constant 0x42 0x4D (*). Since text files contain only content, it would need to be a default option (i.e, if the first few bytes aren't recognized as a known magic number, assume it is a text file.)
The Base-64 encoding is simply the binary representation of the file. Converting back to a byte sequence and assessing the first few bytes should be sufficient to suggest a file is of a certain type. Note that this is an imperfect workaround; for instance, a text file that happens to start with a magic number (e.g., "BM") may be miscategorized as another type of file.

File extension from System.Drawing.Image

I'm writing a method that needs to save a System.Drawing.Image to a file. Without knowing the original file the Image was created from, is there anyway to determine what file extension it should have?
The best solution I've come up with is to use a Switch/Case statement with the value of Image.RawFormat.
Does it even matter that I save the Image in it's original format? Is an Image generated from a PNG any different from say one generated from a JPEG? Or is the data stored in an Image object completely generic?
While Steve Danner is correct in that an image created from a JPG will look different to an image created from a PNG once it's loaded into memory it's an uncompressed data stream.
This means that you can save it out to any file format you want.
However, if you load a JPG image and then save it as another JPG you are throwing away more information due to the compression algorithm. If you do this repeatedly you will eventually lose the image.
If you can I'd recommend always saving as PNG.
Image.RawFormat has cooties, stay away from it. I've seen several reports of it having no legal value for no apparent reason. Undiagnosed as yet.
You are quite right, it doesn't matter what format you save it to. After you loaded the file, the internal format is the same for any bitmap (not vector) with the same pixel format. Generally avoid recompressing jpeg files, they tend to get bigger and acquire more artifacts. Steve mentions multi-frame files, they need to be saved a different way.
Yes, it definitely matters because different fileformats support different features such as compression, multiple frames, etc.
I've always used a switch statement like you have, perhaps baked into an extension method or something.
To answer your question 'Does it even matter that I save the Image in it's original format?' explicitly: Yes, it does, but in a negative way.
When you load the image, it is uncompressed internally to a bitmap (or as ChrisF calls it, an uncompressed data stream). So if the original image used a lossy compression (for example jpeg), saving it in the same format will again result in loss of information (i.e. more artifacts, less detail, lower quality). Especially if you have repeated actions of read - modify - save, this is something to avoid.
(Note that it is also something to avoid if you are not modifying the picture. Just the repeated decompress - compress cycles will degrade the image quality).
So if disk space is not an issue here (and it usually isn't in the age of hard disks that are big enough for HD video), always store any intermediate pictures in lossless compression formats, or uncompressed. You may consider saving the finall output in a compressed format, depending on what you use it for. (If you want to present those final pictures on the web, jpeg or png would be good choices).

Edit Text in SWF File

I am wondering the capabilities of the SWF Format. I have some text in a Flash Video, which is an email address (xyz#somewhere.com) which I would like to write a C# application to edit. I have the SWF File Format Spec and was wondering if the following is possible:
If I read in the Tag (I am assuming this text is stored as a Static Text Tag, correct me if I am wrong). Once I found the correct tag for my text I then change the value in the tag and save the SWF file. Do you see any problems with this approach?
Chris
Yes, almost certainly there is more to this.
First, the swf is most likely compressed and will need decompressing.
Locating the value you need to change should not be too hard, but you will need to change at least one (probably several) field lengths to cater for the change. This in turn will probably require you to parse the rest of the SWF in order to recalculate the lengths of the various blocks that will be altered.
Not for the faint-hearted.

binary data to and from text conversion

Sometimes i generate data that is as little as 1k. I thought it may be good if i could convert it to text so i can paste it to someone on a forum or on MSN. How can i convert data into text then convert back? How many bits can i use? I must have it compatible with pasting on forums and i would like it to be compatible with msn if the string isnt to long. How can i make the data text safe?
Should i assume the data is only allowed to be 6bits? Is 32-127 the only values available? will i have a problem copy/pasting 127 (ascii for 'DEL'). I am using C#
For safe copy and paste, consider UUEncoding or Base64Encoding. .NET has support for Base64 on the Convert class.
Finding sample code in C# that implements UUEncoding or Base64Encoding isn't too hard:
<Link>
http://arcanecode.com/2007/03/21/encoding-strings-to-base64-in-c/
I think the simplest thing would be to use base64 encoding. It will take any binary data and convert it to text suitable for pasting anywhere.
In C# you can use Convert.ToBase64String (or something like this)
Does your forum allow file attachments? If so just attach the binary file.

Categories

Resources