Store BitmapImage in a text file (and back again) - c#

I have a BitmapImage and I need to store the content of this image (as byte array for example) in a text file.
In particular, I have to store this image in the attribute of an XML node, and then I have to read this node to create a new BitmapImage.
What is the fastest way to do this?

Use Convert's ToBase64String to covert Bytes to readable ASCII characters
string bitmapImageAsString=Convert.ToBase64String(binaryData);
You can convert that string back using Convert.FromBase64String
To convert BitmapImage to byte array refer this..
Though you can also use BitmapImage.StreamSource to identify the source stream and then convert that stream to byte array..That would be a better option

Related

How to reduce jpg size saved from a base64 string C#

I'm receiving a base64 string from the client side that represents a jpg and I'm saving it using the following code
byte[] imageBytes = Convert.FromBase64String(base64File.base64Data);
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
Image image = Image.FromStream(ms, true);
image.Save("aPath");
}
The problem is that that the original image entered on the client side (before the base64 encoding) is about 3MB and once that I save it using the method above, it turns to be a 24MB image!
Is there any way to reduce it's size? (I know that sending big images encoded in base64 is not very smart, but I need to find a solution keeping this part)
Just save the bytes, don't convert it back into an image, otherwise you will have to use the same settings as when it was originally saved.
I.e its already the image you want, loading it from a stream and calling save, saves it as a bitmap in probably 32bit
Update
So I tried the code below
byte[] imageBytes = Convert.FromBase64String(base64File.base64Data); File.WriteAllBytes("apath"),imageBytes);
But it generates a 18MB file,
still much larger than the original
Basically this means, when ever you are converting it base64 (server side), it is probably doing the same thing. Maybe its loading it into an Image and saving the bytes before converting it to Base64.
I believe a way to tell if the image is actually a bitmap (in your case) before conversion base64, is just look to see if the first 2 bytes of the file are BM.
Either way, the image is being manipulated on the other side before its converted to base64, maybe its a bitmap or its just reenconding it, its hard to tell. That's to say base64 encoding isn't smart enough to reenconde the image format and inflate the size

Converting Byte Array To Image Throws "Parameter is not valid."

I'm trying to convert several byte arrays (stored in SQL Server 2008 R2 as varbinary(8000)) into images so I can attach them to a PDF file (an example of the data is below). I keep getting "Parameter is not valid." when creating the Image from the MemoryStream. Any help on this?
MemoryStream memoryStream = new MemoryStream(byteArray);
Image image = Image.FromStream(memoryStream);
Bitmap bitmap = new Bitmap(image);
0x0A020101000000005E006500FA00FA0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010C00010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000C1C0C7000FC2FFC1FE80C70007C2FFC1FE1FC7FFC1F03FC1FFC1FE3FC7FFC1F807C1FFC1FE3FC8FFC1F3C1FFC1FE3FC8FFC1FEC1FFC1FE3FC8FFC1FE7FC1FE3FC9FFBFC1FE3FC9FFC1DFC1FE3FC9FFC1CFC1FE3FC9FFC1F3C1FE3FC9FFC1FBC1FE3FC9FFC1F9C1FE3FC9FFC1F9C1FE3FC9FFC1FCC1FE3FC9FFC1FE7E3FC9FFC1FE7E3FCAFF7E3FC2FFC1FCC20007C4FF3E3FC2FFC1F8C300C4FF3E3FC2FFC1F1C2FFC1FC7FC3FF3E3FC2FFC1F1C2FFC1FC3FC3FF3E3FC2FFC1F1C3FF9FC3FF3E3FC2FFC1F1C3FFC1CFC3FF3E3FC2FFC1F1C3FFC1E7C3FF3E3FC2FFC1F1C3FFC1F3C3FF3E3FC2FFC1F1C3FFC1F3C3FF3E3FC2FFC1F1C3FFC1F3C3FF3E3FC2FFC1F1C3FFC1F3C3FF3E3FC2FFC1F1C3FFC1F3C3FF3E3FC2FFC1F1C3FFC1F3C3FF3E3FC2FFC1F1C3FFC1F3C2FFC1FE7E3FC2FFC1F1C3FFC1E7C2FFC1FE7E3FC2FFC1F1C3FFC1C7C2FFC1FCC1FE3FC2FFC1F1C3FFC1C7C2FFC1F9C1FE3FC2FFC1F1C3FFC1DFC2FFC1F9C1FE3FC2FFC1F1C3FFBFC2FFC1F9C1FE3FC2FFC1F1C2FFC1FC3FC2FFC1F3C1FE3FC2FFC1F0C2FFC1F8C3FFC1E7C1FE3FC2FFC1FEC2000FC3FFC1CFC1FE3FC3FFC2000FC3FF9FC1FE3FC9FF3FC1FE3FC8FFC1FE7FC1FE3FC8FFC1F0C1FFC1FE3FC8FFC1E7C1FF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X ver. 2.8 image data, with palette
under linux I just transformed your hex stream from plain text to a binary file called output
echo
"0A020101000000005E006500FA00FA0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010C00010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000C1C0C7000FC2FFC1FE80C70007C2FFC1FE1FC7FFC1F03FC1FFC1FE3FC7FFC1F807C1FFC1FE3FC8FFC1F3C1FFC1FE3FC8FFC1FEC1FFC1FE3FC8FFC1FE7FC1FE3FC9FFBFC1FE3FC9FFC1DFC1FE3FC9FFC1CFC1FE3FC9FFC1F3C1FE3FC9FFC1FBC1FE3FC9FFC1F9C1FE3FC9FFC1F9C1FE3FC9FFC1FCC1FE3FC9FFC1FE7E3FC9FFC1FE7E3FCAFF7E3FC2FFC1FCC20007C4FF3E3FC2FFC1F8C300C4FF3E3FC2FFC1F1C2FFC1FC7FC3FF3E3FC2FFC1F1C2FFC1FC3FC3FF3E3FC2FFC1F1C3FF9FC3FF3E3FC2FFC1F1C3FFC1CFC3FF3E3FC2FFC1F1C3FFC1E7C3FF3E3FC2FFC1F1C3FFC1F3C3FF3E3FC2FFC1F1C3FFC1F3C3FF3E3FC2FFC1F1C3FFC1F3C3FF3E3FC2FFC1F1C3FFC1F3C3FF3E3FC2FFC1F1C3FFC1F3C3FF3E3FC2FFC1F1C3FFC1F3C3FF3E3FC2FFC1F1C3FFC1F3C2FFC1FE7E3FC2FFC1F1C3FFC1E7C2FFC1FE7E3FC2FFC1F1C3FFC1C7C2FFC1FCC1FE3FC2FFC1F1C3FFC1C7C2FFC1F9C1FE3FC2FFC1F1C3FFC1DFC2FFC1F9C1FE3FC2FFC1F1C3FFBFC2FFC1F9C1FE3FC2FFC1F1C2FFC1FC3FC2FFC1F3C1FE3FC2FFC1F0C2FFC1F8C3FFC1E7C1FE3FC2FFC1FEC2000FC3FFC1CFC1FE3FC3FFC2000FC3FF9FC1FE3FC9FF3FC1FE3FC8FFC1FE7FC1FE3FC8FFC1F0C1FFC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C1FE"
| xxd -p -r >output
and then the easy part with file output to get the name of the file format from the utility file.
First, make sure you know the encoding, as the Image.FromStream method knows only a limited number of formats.
Make sure that the Position of your MemoryStream is set properly to 0, and not to the end - otherwise, any reads will begin at the end appear to be empty, instead of starting at the beginning.
Also, note that
This constructor does not expose the underlying stream. GetBuffer throws UnauthorizedAccessException.1
Thus, this could be caused by some of the way Bitmap is handling the underlying stream. IF need be, try making an empty memorystream, copying the byte into it, and then resetting the position
MemoryStream memoryStream = new MemoryStream();
foreach(var b in byteArray) memoryStream.WriteByte(b);
memoryStream.Position = 0;
Image image = Image.FromStream(memoryStream);
The byte array you gave is 1117 bytes long. That's not evenly divisible by 4 or 3, so I'm pretty certain that the image is not raw bytes and is actually encoded in an image format of some sort.
The Image.FromStream() method can't decode an encoded image without knowing the format, and the byte array you gave doesn't specify what that format is. Therefore the parameter you gave is invalid. If you know the format of the image you could attempt to insert a header into the byte array before the rest of the image, and see if that helps. It's curious you have the image but not its header, although it doesn't appear to be a base-64 string either.
Without knowing the image format it could be difficult to convert these byte arrays into images. Is there any way you could find out what they are? How is the image data retrieved to be stored?

Android - Convert Image to Bytes in C# and Convert them to Image in Android

I'm using the C# to convert Images into Bytes or other formats(like Base64) and I'll store them to SQL Server database.Using web service(REST) I would like to get that encoded data and to display as an Image in Android. My Major doubt is that the Bytes or Base64 in the C# same as in the Android ?. Anybody,please give me a suggestion.
They both should work cross language since they're just a serializable representation of the file contents, no real language specific stuff.
For the bytes, you can use the BitmapFactory to get an image in android, and to convert a base64 encoded image to a bitmap, you can use this to get a byte-array and the use the BitmapFactory
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);

Importing an Image from an XML File

I'm trying to produce a system which takes an image in OpenCV, writes it to XML, reads it from XML, and displays it in an Image control in a WPF application.
The problem I'm having is that I'm unclear as to how .NET classes (e.g. BitmapImage) process images from a byte array. I've tried a number of methods and gotten various errors - too numerous to post unless required.
I was wondering if someone could point me in the right direction regarding this? What I want to know is how does the data need to be presented to a .NET object in order to display it in an Image control?
Thanks in advance for your help - it is much appreciated.
EDIT: I expect what I need to do is convert the XML into a byte array and use that, along with a definition of the format, to instantiate a BitmapImage object or equivalent to use as source for the Image control. What I'm not sure of is how to do this.
Use int cvSaveImage(const char* filename, const CvArr* image)
to save the file as an Image.The image format is chosen based on the filename extension
No need to store it in XML if you can directly store it as Image file.
The .NET classes requires an Image.That's it.
Note:
If you want to store red,green,blue values as comma separated strings, it would be very inefficient.Save it in Base64 instead.You can then convert it into bytes and feed it to any .NET image class.Also the .NET ImageConverter Class may be very helpful.
I found that the issue was (presumably) that I wasn't Base64 encoding the XML data before writing it. Thus the solution is to Base64 encode the data before writing to XML, Base64 decoding it on the other side, reading the data as a byte array and using that to instantiate a MemoryStream and then a BitmapImage object.
Thanks for your help everyone.
You can use:
public static BitmapSource Create(
int pixelWidth,
int pixelHeight,
double dpiX,
double dpiY,
PixelFormat pixelFormat,
BitmapPalette palette,
Array pixels,
int stride
)

Managing Images?

I'm working on a class that will help me read a game file, and part of the file is an image. Is there an image object that I can create from a byte array, or should I just store the image as a byte array? If I were to put that image into a picture displayer in winforms, can I do that with a byte array?
What's the best way to store the data from the file?
like this:
byte[] data = getYourImageData();
MemoryStream ms = new MemoryStream(data);
pictureBox1.Image = Image.FromStream(ms);
to answer the other part of your question, it is fine to store it as a byte array - maybe provide a helper method that returns a memory stream as seen above, or alternatively store it in a System.Drawing.Bitmap and return that:
return new Bitmap(ms);
The Bitmap class in System.Drawing supports a constructor that takes a stream as a parameter. This stream can be supplied by a MemoryStream that is created from a byte array.
Once you have the bitmap, a PictureBox can be used to display it.
References:
http://msdn.microsoft.com/en-us/library/z7ha67kw.aspx (for the bitmap)
http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx (for the stream)
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.aspx (PictureBox)

Categories

Resources