I need to store XML data on a server that only accepts jpeg images. I thought of writing my XML data inside a valid jpeg file. After all, other than the jpeg header, the content of the image file is arbitrary data right?
Is it possible to produce a valid jpeg file, but have its "body" filled with custom bytes?
Of course, I also need to be able to decode the custom jpeg file and restore the data.
I'm not familiar with the jpeg file format, so I'd appreciate an explicit example.
Perhaps just appending the data to a small jpeg will work?
Create a small jpeg.
Append your (obfuscated/encrypted) XML to the file.
Upload to server.
FWIW, you can easily see this works using a Hex editor. Just create a small jpeg and append your xml to the end. Then open it using any image editor.
This is a perfectly valid thing to do to a jpeg file:
Will random data appended to a JPG make it unusable?
Uhmmm it is a strange architecture... but anyway I think this post would be useful:
How to Add 'Comments' to a JPEG File Using C#
so the proposal is add the data as a metadata of a jpeg blank image.
If you want to add your data as the actually jpeg data, you first create a BitmapSource with BitmapSource.Create and put your data in the buffer parameter. Than use the JpegBitmapEncoder to save it as a jpeg file (an example is here).
However, as far as I know, the .Net jpeg encoder is not lossless (even if you set it's quality to 100%) so you will need a third party library that can encode JPEG lossless.
I don't know of a JPEG specific way but there is a PNG/GIF method to encode arbitrary data and pixels. Check out this post. Some sites allow you to upload PNGs and GIFs renamed to JPEG so you could try that.
http://blog.nihilogic.dk/2008/05/compression-using-canvas-and-png.html
He's saving javascript but you could use and text, really.
Related
I found out that there are two ways to read the image info using default c# library. One of them is
System.Drawing.Image image = new Bitmap("file..path");
Another one is :
Image image = Image.FromFile("file..path");
May anyone tell me which one will run faster if I need to read a lot of images(nearly 100TB data).
I found out that there are two ways to read the image info
You know, if it is just the image info you are after then I wouldn't use either function as both load the entire image into memory from disk - a rather wasteful exercise of the computer's resources.
Instead you should just load the image file header whether it be EXIF; BITMAPINFOHEADER or other depending on the image format. There are ways to load such info via .NET (see links below).
Image headers
Apart from RAW image file formats (not necessarily that which is output from SLR cameras), most image file formats have a header that can be loaded prior to loading the image raster data into memory from disk. In fact it is a generally a requirement that the header is read first because otherwise you would not know how much memory to allocate prior to loading the image.
How wide is it?
How tall?
How many bits per pixel (colour depth)?
...and so forth. These are all answered by reading the image file header first. As the name suggests, information about the image is generally near the start of the file. Exact formats and layout depends on the file format in question. See BMP; PNG resources for more info.
Here's some suggestions on loading image headers
Obtain image width and height without loading image in .NET?
Getting image dimensions without reading the entire file
Bitmap Storage
I am getting multiple pngs from another process from its standard output as a stream. I want to take this memory stream and save it as multiple png files. I have looked at PngBitmapEncoder/PngBitmapDecoder, but I can't seem to get a multiple page out of it (whenever I create a decoder using PngBitmapDecoder.Create, decoder.Frames.Count is always 1. Here is how I create the decoder:
BitmapDecoder decoder = PngBitmapDecoder.Create(memStream,
BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.Default);
Am I doing something wrong?
There is no such thing a s multi-page PNG.
A PNG decoder will never return more than one frame.
You need to read each image separately.
You have sample here on msdn
http://msdn.microsoft.com/fr-fr/library/system.windows.media.imaging.bitmapdecoder.aspx
i am getting multiple pngs from another process from its standard
output as a stream
It's not clear what this means. PNG does not support multiple image or pages in one file. Are you receiving several PNG files concatenated as a single stream? If this is the case (that would be rather strange) you don't really need to decode the PNGs, just to split the stream and write each one (blindly) in a different file. A quick and dirty approach (not totally foolproof) is to scan the stream for the PNG signature (8 bytes) to detect the start of a new image.
If you rather want to decode the succesive streams (seems overkill), you can use this pngcs library, instantiating a PngReader for each image; just be sure to call
PngReader.ShouldCloseStream(false) so that the stream is not close when each image ends.
Yes, there is such a thing as a multi-page PNG. It's called MNG (Multiple-image Network Graphics). It's almost as old as PNG (Check libpng.org for the format MNG).
And there is a C# library that can help you with that
http://www.codeproject.com/Articles/35289/NET-MNG-Viewer
In the last 4 years a format called APNG (Animated Portable Network Graphics) started being accepted and used by browsers like Firefox. There is a wrapper for C#
https://code.google.com/p/sharpapng/
Saving multiple PNGs using one file only will be much faster than using multiple files.
I am exporting some data from my database in xml format.
The file exported has an extension as .xml and it is viewed as Excel file.
I want to insert an image in this xml file so that when we view it as Excel we will be able to see the image along with the data.
Whatever I have found from the internet is that there is no straight forward way to insert an image in xml file as xml file were designed for handling the data.
Can any one tell me what is the approach I will have to follow in order to obtain the desired functionality.
xml files are text files, and you can embedd binary data if you encode it with Base64 algorithm.
But to view the image you will need to decode the Base64-string and pass the result binary data to an image viewer implementation.
It cannot be done in MS Excel. You will need to implement your own viewer.
You won't be able to view an image inside of an XML file unless you write a dedicated application for it that knows about your particular requirement.
The reason is that XML is character-based, but images are not - they are binary data. A way to embed your image nevertheless is to transform it to something character-based first. For example, you could Base64-encode your image first and embed the resulting string in your XML. But I suppose there is no way to tell Excel to interpret this data as an image right away.
Because embedding binary files into XML using Base64 is such a common idiom, XML Schema even has its own data type for this: base64Binary.
I am currently developing a web application that receives data from an on-site database. The database developers have developed some web-services that I am able to call and send/receive data to and from the the database.
When I want to display an image the method returns the image in BLOB format. My question is: what is the best way to convert BLOB to .jpg or .bmp so I can display the image correctly? If someone could point me in the right direction that would be great!
Cheers,
Tristan
You can create a generic handler (ashx file) and have it return the byte array.
For example, the url http://www.example.com/GetImage.ashx?imageId=1 could contain code to write the bytes received from the image with id of 1.
You will need to implement the handler code yourself to do the database query, etc. and write the bytes out to the response. You will also need to set the response mime type to jpg, png or whatever format your images are in.
Check out this stackoverflow post for more details: How to bind a MemoryStream to asp:image control?
byte[] array and mimetype are the key words :)
There is no one image format associated with BLOB. It's simply a way of putting raw binary in a database. You need to figure out which format it actually is (libmagic may help), then you can convert if needed.
I have a raw pixel data in a byte[] from a DICOM image.
Now I would like to convert this byte[] to an Image object.
I tried:
Image img = Image.FromStream(new MemoryStream(byteArray));
but this is not working for me. What else should I be using ?
One thing to be aware of is that a dicom "image" is not necessarily just image data. The dicom file format contains much more than raw image data. This may be where you're getting hung up. Consider checking out the dicom file standard which you should be able to find linked on the wikipedia article for dicom. This should help you figure out how to parse out the information you're actually interested in.
You have to do the following
Identify the PIXEL DATA tag from the file. You may use FileStream to read byte by byte.
Read the pixel data
Convert it to RGB
Create a BitMap object from the RGB
Use Graphics class to draw the BitMap on a panel.
The pixel data usually (if not always) ends up at the end of the DICOM data. If you can figure out width, height, stride and color depth, it should be doable to skip to the (7FE0,0010) data element value and just grab the succeeding bytes. This is the trick that most normal image viewers use when they show DICOM images.
There is a C# library called EvilDicom (http://rexcardan.com/evildicom/) that can be used to pull the image out of a DICOM file. It has a tutorial on how to do it on the website.
You should use GDCM.
Grassroots DiCoM is a C++ library for DICOM medical files. It is automatically wrapped to python/C#/Java (using swig). It supports RAW, JPEG 8/12/16bits (lossy/lossless), JPEG 2000, JPEG-LS, RLE and deflated (zlib).
It is portable and is known to run on most system (Win32, linux, MacOSX).
http://gdcm.sourceforge.net/wiki/index.php/GDCM_Release_2.4
See for example:
http://gdcm.sourceforge.net/html/DecompressImage_8cs-example.html
Are you working with a pure standard DICOM File? I've been maintainning a DICOM parser for over a two years and I came across some realy strange DICOM files that didn't completely fulfill the standard (companies implementing their "own" twisted standard DICOM files) . flush you byte array into a file and test whether your image viewer(irfanview, picassa or whatever) can show it. If your code is working with a normal JPEG stream then from my experience , 99.9999% chance that this simply because the file voilate the standard in some strange way ( and believe me , medical companies does that a lot)
Also note that DICOM standard support several variants of the JPEG standard . could be that the Bitmap class doesn't support the data you get from the DICOM file. Can you please write down the transfer syntax?
You are welcome to send me the file (if it's not big) yossi1981#gmail.com , I can check it out , There was a time I've been hex-editing DICOM file for a half a year.
DICOM is a ridiculous specification and I sincerely hope it gets overhauled in the near future. That said Offis has a software suite "DCMTK" which is fairly good at converting dicoms with the various popular encodings. Just trying to skip ahead in the file x-bytes will probably be fine for a single file but if you have a volume or several volumes a more robust strategy is in order. I used DCMTK's conversion code and just grabbed the image bits before they went into a pnm. The file you'll be looking for in DCMTK is dcm2pnm or possibly dcmj2pnm depending on the encoding scheme.
I had a problem with the scale window that I fixed with one of the runtime flags. DCMTK is open source and comes with fairly simple build instructions.